TRIGGER显示两行受影响的T-SQL

时间:2013-05-21 09:22:39

标签: c# tsql

我想只获取一次数据。怎么做?例如,仅来自第一个操作

myTABLE1
(
NAME,
SURNAME,
USERNAME, 
USERDATE
)

对MyTABLE1的第一个动作

INSERT INTO MyTABLE1 
SELECT NAME, SURNAME FROM MyTABLE2

对MyTABLE1的第二个动作

CREATE TRIGGER example on MyTABLE1 AFTER INSERT
AS
UPDATE    MyTABLE1
SET       USERNAME = 'sysUser1212' and USERDATE = '2004-09-02 00:00:00.000'

我在MyTable3中受到两行影响,但我想只获得一次

CREATE TRIGGER example2 on MyTABLE1 FOR INSERT UPDATE DELETE
    AS
    INSERT INTO MyTABLE3
    SELECT NAME, SURNAME, USERNAME, USERDATE
    from inserted

1 个答案:

答案 0 :(得分:2)

创建为INSTEAD OF触发器。

在进行插入时,你可以用'sysUser1212'替换USERNAME,用'2004-09-02 00:00:00.000'替换USERDATE。

CREATE TRIGGER examplex on MyTABLE1 INSTEAD OF INSERT
AS
INSERT    MyTABLE1 (USERNAME, USERDATE, NAME, SURNAME)
SELECT
    'sysUser1212'
    , '2004-09-02 00:00:00.000'
    , NAME
    , SURNAME
FROM inserted   


-- The following disapear
/*
CREATE TRIGGER example on MyTABLE1 AFTER INSERT
AS
UPDATE    MyTABLE1
SET       USERNAME = 'sysUser1212' and USERDATE = '2004-09-02 00:00:00.000'
--*/