I am trying to set up a procedure to take care of truncating and shrinking the t-logs of all the user db's on my server. I keep getting a syntax error on the "USE @database" line. what the heck am I missing???
USE Master
DECLARE @fileName varchar(50)
DECLARE @database varchar (100)
DECLARE dbnames Cursor
FOR SELECT Name
FROM sysdatabases
OPEN dbnames
FETCH NEXT FROM dbnames INTO @database
WHILE (@@Fetch_Status <> -1)
BEGIN
IF @database NOT IN ('master','model','tempdb','msdb')
BEGIN
USE @database
SELECT @fileName = NAME FROM sys.database_files
BACKUP LOG @database WITH TRUNCATE_ONLY
DBCC SHRINKFILE (@fileName)
END
FETCH NEXT FROM dbnames INTO @database
END
CLOSE dbnames
DEALLOCATE dbnames
USE Master
DECLARE @fileName varchar(50)
DECLARE @database varchar (100)
DECLARE dbnames Cursor
FOR SELECT Name
FROM sysdatabases
OPEN dbnames
FETCH NEXT FROM dbnames INTO @database
WHILE (@@Fetch_Status <> -1)
BEGIN
IF @database NOT IN ('master','model','tempdb','msdb')
BEGIN
USE @database
SELECT @fileName = NAME FROM sys.database_files
BACKUP LOG @database WITH TRUNCATE_ONLY
DBCC SHRINKFILE (@fileName)
END
FETCH NEXT FROM dbnames INTO @database
END
CLOSE dbnames
DEALLOCATE dbnames