1

我有这个触发器

CREATE TRIGGER TriggerPickup
    ON dbo.Pickup
    AFTER INSERT 
    AS BEGIN
INSERT INTO dbo.Pickup( PickupProxy,PickupHispanic,EthnCode,CategCode,AgencyID)
         SELECT
              Clients.ClientProxy, Clients.Hispanic, Clients.EthnCode, Clients.CategCode,Clients.AgencyID
         FROM
             dbo.Clients,Pickup
             where   Pickup.ClientID =Clients.ClientID
    END 

但是,如果我提交这样的插入语句。它应该从客户端表中提取数据并插入到我插入 ID 和日期的同一行中,但是它会创建新记录并将数据放入其中。

INSERT INTO Pickup (ClientID, PickupDate)
VALUES (13144, '2010-11-20');

它没有从客户表中复制任何数据它只是插入空行 select * from pick 是什么样子的 任何想法发生了什么以及我在这个查询中的错误在哪里

4

1 回答 1

1

您需要将INSTEAD OF INSERT触发器或 AFTER 触发器与 UPDATE 语句一起使用

CREATE TRIGGER TriggerPickup ON dbo.Pickup
INSTEAD OF INSERT
AS BEGIN
  INSERT dbo.Pickup( PickupDate,
                    PickupProxy, PickupHispanic, EthnCode, CategCode, AgencyID)
  SELECT i.PickupDate, 
         c.ClientProxy, c.Hispanic, c.EthnCode, c.CategCode, c.AgencyID
  FROM dbo.Clients c JOIN inserted i ON i.ClientID = c.ClientID
END    

触发后

CREATE TRIGGER TriggerPickup ON dbo.Pickup
FOR INSERT
AS BEGIN
  UPDATE p
  SET p.PickupProxy = c.ClientProxy,
      p.PickupHispanic = c.PickupHispanic,
      p.EthnCode = c.EthnCode,
      p.CategCode = c.CategCode,
      p.AgencyID = c.AgencyID
  FROM inserted i JOIN dbo.Clients c ON i.ClientID = c.ClientID
                  JOIN dbo.Pickup p ON i.PickupID = p.PickupID
END   
于 2013-02-20T19:42:59.600 回答