SQL Server触发更新在不同DB中的表之间插入

时间:2015-07-14 04:37:34

标签: sql-server triggers

我在同一台服务器上有三个SQL Server数据库。之前,我必须使用查询在所有三个数据库上手动更新/插入。现在我想用触发器完成它。

我创建了一个触发器,但是在更新时,另一个表上的所有行都使用相同的数据进行了更新。

以下是更新不同数据库上其他表的触发器。

USE [QMS]
GO
/****** Object:  Trigger [dbo].[afterUserUpdate]    Script Date: 07/14/2015 12:19:39 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[afterUserUpdate] ON [dbo].[USERTBL] AFTER Update
AS
BEGIN 
  IF @@rowcount = 0 RETURN;
    Update QMSFL.[dbo].UserTBL SET USERID=b.USERID, USER_NAME=b.USER_NAME, USER_LEVEL=b.USER_LEVEL,
    PASSWORD=b.PASSWORD, USER_GRP=b.USER_GRP, STATUS=b.STATUS, MODIFY_BY=b.MODIFY_BY,MODIFY_DATE=b.MODIFY_DATE,LOGON_IND=b.LOGON_IND,
    SUPERVISOR_ID=b.SUPERVISOR_ID,STARTDATE=b.STARTDATE,RESIGNDATE=b.RESIGNDATE,COMPANY=b.COMPANY,PABXID=b.PABXID,branch=b.branch
    FROM QMSFL.[dbo].UserTBL a
    INNER Join inserted b ON a.UserID=b.UserID
    WHERE a.userid IN (SELECT userid FROM inserted) 

    Update QMSUC.[dbo].UserTBL SET USERID=b.USERID, USER_NAME=b.USER_NAME, USER_LEVEL=b.USER_LEVEL,
    PASSWORD=b.PASSWORD, USER_GRP=b.USER_GRP, STATUS=b.STATUS, MODIFY_BY=b.MODIFY_BY,MODIFY_DATE=b.MODIFY_DATE,LOGON_IND=b.LOGON_IND,
    SUPERVISOR_ID=b.SUPERVISOR_ID,STARTDATE=b.STARTDATE,RESIGNDATE=b.RESIGNDATE,COMPANY=b.COMPANY,PABXID=b.PABXID,branch=b.branch
    FROM QMSFL.[dbo].UserTBL a
    INNER Join inserted b ON a.UserID=b.UserID
    WHERE a.userid IN (SELECT userid FROM inserted) 
END

及以下是插入后触发器。尚未测试,导致我担心更新触发器会发生坏事。寻求你的建议这是好的。

USE [QMS]
GO
/****** Object:  Trigger [dbo].[afterUserInsert]    Script Date: 07/14/2015 12:31:59 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[afterUserInsert] ON [dbo].[USERTBL] AFTER INSERT
AS
BEGIN 
  IF @@rowcount = 0 RETURN;
    Insert Into QMSFL.[dbo].UserTBL (USERID,USER_NAME,USER_LEVEL,PASSWORD,USER_GRP,STATUS,MODIFY_BY,
    MODIFY_DATE,LOGON_IND,SUPERVISOR_ID,STARTDATE,RESIGNDATE,COMPANY,PABXID,branch)
    Select USERID,USER_NAME,USER_LEVEL,
    PASSWORD,USER_GRP,STATUS,MODIFY_BY,MODIFY_DATE,LOGON_IND,SUPERVISOR_ID,STARTDATE,RESIGNDATE,
    COMPANY,PABXID,branch From inserted
    Insert Into QMSUC.[dbo].UserTBL (USERID,USER_NAME,USER_LEVEL,PASSWORD,USER_GRP,STATUS,MODIFY_BY,
    MODIFY_DATE,LOGON_IND,SUPERVISOR_ID,STARTDATE,RESIGNDATE,COMPANY,PABXID,branch)
    Select USERID,USER_NAME,USER_LEVEL,PASSWORD,USER_GRP,STATUS,MODIFY_BY,MODIFY_DATE,LOGON_IND,
    SUPERVISOR_ID,STARTDATE,RESIGNDATE,COMPANY,PABXID,branch From inserted
END;

最后,是否可以在一次触发器中更新/插入后进行这些操作。并尽可能提供更新的工作触发器。

...谢谢=)

更新:简而言之,如何在一个触发器中更新多个表。

1 个答案:

答案 0 :(得分:0)

这将是我经过大量搜索和错误后完成的最终代码。按预期工作。可能对其他人有所帮助。

USE [QMS]
GO
/****** Object:  Trigger [dbo].[afterInsertUpdate]    Script Date: 07/24/2015 12:00:33 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[afterInsertUpdate] ON [dbo].[USERTBL] AFTER Insert,Update
AS 
SET NOCOUNT ON;
--BEGIN

    --add updated/inserted data into temp table
    Insert Into QMS.[dbo].UserTBLTemp (USERID,USER_NAME,USER_LEVEL,PASSWORD,USER_GRP,STATUS,MODIFY_BY,MODIFY_DATE,
    LOGON_IND,SUPERVISOR_ID,STARTDATE,RESIGNDATE,COMPANY,PABXID,branch)
    Select a.USERID,a.USER_NAME,a.USER_LEVEL,a.PASSWORD,a.USER_GRP,a.STATUS,a.MODIFY_BY,a.MODIFY_DATE,a.LOGON_IND,
    a.SUPERVISOR_ID,a.STARTDATE,a.RESIGNDATE,a.COMPANY,a.PABXID,a.branch
    From inserted a
    Left Outer Join QMS.[dbo].UserTBLTemp b
    ON a.UserID = b.UserID
    Where b.UserID IS NULL

    --remove unneeded character
    UPDATE UserTBLTemp SET USER_NAME=replace(USER_NAME,'''',' '),UserID=replace(replace(UserID,'''',''),' ','') WHERE UserID IN (SELECT UserID FROM UserTBLTemp) 

    --update data from temp table if exist in dest table
    Update QMSFL.[dbo].UserTBL SET USERID=d.USERID, USER_NAME=d.USER_NAME, USER_LEVEL=d.USER_LEVEL,
    PASSWORD=d.PASSWORD, USER_GRP=d.USER_GRP, STATUS=d.STATUS, MODIFY_BY=d.MODIFY_BY,MODIFY_DATE=d.MODIFY_DATE,
    LOGON_IND=d.LOGON_IND,SUPERVISOR_ID=d.SUPERVISOR_ID,STARTDATE=d.STARTDATE,RESIGNDATE=d.RESIGNDATE,
    COMPANY=d.COMPANY,PABXID=d.USERID,branch=d.branch
    FROM QMSFL.[dbo].UserTBL c
    INNER Join UserTBLTemp d
    ON c.UserID=d.UserID

    Update QMSUC.[dbo].UserTBL SET USERID=f.USERID, USER_NAME=f.USER_NAME, USER_LEVEL=f.USER_LEVEL,
    PASSWORD=f.PASSWORD, USER_GRP=f.USER_GRP, STATUS=f.STATUS, MODIFY_BY=f.MODIFY_BY,MODIFY_DATE=f.MODIFY_DATE,
    LOGON_IND=f.LOGON_IND,SUPERVISOR_ID=f.SUPERVISOR_ID,STARTDATE=f.STARTDATE,RESIGNDATE=f.RESIGNDATE,
    COMPANY=f.COMPANY,PABXID=f.USERID,branch=f.branch
    FROM QMSUC.[dbo].UserTBL e
    INNER Join UserTBLTemp f
    ON e.UserID=f.UserID

    --insert data from temp table if not exist in dest table
    Insert Into QMSFL.[dbo].UserTBL (USERID,USER_NAME,USER_LEVEL,PASSWORD,USER_GRP,STATUS,MODIFY_BY,MODIFY_DATE,
    LOGON_IND,SUPERVISOR_ID,STARTDATE,RESIGNDATE,COMPANY,PABXID,branch)
    Select g.USERID,g.USER_NAME,g.USER_LEVEL,g.PASSWORD,g.USER_GRP,g.STATUS,g.MODIFY_BY,g.MODIFY_DATE,g.LOGON_IND,
    g.SUPERVISOR_ID,g.STARTDATE,g.RESIGNDATE,g.COMPANY,g.USERID,g.branch
    From QMS.[dbo].UserTBLTemp g
    Left Outer Join QMSFL.[dbo].UserTBL h
    ON g.UserID = h.UserID
    Where h.Userid IS NULL

    Insert Into QMSUC.[dbo].UserTBL (USERID,USER_NAME,USER_LEVEL,PASSWORD,USER_GRP,STATUS,MODIFY_BY,MODIFY_DATE,
    LOGON_IND,SUPERVISOR_ID,STARTDATE,RESIGNDATE,COMPANY,PABXID,branch)
    Select i.USERID,i.USER_NAME,i.USER_LEVEL,i.PASSWORD,i.USER_GRP,i.STATUS,i.MODIFY_BY,i.MODIFY_DATE,i.LOGON_IND,
    i.SUPERVISOR_ID,i.STARTDATE,i.RESIGNDATE,i.COMPANY,i.USERID,i.branch
    From QMS.[dbo].UserTBLTemp i
    Left Outer Join QMSUC.[dbo].UserTBL j
    ON i.UserID = j.UserID
    Where j.Userid IS NULL

    --empty data in temp table
    Delete From QMS.[dbo].UserTBLTemp

--END