How can I use a variable as db context?
Create Procedure [dbo].[prName] (@dbname varchar(25)) as
begin
use master
some sql
<!-- I need to use master for some functions stored in master -->
use @dbname
exec('SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE [TABLE_CATALOG] = '+@dbname+' and TABLE_NAME=table123')
end
GO
Thanks
I think you might want to use the following:
The
USEstatement is not allowed in a stored procedure. If you are passing in the database name, then you do not need theUSEstatement, the database name will be included in your sql query.Edit: Based on your edit that you need to access items in master, then all you need to do is execute your sql, specifying the need of
masterjust use fully qualified sql.