MS SQL Server沒有行級觸發器,是否正確?如果我需要從觸發器中插入一行,然後插入另一行,根據第一次插入的結果,光標會是最好的解決方案嗎?替代行級觸發器?
例如,是否有更好的方法來做到這一點:
CREATE TABLE t1 (foo int)
CREATE TABLE t2 (id int IDENTITY, foo int)
CREATE TABLE t3 (t2_id int)
GO
CREATE TRIGGER t1_insert_trg ON t1 FOR INSERT AS
DECLARE c CURSOR FOR
SELECT foo FROM inserted
DECLARE @foo int
OPEN c
FETCH NEXT FROM c INTO @foo
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO t2 (foo) VALUES (@foo)
INSERT INTO t3 (t2_id) VALUES (@@IDENTITY)
FETCH NEXT FROM c INTO @foo
END
CLOSE c
DEALLOCATE c
謝謝,我真的很喜歡你的解決方案與OUTPUT! – Alvis 2009-02-02 19:40:11