GO causes error when used in EXEC: “Incorrect syntax near 'GO'.”

前端 未结 4 1531
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 18:24

I\'ve created this stored procedure which dynamically creates the same trigger for all my tables:

USE [MyDatabase]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIE         


        
4条回答
  •  天命终不由人
    2021-01-21 18:38

    The error says what the problem is exactly. You are creating a dynamic sql statement to be run all at once by the EXEC statement. GO is a batch separator for use in a command line interface or SSMS. It separates multiple statements to be executed separately. Therefore, you cannot have GO in query statements to be executed by EXEC or sp_executesql. Simply remove the GO statement, or create two queries to be run by two EXEC statements.

提交回复
热议问题