我正在尝试执行以下操作,但努力使语法正确以将插入的值传递给我的存储过程。
CREATE TRIGGER tInsertCall
ON cdr2
FOR INSERT
AS
BEGIN
EXEC Track.dbo.InsertCall inserted.calldate, inserted.clid,
inserted.src, inserted.dst, inserted.dcontext, inserted.channel,
inserted.dstchannel, inserted.lastapp, inserted.lastdata,
inserted.duration, inserted.billsec, inserted.disposition,
inserted.uniqueid, inserted.amaflags, inserted.recordingpath,
inserted.accountcode, inserted.userfield
END
GO
由于
答案 0 :(得分:1)
你可以创建这样的东西:
CREATE TRIGGER dbo.tInsertCall ON dbo.cdr2
FOR INSERT
AS
BEGIN
DECLARE @CallDate DATETIME;
DECLARE @clid INT;
. --Add the other variables here
.
.
DECLARE @userfield SOMEDATATYPE;
SELECT TOP(1) @CallDate = INSERTED.calldate
, @clid = INSERTED.clid
... --Add the other variables here
, @userfield = INSERTED.userfield
FROM INSERTED;
EXEC Track.dbo.InsertCall @CallDate, @clid, ... /*other variables here*/, @userfield;
END
GO
此触发器仅处理INSERTED
虚拟表中的第1行,因为它在TOP(1)
语句中有SELECT
。
更好(可论证)这样做的方法是使用触发器迭代INSERTED
中的所有行。类似的东西:
CREATE TRIGGER dbo.tInsertCall ON dbo.cdr2
FOR INSERT
AS
BEGIN
DECLARE @CallDate DATETIME;
DECLARE @clid INT;
. -- add the other variable declarations here
.
.
DECLARE @userfield SOMEDATATYPE;
-- use FAST_FORWARD and LOCAL to minimize locking etc
DECLARE cur CURSOR FAST_FORWARD LOCAL FOR
SELECT *
FROM INSERTED;
OPEN cur;
--Get the first row from the INSERTED table
FETCH NEXT FROM cur
INTO @CallDate, @clid, ... /* other vars here */, @userfield;
WHILE @@FETCH_STATUS=0
BEGIN
-- run the stored proc
EXEC Track.dbo.InsertCall @CallDate, @clid, .../* other vars here */, @userfield;
-- get the next row from INSERTED
FETCH NEXT FROM cur
INTO @CallDate, @clid, .../* other vars here */, @userfield;
END
CLOSE cur;
DEALLOCATE cur;
END
GO