DECLARE @SQL VARCHAR(100)
DECLARE @dbName VARCHAR(100)--
SET @dbName = 'somedbname'--
SET @sql = 'USE [' + @dbName + ']' + CHAR(13) + CHAR(10)
SET @sql = @sql + 'GO' + CHAR(13) + CHAR(10)-- Print the command
EXEC (@sql)
When I run this it gives error Incorrect syntax near ‘GO’ has someone found workaround to this?
Requirement : I need to include stored procedure creation in switched database.
GOis not a SQL statement – it is a command recognized by the SQL Server utilities (e.g. sqlcmd, osql, SQL Server Management Studio code editor).However, you can change the database without the
GOcommand.