0

我想在 SQL Server 表上创建触发器。

表有 8 列。其中之一是email。IEpeter@msn.com

我想要的是:当将新行插入表中时,触发器应该更新我的ISPmsn.com

  CREATE TRIGGER TIGUPDATEISP ON SUBS
  FOR INSERT
  AS 
     UPDATE SUBS 
     SET ISP = SELECT RIGHT(EMAIL, LEN(EMAIL) - CHARINDEX('@', EMAIL) FROM INSERTED)

这是isp用最后一个值更新我的所有内容。但是,我想根据插入更新一个。

4

1 回答 1

3

如果 ISP 列允许 NULL 值,这将起作用

CREATE TRIGGER TIGUPDATEISP ON Subs
   FOR INSERT
AS 
BEGIN

    SET NOCOUNT ON;

    UPDATE SUBS 
        SET ISP = (SELECT RIGHT(EMAIL, LEN(EMAIL) - CHARINDEX('@', EMAIL)) 
        FROM INSERTED
        WHERE SUBS.ID = INSERTED.ID)
    FROM INSERTED
    WHERE SUBS.Id = INSERTED.ID

END

如果 ISP 列不允许 NULLS,这将起作用

CREATE TRIGGER TIGUPDATEISP ON Subs
   INSTEAD OF INSERT
AS 
BEGIN

    SET NOCOUNT ON;

    INSERT Subs (EMAIL, Isp)
    SELECT EMAIL, RIGHT(EMAIL, LEN(EMAIL) - CHARINDEX('@', EMAIL)) FROM INSERTED

END
于 2012-04-06T19:04:55.650 回答