我想在SQL Server表上创建一个触发器。
表有8列。其中一个是email
。即peter@msn.com
我想要的是:当一个新行插入表格时,触发器应该使用ISP
msn.com
列
CREATE TRIGGER TIGUPDATEISP ON SUBS
FOR INSERT
AS
UPDATE SUBS
SET ISP = SELECT RIGHT(EMAIL, LEN(EMAIL) - CHARINDEX('@', EMAIL) FROM INSERTED)
这是使用最后一个值更新我的所有isp
。但是,我想根据插入更新一个。
答案 0 :(得分: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