启用触发器时查询不起作用

时间:2016-07-28 17:45:29

标签: php sql sql-server triggers

我的表格中有一些触发器,因此我可以记录用户在我的网站上所做的任何更改。然后我发现在一些表中查询是静默的,因为我的PHP代码没有触发错误。我试过禁用我的触发器,然后他们工作了。我还注意到,当我在查询之后放置die时,他们使用启用了触发器。此外,当我在SQL Manager中运行查询时,它们也可以正常运行而不会出现任何错你知道为什么会这样吗?如何查看触发器是否失败?

我的触发器示例(一次只有一个插件):

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER TRIGGER [dbo].[WorkflowBase_insert]
ON [dbo].[WorkflowBase]
FOR INSERT
AS
BEGIN
    DECLARE
        @id INT,
        @result varchar(MAX),
        @user varchar(MAX),
        @LoopCounter INT = 1, 
        @MAX INT, 
        @Column NVARCHAR(100),
        @Type NVARCHAR(100),
        @Value NVARCHAR(100),
        @Sql NVARCHAR(MAX),
        @Tmp NVARCHAR(MAX)

    SET @result = '{'

    SELECT @user = system_user

    SELECT @MAX = MAX(ORDINAL_POSITION) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'WorkflowBase'

    WHILE(@LoopCounter <= @MAX)
    BEGIN
       SELECT @Column = COLUMN_NAME, @Type = DATA_TYPE
       FROM INFORMATION_SCHEMA.COLUMNS WHERE ORDINAL_POSITION = @LoopCounter and TABLE_NAME = 'WorkflowBase'

       Select * into #inserted from inserted

       SET @Sql = 'SELECT @Value = ' + @Column + ' FROM #inserted'

       exec sp_executesql @Sql, N'@Value varchar(MAX) out', @Value out

       DROP TABLE #inserted

       IF (@Value is not null or @Value != '') and  (@Type = 'int' or @Type = 'varchar' or @Type = 'nvarchar')
       BEGIN
            IF @result = '{'
            BEGIN
                SET @result = @result + ' "' + @Column + '":"' + @Value + '"'
            END
            ELSE
            BEGIN
                SET @result = @result + ', "' + @Column + '":"' + @Value + '"'
            END
       END
       ELSE IF (@Value is not null or @Value != '') and  (@Type = 'datetime2' or @Type = 'datetime' or @Type = 'date')
       BEGIN
            IF @result = '{'
            BEGIN
                SET @result = @result + ' "' + @Column + '":"' + CONVERT(VARCHAR(20),@Value,120) + '"'
            END
            ELSE
            BEGIN
                SET @result = @result + ', "' + @Column + '":"' + CONVERT(VARCHAR(20),@Value,120) + '"'
            END
       END
       SET @LoopCounter  = @LoopCounter  + 1        
    END

    SET @result = @result + '}'

    SELECT @id = inserted.ID FROM INSERTED

    INSERT INTO sys_logs (username,datahora,tabela,[object_id],[action],newvalue) values (@user,GETDATE(),'WorkflowBase',@id,'INSERT',@result)

END

查询失败的代码:

foreach ($z as $k => $v)
        {
            $_xuser = array();
            $_user = array();
            $ordem = str_pad($k, 3, "0", STR_PAD_LEFT);


            $performers = $v["Utilizador"];


            $_form = explode("|", $v["Definicoes"]["Operation"]);

            $Duration = $v["Definicoes"]["Duration"];
            $Advanced_Function = $v["Definicoes"]["Advanced_Function"];
            $Note = strip_tags(html_entity_decode($v["Definicoes"]["Note"]));
            $PaiGUID = "'" . $v["PaiGUID"] ."'";
            $RouteToGUID = "'" . $v["RouteToGUID"] . "'";

            if ($v["PaiGUID"] == "")
                $PaiGUID = "null";

            if ($v["RouteToGUID"] == "")
                $RouteToGUID = "null";

            $sql = "INSERT INTO WorkflowBase (
            [IDGuid]
      ,[IDWorkflowDefinition]
      ,[TaskType]
      ,[TaskName]
      ,[Ordem]
      ,[DescriptionText]
      ,[Duration]
      ,[IDOwners]
      ,[IDOperation]
      ,[IDParent]
      ,[NextTask]
      ,[ExternalFunction]
      ,[Condition]
      ,[ExplanatoryNote]
      ,[IDSubProcess]
            ) VALUES (
            '$v[GUID]'
            ,'$wf'
            ,'$v[Tipo]'
            ,'$v[Nome]'
            ,'$ordem'
            ,'$v[Descricao]'
            ,'$Duration'
            ,'$performers'
            ,'" . trim($_form[0]) . "'
            ,$PaiGUID
            ,$RouteToGUID
            ,'$Advanced_Function'
            ,'$v[Condicao]'
            ,'$Note'
      ,'$SubProcess'
            ) 
            ";


            ($res = cms_sqlsrv_query($sql)) || exit(cms_error());



        }

谢谢:)

0 个答案:

没有答案