触发器导致子查询返回的值超过1

时间:2014-04-21 12:34:12

标签: sql sql-server triggers

我没有多少使用触发器,但是当在update语句上执行触发器时,我似乎遇到了问题。例如,我有一个名为Project的表,我们允许软删除。我的意思是软删除是在这个表中我们有一个名为Deleted的位字段,如果它是真的意味着记录被删除,如果是假则意味着它是一个有效的记录。

现在这个表有很多关系(引用这个项目的表)。例如,项目可以有多个与之关联的程序。所以我有一个ProgramProject表,其中一个程序可以有多个项目(1到多个关系,其中1是程序,很多是项目)。所以当我删除一个项目时(从我上面提到的),触发器应该删除与之关联的所有项目...

但是,在某些情况下,当触发器执行时,它似乎返回错误:

System.Data.SqlClient.SqlException was caught HResult=-2146232060 Message=Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

我是否错误地执行了这些触发器?以下是触发器的示例:

ALTER TRIGGER [dbo].[trDeleteProject] ON [dbo].[Project]
FOR UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

    --check if the Deleted field was updated
    IF(UPDATE(Deleted))
        BEGIN
            DECLARE @WasDeleted bit
            DECLARE @ProjectID int

            --get the project that was just deleted
            SET @ProjectID = (SELECT ProjectID FROM Inserted)
            --get whether they want the record deleted (1) or not (0)
            SET @WasDeleted = (SELECT Deleted FROM Inserted)

            --did they want to delete it? (1)
            if (@WasDeleted=1)
                BEGIN
                --yes they delete the record so we remove instances...

                    --1.)Remove programs when a project is deleted
                    UPDATE ProgramProject SET Deleted=1 WHERE ProjectID = @ProjectID

                    --2.)Remove project contact roles when you delete a project
                    UPDATE ProjectContactRole SET Deleted=1 WHERE ProjectID = @ProjectID

                    --3.)Remove sub projects when you delete a project
                    --UPDATE SubProject SET Deleted=1 WHERE ProjectID = @ProjectID

                    --4.)Remove any transport system when you delete a project
                    UPDATE ProjectTransportSystem SET Deleted=1 WHERE ProjectID = @ProjectID

                    --5.)Remove any project mechanical architecture when you delete a project
                    UPDATE ProjectMechanicalArchitecture SET Deleted=1 WHERE ProjectID = @ProjectID

                    --6.)Remove any install personnel when you delete a project
                    UPDATE ProjectInstallPersonnel SET Deleted=1 WHERE ProjectID = @ProjectID
                     END
        END
END

我是不是因为它是一个触发器而做错了?我应该加入一些临时表而不是单个更新语句吗?

更新

我尝试使用M Ali的例子,但我遇到了同样的问题,子查询返回超过1行......

以下是我使用的代码:

ALTER TRIGGER [dbo].[trDeleteProject] ON [dbo].[Project]
FOR UPDATE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT * INTO #temp 
    FROM Inserted i
    WHERE i.deleted = 1

        --1.)Remove programs when a project is deleted
        UPDATE P 
        SET p.Deleted = 1 
        FROM #temp t INNER JOIN ProgramProject p ON P.ProjectID = t.ProjectID

        --2.)Remove project contact roles when you delete a project
        UPDATE Pr 
        SET pr.Deleted = 1 
        FROM #temp t INNER JOIN ProjectContactRole pr ON Pr.ProjectID = t.ProjectID

        --3.)Remove sub projects when you delete a project
        UPDATE SP 
        SET SP.Deleted = 1 
        FROM #temp t INNER JOIN SubProject sp ON SP.ProjectID = t.ProjectID

        --4.)Remove any transport system when you delete a project
        UPDATE PTS 
        SET PTS.Deleted = 1 
        FROM #temp t INNER JOIN ProjectTransportSystem PTS ON PTS.ProjectID = t.ProjectID

        --5.)Remove any project mechanical architecture when you delete a project
        UPDATE PMA 
        SET PMA.Deleted = 1 
        FROM #temp t INNER JOIN ProjectMechanicalArchitecture PMA ON PMA.ProjectID = t.ProjectID

        --6.)Remove any install personnel when you delete a project
        UPDATE PIP 
        SET PIP.Deleted = 1 
        FROM #temp t INNER JOIN ProjectInstallPersonnel PIP ON PIP.ProjectID = t.ProjectID

        --7.)Remove any install shifts when you delete a project
        UPDATE PIS
        SET PIS.Deleted = 1
        FROM #temp t INNER JOIN ProjectInstallShift PIS ON PIS.ProjectID = t.ProjectID

        --8.)Remove any install windows when you delete a project
        UPDATE PIW
        SET PIW.Deleted = 1
        FROM #temp t INNER JOIN ProjectInstallWindow PIW ON PIW.ProjectID = t.ProjectID

        --9.)Remove any project pallet type when you delete a project
        UPDATE PPT
        SET PPT.Deleted = 1
        FROM #temp t INNER JOIN ProjectPalletType PPT ON PPT.ProjectID = t.ProjectID

        --10.)Remove issues related to a project
        UPDATE I
        SET I.Deleted = 1
        FROM #temp t INNER JOIN Issue I ON I.ProjectID = t.ProjectID

        --11.)Remove measurement architecture for a project
        UPDATE PMAS
        SET PMAS.Deleted = 1
        FROM #temp t INNER JOIN ProjectMeasurementArchitectureSystem PMAS ON PMAS.ProjectID = t.ProjectID

        --12.)Remove controls architecture for a project
        UPDATE PCAS
        SET PCAS.Deleted = 1
        FROM #temp t INNER JOIN ProjectControlsArchitectureSystem PCAS ON PCAS.ProjectID = t.ProjectID                  
