In TSQL I frequently use the use
(excuse me for the wordplay):
use choosenOne
set the choosenOne
database as active database.
Suppose now something like:
DECLARE @NewDB varchar(255)
SET @NewDB = 'choosenOne'
I want dinamically compose a valid use command, but I have no luck, since these codes aren't working:
use @NewDB -- Incorrect syntax near '@NewDB'.
EXEC('use '+@NewDB) -- No runtime error but no change of db is performed
What is the right way?