I have five scripts that must be run against each of three database. At the top of each script are three USE statements - one for each database. Two of the three are commented out. There is a potential problem of ensuring that these all have the same database "uncommented". Is there any way to set a field in a table to hold the name of the desired database and then use this to load a variable in USE statement
As in:
I've tried loading the database name into a holding table and then running
While this results in a response of
As in:
Code:
USE @DBName
GO
I've tried loading the database name into a holding table and then running
Code:
DECLARE @DBName NVARCHAR(100)
DECLARE @SQLStr NVARCHAR(4000)
SELECT @DBName = fldDBName FROM dbo.tblHoldTable
SET @SQLStr = 'USE ' + @DBName
EXEC sp_executesql @SQLStr
While this results in a response of
the database setting doesn't change.Commands completed successfully.