END

1 个答案:

答案 0 :(得分:0)

只需将逻辑删除的行放入临时表中,然后将该临时表与每个相关表连接起来,并更新其中的已删除字段。

您可以简单地使用已删除的表本身来连接要更新的每个表,但只将字段插入临时表,其中字段deleted = 1限制临时表中的数字或行。并加入一个相对较小的表将为您带来一些性能提升。请看下面:

ALTER TRIGGER [dbo].[trDeleteProject] ON [dbo].[Project]
FOR UPDATE
AS 
BEGIN
    SET NOCOUNT ON;
-- Get logically deleted rows into a temp table 

 SELECT * INTO #temp 
 FROM Inserted i
 WHERE i.deleted = 1

        --1.)Remove programs when a project is deleted
        UPDATE P 
        SET p.Deleted = 1 
        FROM #temp t INNER JOIN ProgramProject p ON P.ProjectID = t.ProjectID

        --2.)Remove project contact roles when you delete a project
        UPDATE Pr 
        SET pr.Deleted = 1 
        FROM #temp t INNER JOIN ProjectContactRole pr ON Pr.ProjectID = t.ProjectID

        --3.)Remove sub projects when you delete a project
        UPDATE SP 
        SET SP.Deleted = 1 
        FROM #temp t INNER JOIN SubProject sp ON SP.ProjectID = t.ProjectID

        --4.)Remove any transport system when you delete a project
        UPDATE PTS 
        SET PTS.Deleted = 1 
        FROM #temp t INNER JOIN ProjectTransportSystem PTS ON PTS.ProjectID = t.ProjectID

        --5.)Remove any project mechanical architecture when you delete a project
        UPDATE PMA 
        SET PMA.Deleted = 1 
        FROM #temp t INNER JOIN ProjectMechanicalArchitecture PMA ON PMA.ProjectID = t.ProjectID

        --6.)Remove any install personnel when you delete a project
        UPDATE PIP 
        SET PIP.Deleted = 1 
        FROM #temp t INNER JOIN ProjectInstallPersonnel PIP ON PIP.ProjectID = t.ProjectID

END

<强>更新

由于您已经提到过,您希望一次只使用一个projectID来更新表。要实现这一点,你需要再次创建一个while循环和一个临时表,并循环通过你的临时表来更新projectid projectid的每个表中的记录,我认为这对于一个相当简单的任务来说是一种过度杀伤力。见下文:

ALTER TRIGGER [dbo].[trDeleteProject] ON [dbo].[Project]
FOR UPDATE
AS 
BEGIN
    SET NOCOUNT ON;

--check if the Deleted field was updated
    DECLARE @ProjectID int

  SELECT * INTO #Temp 
  FROM Inserted 
  WHERE deleted = 1

    --did they want to delete it? (1)
    WHILE EXISTS (SELECT * FROM #Temp)
        BEGIN
              SELECT TOP 1 @ProjectID = ProjectID 
              FROM #Temp

            --1.)Remove programs when a project is deleted
            UPDATE ProgramProject SET Deleted=1 WHERE ProjectID = @ProjectID

            --2.)Remove project contact roles when you delete a project
            UPDATE ProjectContactRole SET Deleted=1 WHERE ProjectID = @ProjectID

            --3.)Remove sub projects when you delete a project
            --UPDATE SubProject SET Deleted=1 WHERE ProjectID = @ProjectID

            --4.)Remove any transport system when you delete a project
            UPDATE ProjectTransportSystem SET Deleted=1 WHERE ProjectID = @ProjectID

            --5.)Remove any project mechanical architecture when you delete a project
            UPDATE ProjectMechanicalArchitecture SET Deleted=1 WHERE ProjectID = @ProjectID

            --6.)Remove any install personnel when you delete a project
            UPDATE ProjectInstallPersonnel SET Deleted=1 WHERE ProjectID = @ProjectID

         DELETE FROM #Temp WHERE ProjectID = @ProjectID  

       END 
END