TransactSQL触发器根据条件AFTER UPDATE更新多个列

时间:2014-10-22 00:31:07

标签: sql sql-server triggers

我手边的问题

包含5列(TAB1)的表id1, colA, colB, colC, id2。 id1与id2匹配的行称为权威行。所以我们可能有多个行具有相同的id1但只有一行具有id1 = id2。

现在,如果仅针对“权威”行对TAB1进行更新或插入,则使用该数据更新其他非权威行。如果有可能,如果有人可以,请提供触发器的示例代码...感谢您的帮助...问候

示例数据

row 1(id1, cola, colb, colc, id2): 123, org1, mgr1, dept1, 999)
row 2(id1, cola, colb, colc, id2): 456, org2, mgr1, dept1, 999)
row 3(id1, cola, colb, colc, id2): 999, org3, mgr1, dept1, 999)
row 4(id1, cola, colb, colc, id2): 123, org4, mgr1, dept1, 999)

因此,在此示例数据中,我们看到row3是权威行(id1 = id2)。所以我喜欢写一个触发器,如果​​这行有任何更新(即row3,然后用更新的数据更新所有其他行(1,2和4),其中id2 = 999

2 个答案:

答案 0 :(得分:0)

试试这个

CREATE TABLE temp 
  (id1  INT, cola VARCHAR(50), colb VARCHAR(50), colc VARCHAR(50), id2  INT) 

insert into temp (id1, cola, colb, colc, id2) values
                (124,' org1', 'mgr1', 'dept1', 333) ,
                ( 456, 'org2', 'mgr1', 'dept1', 999),
                 (999, 'org3', 'mgr1', 'dept1', 999) ,
                  (123, 'org4',' mgr1', 'dept1', 999)

CREATE TABLE inserted1 
  (id1  INT, cola VARCHAR(50), colb VARCHAR(50), colc VARCHAR(50), id2  INT)

insert into inserted1 (id1, cola, colb, colc, id2) values
                      (999, 'org3', 'mgr1', 'dept1', 999)  

UPDATE temp 
SET    id1 = c.id1, 
       cola = c.cola, 
       colb = c.colb, 
       colc = c.colc, 
       id2 = C.id2 
FROM   (SELECT A.id1, 
               a.cola, 
               a.colb, 
               a.colc, 
               a.id2 
        FROM   inserted1 A 
               JOIN (SELECT * 
                     FROM   temp 
                     WHERE  id1 = id2) b 
                 ON a.id1 = b.id1 
                    AND a.id2 = b.id2) C 
WHERE  temp.id2 = c.id2 

答案 1 :(得分:0)

这可能就是你想要的。您的要求有点模糊,但至少这是一个开始:

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[TAB1]') AND type in (N'U'))
DROP TABLE [dbo].[TAB1]
GO

SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[TAB1](
    [id1] [int] NULL,
    [cola] [char](4) NULL,
    [colb] [char](4) NULL,
    [colc] [char](5) NULL,
    [id2] [int] NULL
) ON [PRIMARY]

SET ANSI_PADDING OFF
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO


-- This is the trigger code
CREATE TRIGGER dbo.Trigger_Name 
   ON  dbo.TAB1 
   AFTER UPDATE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

  UPDATE T1
    SET 
      T1.cola = CASE WHEN UPDATE(cola) THEN I.cola ELSE T1.cola END,
      T1.colb = CASE WHEN UPDATE(colb) THEN I.colb ELSE T1.colb END,
      T1.colc = CASE WHEN UPDATE(colc) THEN I.colc ELSE T1.colc END
  FROM
    TAB1 T1 
    JOIN INSERTED I ON T1.id2 = I.Id1 AND T1.Id1 <> T1.Id2
END
GO
-- End of trigger code

-- Insert test data
INSERT TAB1 (id1, cola, colb, colc, id2) VALUES ( 123, 'org1', 'mgr1', 'dept1', 999) 
INSERT TAB1 (id1, cola, colb, colc, id2) VALUES ( 456, 'org2', 'mgr2', 'dept2', 999) 
INSERT TAB1 (id1, cola, colb, colc, id2) VALUES ( 999, 'org3', 'mgr3', 'dept3', 999) 
INSERT TAB1 (id1, cola, colb, colc, id2) VALUES ( 123, 'org4', 'mgr4', 'dept4', 999)
INSERT TAB1 (id1, cola, colb, colc, id2) VALUES ( 444, 'org5', 'mgr5', 'dept5', 444)
INSERT TAB1 (id1, cola, colb, colc, id2) VALUES ( 445, 'org6', 'mgr6', 'dept6', 444)

-- Before updates
SELECT * FROM dbo.TAB1 AS t

-- Test updates
UPDATE TAB1 SET colb = 'HHHH' WHERE id1 = 999
UPDATE TAB1 SET cola = 'IIII' WHERE id1 = 445

-- After updates
SELECT * FROM dbo.TAB1 AS t

我已通过全面测试更新了我的答案