Question : Use statement with a variable DBName

I'm having trouble getting the "USE" statement to accept a variable. I pass @i_dbname to a stored procedure and need it to be accepted like so

USE @i_dbname

so that I can subsequently do a DBCC showfilestats and get the relevant info for the correct database. Could u please tell me if this is possible or suggest an alternative solution.
Thanks,
Seán

Answer : Use statement with a variable DBName

>>  or suggest an alternative solution. <<

As noted above by others, you cannot put USE in a stored proc.

If you want to issue the USE dynamically (within an EXEC), you must put the other command(s) immediately after it; the context of the USE will end when the EXEC does.  For example:

EXEC('USE ' + @i_dbname + ' DBCC commandName ')
Random Solutions  
 
programming4us programming4us