SQL语法失败 - 需要经验丰富的SQL眼睛

时间:2016-02-09 16:46:43

标签: sql-server tsql

我有一个软件,它从我们在业务中使用的旧Linux服务器的后端提供SQL数据。

我们不时要将这些数据的'BULK INSERT'写入我们的SQL,它的作用是DROPS相关的表和任何附加的东西(触发器等)并重建它们并重新填充数据。

该软件提供了Pre-SQL和Post-SQL'Scripting'区域,您可以在其中放置自己的脚本。我想在那里放置一个脚本,但它总是因语法问题而失败。

我在下面复制并粘贴:

CREATE TRIGGER [dbo].[Tr_icp_Dapolicy_Insert] 
ON  [dbo].[icp_Dapolicy] 
AFTER INSERT
AS 
BEGIN
    INSERT INTO T_FIG_PreDialler (Branch@, Key@, PolicyRef@, ClientRef@, Create_Date, Create_Time, Copied, CopyDateTime) 
        SELECT 
            Branch@, Key@, PolicyRef@, ClientRef@, Create_date, Create_time, 0, GETDATE() 
        FROM 
            INSERTED
        WHERE 
            Branch@ <>1
END

CREATE TRIGGER [dbo].[Tr_icp_Dapolicy_Update] 
ON  [dbo].[icp_Dapolicy] 
AFTER UPDATE
AS 
BEGIN
    IF UPDATE(#Exec)
    BEGIN
        INSERT INTO T_FIG_PreDiallerUpdates (Branch@, Key@, NewExec, OldExec) 
            SELECT 
                INSERTED.Branch@, INSERTED.Key@, INSERTED.#Exec, DELETED.#Exec 
            FROM 
                INSERTED
            INNER JOIN 
                DELETED ON INSERTED.Branch@ = DELETED.Branch@
                        AND INSERTED.Key@ = DELETED.Key@
            WHERE 
                ((INSERTED.#Exec = ''9999'') AND (DELETED.#Exec <> ''9999'')) 
                OR ((INSERTED.#Exec <> ''9999'') AND (DELETED.#Exec = ''9999''))
                AND (INSERTED.Branch@ in (0, 2))
    END
END

CREATE TRIGGER [dbo].[Tr_icp_brcledger_Insert] 
ON  [dbo].[icp_brcledger] 
AFTER INSERT
AS 
BEGIN
    INSERT INTO T_FIG_Trigger_Ledger_Add (Branch@, Key@, PolicyRef@, ClientRef@, [Suffix@],
                [Polref], [Poltype], [Suffix], [Trantype], [Dt_raised],[Status], [Ledger_dt], [Remarks_4], 
                [#Exec], [Datecreated], [Operator], [LastUpdatedTime])
        SELECT 
            Branch@, Key@, PolicyRef@, ClientRef@, [Suffix@], 
            [Polref], [Poltype], [Suffix], [Trantype], [Dt_raised], [Status], [Ledger_dt], [Remarks_4],
            [#Exec], [Datecreated], [Operator], [LastUpdatedTime]
        FROM 
            INSERTED
        WHERE 
            (INSERTED.Trantype IN (''New Business'', ''Renewal'', ''Transfrd NB''))
            OR ((INSERTED.Branch@ = 2) 
              AND (INSERTED.Poltype = ''LE'')
              AND (INSERTED.Trantype = ''Charge''))
END

CREATE TRIGGER [dbo].[Tr_icp_brcledger_Update] 
ON  [dbo].[icp_brcledger] 
AFTER UPDATE
AS 
BEGIN
    DECLARE @Now DateTime;
    SET @Now = GETDATE();

    INSERT INTO T_FIG_Trigger_Ledger_Update
              (Branch@, Key@, PolicyRef@_Before, Poltype_Before, Trantype_Before, Ledger_dt_Before, 
                    Remarks_4_Before, #Exec_Before, Datecreated_Before, Operator_Before, 
                    LastUpdatedTime_Before, PolicyRef@_After, Poltype_After, Trantype_After, Ledger_dt_After, 
                    Remarks_4_After, #Exec_After, Datecreated_After, Operator_After, 
                    LastUpdatedTime_After, Copied, CopyDateTime, 
                    Orig_debt_Before, Comm_amt_Before, Orig_debt_After, Comm_amt_After)
        SELECT 
            INSERTED.[Branch@], INSERTED.[Key@],
            DELETED.[PolicyRef@] AS [PolicyRef@_Before],
            DELETED.[Poltype] AS [Poltype_Before],
            DELETED.[Trantype] AS [Trantype@_Before],
            DELETED.[Ledger_dt] AS [Ledger_dt_Before],
            DELETED.[Remarks_4] AS [Remarks_4_Before],
            DELETED.[#Exec] AS [#Exec_Before],
            DELETED.[Datecreated] AS [Datecreated_Before],
            DELETED.[Operator] AS [Operator_Before],
            DELETED.[LastUpdatedTime] AS [LastUpdatedTime_Before],
            INSERTED.[PolicyRef@] AS [PolicyRef@_After],
            INSERTED.[Poltype]AS [Poltype_After],
            INSERTED.[Trantype]AS [Trantype@_After],
            INSERTED.[Ledger_dt] AS [Ledger_dt_After],
            INSERTED.[Remarks_4] AS [Remarks_4_After],
            INSERTED.[#Exec] AS [#Exec_After],
            INSERTED.[Datecreated] AS [Datecreated_After],
            INSERTED.[Operator] AS [Operator_After],
            INSERTED.[LastUpdatedTime] AS [LastUpdatedTime_After],
            0, @Now,
            DELETED.Orig_debt AS [Orig_debt_Before],
            DELETED.Comm_amt AS [Comm_amt_Before],
            INSERTED.Orig_debt AS [Orig_debt_After],
            INSERTED.Comm_amt AS [Comm_amt_After]
        FROM 
            DELETED 
        INNER JOIN 
            INSERTED ON DELETED.Branch@ = INSERTED.Branch@ AND DELETED.Key@ = INSERTED.Key@
        WHERE 
            (INSERTED.PolicyRef@ <> DELETED.PolicyRef@
            OR INSERTED.Poltype <> DELETED.Poltype
            OR INSERTED.Trantype <> DELETED.Trantype
            OR INSERTED.Ledger_dt  <> DELETED.Ledger_dt
            OR INSERTED.Ledger_dt is null and DELETED.Ledger_dt is not null
            OR INSERTED.Ledger_dt is not null and DELETED.Ledger_dt is null
            OR ISNULL(INSERTED.Remarks_4, ''NULL'') <> isnull(DELETED.Remarks_4, ''NULL'')
            OR ISNULL(INSERTED.#Exec, ''NULL'') <> isnull(DELETED.#Exec, ''NULL'')
            OR ISNULL(INSERTED.Operator, ''NULL'') <> isnull(DELETED.Operator, ''NULL'')
            OR INSERTED.Operator <> DELETED.Operator
            OR INSERTED.Orig_debt <> DELETED.Orig_debt
            OR INSERTED.Comm_amt <> DELETED.Comm_amt)
            AND (INSERTED.Trantype <> ''Journal'')
END

CREATE TRIGGER [dbo].[Tr_icp_brcledger_Delete] 
ON  [dbo].[icp_brcledger] 
AFTER DELETE
AS 
BEGIN
    INSERT INTO T_FIG_Trigger_Ledger_Delete (Branch@, Key@,PolicyRef@, TranType, PolType)
        SELECT 
            Branch@, Key@,PolicyRef@, Trantype, Poltype
        FROM 
            DELETED
        WHERE 
            (DELETED.Trantype <> ''Journal'')
END

当它试图在BULK INSERT结束时运行它时,我得到如下错误:

  

com.microsoft.sqlserver.jdbc.SQLServerException:关键字“TRIGGER”附近的语法不正确。

现在我不是在寻求软件方面的帮助,我的问题是:

上述失败的语法是什么?我可以在SQL查询窗口中运行它并且它成功了,这些软件开发人员在这个postdone-sql脚本运行之前编码的是什么导致语法错误?

感谢您对此提供的任何和所有帮助,因为软件供应商本身几乎没有任何线索,并且未能在2年内回答这个问题。

更新

尝试将整个事物包装在var char中并执行它,这也失败了

 DECLARE @CreateTriggers VARCHAR(MAX);

    SET @CreateTriggers = 'CREATE TRIGGER [dbo].[Tr_icp_Dapolicy_Insert] 
                       ON  [dbo].[icp_Dapolicy] 
                       AFTER INSERT
                    AS 
                    BEGIN
                                    INSERT INTO T_FIG_PreDialler (Branch@, Key@, PolicyRef@, ClientRef@, Create_Date, Create_Time, Copied, CopyDateTime) 
                                                    SELECT Branch@, Key@, PolicyRef@, ClientRef@, Create_date, Create_time, 0, GETDATE() FROM INSERTED
                                                    WHERE Branch@ <>1

                    END'

    EXEC(@CreateTriggers);

2 个答案:

答案 0 :(得分:1)

这可能与通过JDBC发送语句的软件有关。

或者他们可能会批量发送前/后脚本以及其他内容,因此SQL Server会因为“创建触发器”而抱怨。必须是批量中的第一件事。

其他有类似问题的人建议选择将整个事物包装在exec(' ')子句中。

请参阅Create mssql trigger from java program

以及另一个问题的答案:https://stackoverflow.com/a/951956/22194

答案 1 :(得分:0)

这应该有效,你在每个CREATE语句后都缺少GO语句。每个触发器创建脚本都必须在其自己的批处理中:

CREATE TRIGGER [dbo].[Tr_icp_Dapolicy_Insert] 
ON  [dbo].[icp_Dapolicy] 
AFTER INSERT
AS 
BEGIN
    INSERT INTO T_FIG_PreDialler (Branch@, Key@, PolicyRef@, ClientRef@, Create_Date, Create_Time, Copied, CopyDateTime) 
        SELECT 
            Branch@, Key@, PolicyRef@, ClientRef@, Create_date, Create_time, 0, GETDATE() 
        FROM 
            INSERTED
        WHERE 
            Branch@ <>1
END
GO

CREATE TRIGGER [dbo].[Tr_icp_Dapolicy_Update] 
ON  [dbo].[icp_Dapolicy] 
AFTER UPDATE
AS 
BEGIN
    IF UPDATE(#Exec)
    BEGIN
        INSERT INTO T_FIG_PreDiallerUpdates (Branch@, Key@, NewExec, OldExec) 
            SELECT 
                INSERTED.Branch@, INSERTED.Key@, INSERTED.#Exec, DELETED.#Exec 
            FROM 
                INSERTED
            INNER JOIN 
                DELETED ON INSERTED.Branch@ = DELETED.Branch@
                        AND INSERTED.Key@ = DELETED.Key@
            WHERE 
                ((INSERTED.#Exec = ''9999'') AND (DELETED.#Exec <> ''9999'')) 
                OR ((INSERTED.#Exec <> ''9999'') AND (DELETED.#Exec = ''9999''))
                AND (INSERTED.Branch@ in (0, 2))
    END
END
GO

CREATE TRIGGER [dbo].[Tr_icp_brcledger_Insert] 
ON  [dbo].[icp_brcledger] 
AFTER INSERT
AS 
BEGIN
    INSERT INTO T_FIG_Trigger_Ledger_Add (Branch@, Key@, PolicyRef@, ClientRef@, [Suffix@],
                [Polref], [Poltype], [Suffix], [Trantype], [Dt_raised],[Status], [Ledger_dt], [Remarks_4], 
                [#Exec], [Datecreated], [Operator], [LastUpdatedTime])
        SELECT 
            Branch@, Key@, PolicyRef@, ClientRef@, [Suffix@], 
            [Polref], [Poltype], [Suffix], [Trantype], [Dt_raised], [Status], [Ledger_dt], [Remarks_4],
            [#Exec], [Datecreated], [Operator], [LastUpdatedTime]
        FROM 
            INSERTED
        WHERE 
            (INSERTED.Trantype IN (''New Business'', ''Renewal'', ''Transfrd NB''))
            OR ((INSERTED.Branch@ = 2) 
              AND (INSERTED.Poltype = ''LE'')
              AND (INSERTED.Trantype = ''Charge''))
END
GO

CREATE TRIGGER [dbo].[Tr_icp_brcledger_Update] 
ON  [dbo].[icp_brcledger] 
AFTER UPDATE
AS 
BEGIN
    DECLARE @Now DateTime;
    SET @Now = GETDATE();

    INSERT INTO T_FIG_Trigger_Ledger_Update
              (Branch@, Key@, PolicyRef@_Before, Poltype_Before, Trantype_Before, Ledger_dt_Before, 
                    Remarks_4_Before, #Exec_Before, Datecreated_Before, Operator_Before, 
                    LastUpdatedTime_Before, PolicyRef@_After, Poltype_After, Trantype_After, Ledger_dt_After, 
                    Remarks_4_After, #Exec_After, Datecreated_After, Operator_After, 
                    LastUpdatedTime_After, Copied, CopyDateTime, 
                    Orig_debt_Before, Comm_amt_Before, Orig_debt_After, Comm_amt_After)
        SELECT 
            INSERTED.[Branch@], INSERTED.[Key@],
            DELETED.[PolicyRef@] AS [PolicyRef@_Before],
            DELETED.[Poltype] AS [Poltype_Before],
            DELETED.[Trantype] AS [Trantype@_Before],
            DELETED.[Ledger_dt] AS [Ledger_dt_Before],
            DELETED.[Remarks_4] AS [Remarks_4_Before],
            DELETED.[#Exec] AS [#Exec_Before],
            DELETED.[Datecreated] AS [Datecreated_Before],
            DELETED.[Operator] AS [Operator_Before],
            DELETED.[LastUpdatedTime] AS [LastUpdatedTime_Before],
            INSERTED.[PolicyRef@] AS [PolicyRef@_After],
            INSERTED.[Poltype]AS [Poltype_After],
            INSERTED.[Trantype]AS [Trantype@_After],
            INSERTED.[Ledger_dt] AS [Ledger_dt_After],
            INSERTED.[Remarks_4] AS [Remarks_4_After],
            INSERTED.[#Exec] AS [#Exec_After],
            INSERTED.[Datecreated] AS [Datecreated_After],
            INSERTED.[Operator] AS [Operator_After],
            INSERTED.[LastUpdatedTime] AS [LastUpdatedTime_After],
            0, @Now,
            DELETED.Orig_debt AS [Orig_debt_Before],
            DELETED.Comm_amt AS [Comm_amt_Before],
            INSERTED.Orig_debt AS [Orig_debt_After],
            INSERTED.Comm_amt AS [Comm_amt_After]
        FROM 
            DELETED 
        INNER JOIN 
            INSERTED ON DELETED.Branch@ = INSERTED.Branch@ AND DELETED.Key@ = INSERTED.Key@
        WHERE 
            (INSERTED.PolicyRef@ <> DELETED.PolicyRef@
            OR INSERTED.Poltype <> DELETED.Poltype
            OR INSERTED.Trantype <> DELETED.Trantype
            OR INSERTED.Ledger_dt  <> DELETED.Ledger_dt
            OR INSERTED.Ledger_dt is null and DELETED.Ledger_dt is not null
            OR INSERTED.Ledger_dt is not null and DELETED.Ledger_dt is null
            OR ISNULL(INSERTED.Remarks_4, ''NULL'') <> isnull(DELETED.Remarks_4, ''NULL'')
            OR ISNULL(INSERTED.#Exec, ''NULL'') <> isnull(DELETED.#Exec, ''NULL'')
            OR ISNULL(INSERTED.Operator, ''NULL'') <> isnull(DELETED.Operator, ''NULL'')
            OR INSERTED.Operator <> DELETED.Operator
            OR INSERTED.Orig_debt <> DELETED.Orig_debt
            OR INSERTED.Comm_amt <> DELETED.Comm_amt)
            AND (INSERTED.Trantype <> ''Journal'')
END
GO

CREATE TRIGGER [dbo].[Tr_icp_brcledger_Delete] 
ON  [dbo].[icp_brcledger] 
AFTER DELETE
AS 
BEGIN
    INSERT INTO T_FIG_Trigger_Ledger_Delete (Branch@, Key@,PolicyRef@, TranType, PolType)
        SELECT 
            Branch@, Key@,PolicyRef@, Trantype, Poltype
        FROM 
            DELETED
        WHERE 
            (DELETED.Trantype <> ''Journal'')
END
GO