Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

USE @database syntax error

Status
Not open for further replies.

xp8103

Programmer
May 22, 2001
62
US
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
 
You would need to use EXEC, something like:
Code:
EXEC ('Use '+ @database)
Hope this helps


HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Yep, that works. But I discovered it doesn't. USE isn't recognized inside the loop because USE Master was defined first for this thread. Putting the functions inside the loop in a string and executing it spawns another thread where USE @database works. Thanks!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top