Score:0

Alter Database recovery model & Shrink log, DB dynamically

sx flag

I normally do on daily basis these operations in GUI

  • Alter database to simple recovery model
  • shrink log file
  • shrink database file
  • Alter database to full recovery model

I can understand script but can't write one.

I just want to give DB name then script must execute above tasks and possible show log of it

Declare @DBname varchar(50) Set @DBname = 'AdventureWorks'

After these 2 lines

joeqwerty avatar
cv flag
Ummm... why not just leave the database in the Simple Recovery model? Why are you switching back and forth?
Shadow777 avatar
sx flag
@joeqwerty, so that in future i can do tail log back up in case of failure.
Ben Thul avatar
cn flag
Is your proposal any easier than leaving the database in full recovery and taking log backups throughout the day?
Shadow777 avatar
sx flag
@BenThul ,yes I hope so
cn flag
Sorry, are you mistaken us for a code writing service? Writing trivial scripts like this is baseline knowledge for admins. And no, we are not a code writing service.
Score:-1
in flag

Please try below Dynamic T-SQL statements:

DECLARE @exec nvarchar(max);
DECLARE @db  nvarchar(max) = N'AdventureWorks';

SET @exec = N'ALTER DATABASE ' + QUOTENAME(@db) + N' SET RECOVERY SIMPLE';
EXEC sp_executesql @exec;

SET  @exec = N'DBCC SHRINKFILE (' + QUOTENAME(@db) + N')';
EXEC sp_executesql @exec;

SET  @exec = N'DBCC SHRINKFILE (' + QUOTENAME(@db) + N'_log)';
EXEC sp_executesql @exec;

SET @exec = N'ALTER DATABASE ' + QUOTENAME(@db) + N' SET RECOVERY FULL';
EXEC sp_executesql @exec;
    

Add this code to a SQL Server Agent job, that will keep a history of the executions. You can later make a right click on the job and see its execution history.

The database files will be shrinked to the size specified when the files were created.

Make sure AdventureWorks and AdventureWorks_log are the logical names of the database files.

Shadow777 avatar
sx flag
Thank you for the response but I already using this daily basis . Now what I expecting I don't want mention db name again and again , instead how about we use a variable. Which stores my db name and does exactly what you wrote.now I just need to mention db name only once So is this possible if so how ?
Alberto Morillo avatar
in flag
@Shadow777 Let me know if the changes I made to my initial answer is what you are looking for
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.