SQL 触发器的语法,用于在另一个数据库中插入数据,并在编辑字段后更新另一个数据库中的任何字段

Syntax for SQL Trigger to Insert Data in another DB and also to update any field in another db adfter a field is edited

这是场景 - 我将具体说明。我在 Sql 上有一个名为 [Fulcrum_Xfer] 的 "bridged" 数据库 我使用这个桥接是因为名为 [Fulcrum UAT] 的主数据库正在对某些字段使用 bigint 数据类型,从而显示在我的访问中所有字段中的前端“#Deleted data” - 在当前设计中无法更改此行为(bigint 必须保留)所以我有确切的 table 名称和字段名在我的 [Fulcrum_Xfer] 数据库中 - [Fulcrum_Xfer] 中的订单 table 中的 OrderNO 字段是 int 并且没有主键

在某些 "you let us down" 的威胁下,我需要在明天之前完成以下工作

最初将数据插入或更新的table称为Orders,在[Fulcrum_Xfer]数据库中,结构如下

OrderNo                int              Unchecked
OrderDate              smalldatetime    Unchecked
ApplicationTenantLinkId int             Unchecked
OrderStatus             int             Unchecked

从FulCrum_Xfer中的Orders中接收触发数据的table称为Orders,它在Database Fulcrum UAT中 结构是

OrderNo                bigint           Unchecked  Primarykey 
OrderDate              smalldatetime    Unchecked
ApplicationTenantLinkId Bigint          Unchecked
OrderStatus             int             Unchecked

我需要两个触发器语句,在我将新记录插入 [FulCrum_Xfer]

中的订单后,它们将在 [Fulcrum UAT] 中的订单中插入一条新记录

我需要一个触发器,当我更改 [Fulcrum_Xfer]

中的订单时,它会更新 [Fulcrum UAT] 中订单中的任何字段

除了 [FulCrum_XFer] 中的数据库触发器,我不知道触发器去哪里了,但我被模板语法吓坏了(我认为我不需要所有这些)而且我不知道如何为每个任务编写语法

我是一名非常有经验的 VB / VB 开发人员,我曾使用 ADO 在 SQL 上构建和调用存储过程,但从未在 SQL 上执行过此类任务SQL 服务器 - 请不要像对待笨蛋一样对待我 - 但这对我现在的工作非常重要。

嗯,当然我没有办法测试它,但我认为这就是您编写 INSERT 触发器的方式:

USE [Fulcrum_Xfer]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER dbo.trOrders_Insert 
   ON  dbo.Orders
   AFTER INSERT AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Just INSERT everything from the [inserted] pseudotable into
    --the target table
    INSERT INTO [Fulcrum UAT].dbo.Orders 
              (OrderNo, OrderDate, ApplicationTenantLinkId, OrderStatus)
        SELECT OrderNo, OrderDate, ApplicationTenantLinkId, OrderStatus
        FROM   inserted;

END
GO

将其复制并粘贴到 Management Studio 中的查询 window 中并执行。

下面是我执行更新触发器的方法。同样,未经测试...

USE [Fulcrum_Xfer]
GO
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
GO

CREATE TRIGGER dbo.trOrders_Update 
   ON  dbo.Orders
   AFTER UPDATE AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Just UPDATE everything matching [inserted] pseudotable 
    --into the target table
    --(NOTE: This assumes that UPDATES will never change the PK/OrderNo)
    UPDATE [Fulcrum UAT].dbo.Orders 
        SET OrderDate   = ins.OrderDate, 
            ApplicationTenantLinkId
                        = ins.ApplicationTenantLinkId, 
            OrderStatus = ins.OrderStatus
        FROM [Fulcrum UAT].dbo.Orders As tar
        JOIN inserted as ins ON tar.OrderNo = ins.OrderNo;
    --(also, performance may not be great as the JOIN columns are 
    --  different datatypes)
END
GO

试试这个

USE BioStar;// Which data base you want to create trigger
GO 
CREATE TRIGGER trgAfterInsertnew ON [dbo].[TB_EVENT_LOG] 
FOR INSERT
AS
    declare @nDateTime int;
    declare @nReaderIdn int;
    declare @nEventIdn int;
    declare @nUserID int;
    declare @nIsLog smallint;
    declare @nTNAEvent smallint;
    declare @nIsUseTA smallint;
    declare @nType smallint;


    select @nDateTime=i.nDateTime from inserted i;  
    select @nDateTime=i.nReaderIdn from inserted i; 
    select @nEventIdn=i.nEventIdn from inserted i;  
    select @nUserID=i.nUserID from inserted i;

    select @nIsLog=i.nIsLog from inserted i;    
    select @nTNAEvent=i.nTNAEvent from inserted i;  
    select @nIsUseTA=i.nIsUseTA from inserted i;    
    select @nType=i.nType from inserted i;
    insert into [HRM].dbo.Device_Data
           (nDateTime,nReaderIdn,nEventIdn,nUserID,nIsLog,nTNAEvent,nIsUseTA,nType) 
    values(@nDateTime,@nDateTime,@nEventIdn,@nUserID,@nIsLog,@nTNAEvent,@nIsUseTA,@nType);


    --set @audit_action='Inserted Record -- After Insert Trigger.';
        PRINT 'AFTER DELETE TRIGGER fired.'
GO

给定:

  • 具有 table t1 和字段(username1password1
  • 的数据库 db1
  • 具有 table t2 和字段(username2password2
  • 的数据库 db2

如果要在 db1 中发生更改(假设插入)时在 db2 中插入或更新 t2,则应将触发器添加到 t1 中的 [=11] =]:

CREATE TRIGGER `trigger_name` AFTER INSERT ON `t1` 
FOR EACH ROW INSERT INTO `db2`.`t2`( `username2`, `password2`)
VALUES ( new.username1, new.password1)

(我假设两个数据库都托管在同一台服务器上。)