Unfortunately not.
If you cannot execute the rest of your batch as dynamic SQL.
Using execute to dynamically execute SQL will change the context for the scope of the execute , but it will not leave a lasting effect on the scope of the execute .
In other words, it is:
DECLARE @db VARCHAR(100) SET @db = 'SweetDB' EXECUTE('use ' + @db)
Will not install the current database forever, but if you changed the above code as follows:
DECLARE @db VARCHAR(100) SET @db = 'SweetDB' EXECUTE('use ' + @db + ';select * from sysobjects') select * from sysobjects
Then the result of these two queries will be different (if you are not already in SweetDB), since the first choice made inside execute is executed in SweetDB, and the second one is not.
Lasse Vรฅgsรฆther Karlsen Jun 01 '09 at 23:36 2009-06-01 23:36
source share