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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamic use 2

Status
Not open for further replies.

simian336

Programmer
Joined
Sep 16, 2009
Messages
723
Location
US
I have a little script to shrink log files.

The problem is when I run

set @sql='DBCC SHRINKFILE ('+ @logfilename +' , 500)'
print (@sql)
exec (@sql)

I need to be in that database and

set @test = 'use [Warehouse]'
print @test
exec @test

does not work. I think I had solved this one time before but I cannot find my solution.

Thanks

Simi
 
Code:
set @sql='USE [Warehouse]
DBCC SHRINKFILE ('+ @logfilename +' , 500)'
print (@sql)
exec (@sql)

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Hey Borislav,

I tried that as well. It stays in the Master and the error is...

"Could not locate file 'Build_log' for database 'Warehouse' in sys.database_files. The file either does not exist, or was dropped.
 
Then maybe you do not have Build_log for Warehouse DB?

It will stays in master because the EXEC is ran in another batch.

Could you execute this w/o using dynamics (just for testing)
Code:
USE [Warehouse]
DBCC SHRINKFILE (Build_log , 500)

Borislav Borissov
VFP9 SP2, SQL Server 2000/2005.
 
Hey Borislav,

I'm an idiot, I simplified my example to much and did not notice.

set @dbname= 'Warehouse'
set @test = 'use ' + @dbname
print @test
exec @test

and yes, non-dynamic works.

Thanks

Simi

 
I think I got it...

set @sql='USE [' + @dbname + '] DBCC SHRINKFILE ('+ @logfilename +' , 400)'

Thanks

Simi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top