GO 在 EXEC 中使用时会导致错误:“'GO' 附近的语法不正确." [英] GO causes error when used in EXEC: "Incorrect syntax near 'GO'."

查看:31
本文介绍了GO 在 EXEC 中使用时会导致错误:“'GO' 附近的语法不正确."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了这个存储过程,它为我的所有表动态创建相同的触发器:

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_IDENTIFIER ON
GO

--Drop Stored Procedure
BEGIN TRY
     DROP PROCEDURE [dbo].[sp_CreateDataChangedTrigger]
END TRY
BEGIN CATCH
END CATCH
GO

--Create Stored Procedure

-- ================================================
-- Template generated from Template Explorer using:
-- Create Procedure (New Menu).SQL
--
-- Use the Specify Values for Template Parameters 
-- command (Ctrl-Shift-M) to fill in the parameter 
-- values below.
--
-- This block of comments will not be included in
-- the definition of the procedure.
-- ================================================

-- =============================================
-- Author:      Scott Bass
-- Create date: 06JUL2014
-- Description: Create Data Change triggers
-- =============================================
CREATE PROCEDURE sp_CreateDataChangedTrigger
    -- Add the parameters for the stored procedure here
    @TableName varchar(255), 
    @TableKey  varchar(255),
    @Debug     bit=1
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

     DECLARE @SQL varchar(max);

     SET @SQL = '
--Drop Trigger
BEGIN TRY
     DROP TRIGGER [dbo].[TR_' + @TableName + '_Audit]
END TRY
BEGIN CATCH
END CATCH
GO

--Create Trigger
CREATE TRIGGER [dbo].[TR_' + @TableName + '_Audit]
     ON [dbo].[' + @TableName + ']
     AFTER INSERT, UPDATE, DELETE
AS
BEGIN
     SET NOCOUNT ON
     DECLARE @event_type [char]

     --Get Event Type
     IF EXISTS(SELECT * FROM INSERTED)
     IF EXISTS(SELECT * FROM DELETED)
          SELECT @event_type = ''U''
     ELSE
          SELECT @event_type = ''I''
     ELSE
     IF EXISTS(SELECT * FROM deleted)
          SELECT @event_type = ''D''
     ELSE
     --no rows affected - cannot determine event
          SELECT @event_type = ''K''

     IF @event_type IN (''I'',''U'') BEGIN
          DECLARE @CurrentUserID INT;
          SELECT  @CurrentUserID = u.UserID
          FROM    [dbo].[dim_Users] u
          WHERE   u.[Username] = dbo.udfUserName()

          UPDATE  t
          SET     DateModified = GETDATE(),
                  WhoModifiedID = @CurrentUserID
          FROM    INSERTED e
          JOIN    [dbo].[' + @TableName + '] t ON e.[' + @TableKey + '] = t.[' + @TableKey + ']
     END

     IF @event_type = ''D'' BEGIN
          no_op:  --Nothing for now
     END
END
GO
';

     IF @Debug=1 BEGIN
          set nocount on;
          print @SQL;
     END
     ELSE BEGIN
          exec(@SQL);
     END    
END
GO

如果我使用调试选项调用 SP:

If I call the SP with the debug option:

SET NOCOUNT ON;

DECLARE @return_value int

EXEC    @return_value = [dbo].[sp_CreateDataChangedTrigger]
        @TableName = N'dim_Status',
        @TableKey = N'StatusID',
        @Debug = 1

SELECT  'Return Value' = @return_value

GO

然后从消息窗口提交结果,它工作正常.

Then submit the results from the Messages window, it works fine.

但是,当我关闭 @Debug 开关时,我收到以下错误消息:

But, when I turn the @Debug switch off, I get these error messages:

消息 102,级别 15,状态 1,第 10 行
GO"附近的语法不正确.
消息 111,级别 15,状态 1,第 13 行
CREATE TRIGGER"必须是查询批处理中的第一条语句.
消息 102,级别 15,状态 1,第 51 行
GO"附近的语法不正确.

Msg 102, Level 15, State 1, Line 10
Incorrect syntax near 'GO'.
Msg 111, Level 15, State 1, Line 13
'CREATE TRIGGER' must be the first statement in a query batch.
Msg 102, Level 15, State 1, Line 51
Incorrect syntax near 'GO'.

谢谢...

推荐答案

1) EXEC[UTE] 只能执行 T-SQL 语句.

1) EXEC[UTE] can execute only T-SQL statements.

GO 不是 T-SQL 语句.

GO 不是 Transact-SQL 语句;这是一个被识别的命令sqlcmd 和 osql 实用程序以及 SQL Server Management Studio 代码编辑.SQL Server 实用程序将 GO 解释为一个信号,它们应该将当前一批 Transact-SQL 语句发送到SQL 服务器.

GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor. SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server.

2) 你可以替换

     SET @SQL = '
--Drop Trigger
BEGIN TRY
     DROP TRIGGER [dbo].[TR_' + @TableName + '_Audit]
END TRY
BEGIN CATCH
END CATCH
GO

--Create Trigger
CREATE TRIGGER [dbo].[TR_' + @TableName + '_Audit]

DECLARE @TriggerName SYSNAME;
SET @TriggerName = 'TR_' + @TableName + '_Audit';

IF EXISTS (
    SELECT  *
    FROM    sys.triggers 
    WHERE   parent_id = OBJECT_ID(@TableName)
    AND     name = @TriggerName
)
BEGIN
     SET @SQL = N'DROP TRIGGER [dbo].' + QUOTENAME(@TriggerName);
     EXEC(@SQL);
END

    SET @SQL = '
--Create Trigger
CREATE TRIGGER [dbo].[TR_' + @TableName + '_Audit]

或(更好)

DECLARE @TriggerName SYSNAME;
SET @TriggerName = 'TR_' + @TableName + '_Audit';

IF NOT EXISTS (
    SELECT  *
    FROM    sys.triggers 
    WHERE   parent_id = OBJECT_ID(@TableName)
    AND     name = @TriggerName
)
BEGIN
     SET @SQL = N'CREATE TRIGGER [dbo].' + QUOTENAME(@TriggerName) + 'ON ' + @TableName + ' AFTER INSERT, UPDATE, DELETE AS BEGIN SELECT NULL END';
     EXEC(@SQL);
END

    SET @SQL = '
--Alter Trigger
ALTER TRIGGER [dbo].[TR_' + @TableName + '_Audit]

注意:对象的名称应为 NVARCHAR(128)SYSNAME.

Note: The object's name should be NVARCHAR(128) or SYSNAME.

这篇关于GO 在 EXEC 中使用时会导致错误:“'GO' 附近的语法不正确."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