DSharper
DSharper

Reputation: 3217

Is there a way to include go in dymanic SQL?

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.

Upvotes: 0

Views: 58

Answers (1)

Marek Grzenkowicz
Marek Grzenkowicz

Reputation: 17353

GO is 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 GO command.

Upvotes: 4

Related Questions