使用触发器在 SQL 服务器中插入多行
Insert Multiple Rows in SQL SERVER using a Trigger
我需要使用触发器将多行插入另一个 table,但它只插入最后一条记录
我检查了 Whosebug 中的其他一些帖子,但没有找到答案,
这是我的触发器
IF(@TNAEventID IN(1,2,3))
BEGIN
INSERT INTO [Biostar].Cen.WentOutLog (AutoID, nUserID, nOutDateTime,nOutTNAEvent ,nReaderID) values (@AutoID,@UseID, @DateTime,@TNAEventID, @ReaderID )
END
else IF(@TNAEventID=0)
BEGIN
UPDATE Cen.WentOutLog Set nINDateTime =@DateTime,nInTNAEvent = @TNAEventID Where AutoID = (Select top (1) AutoID from Cen.WentOutLog where nINDateTime is null AND nOutDateTime<@DateTime AND nUserID=@UseID order by nOutDateTime desc)
END
else
begin
....
end
提前致谢。
你可以试试下面的代码,插入代码很容易使用。
You might need to change the UPDATE statement as I do not know what is
your data:
INSERT INTO [Biostar].[Cen].[WentOutLog]
([AutoID], [nUserID], [nOutDateTime], [nOutTNAEvent], [nReaderID])
SELECT [AutoID], [nUserID], [nOutDateTime], [nOutTNAEvent], [nReaderID]
FROM INSERTED
WHERE TNAEventID IN (1, 2, 3)
UPDATE W
FROM [Cen].[WentOutLog]
SET W.[nINDateTime] = I.[DateTime],
W.[nInTNAEvent] = I.[TNAEventID]
INNER JOIN INSERTED I ON W.[AutoID] = I.[AutoID]
WHERE I.[TNAEventID] = 0
AND W.[nINDateTime] IS NULL
AND W.[nOutDateTime] < I.[DateTime]
AND W.[nUserID] = I.[UserID]
我需要使用触发器将多行插入另一个 table,但它只插入最后一条记录 我检查了 Whosebug 中的其他一些帖子,但没有找到答案,
这是我的触发器
IF(@TNAEventID IN(1,2,3))
BEGIN
INSERT INTO [Biostar].Cen.WentOutLog (AutoID, nUserID, nOutDateTime,nOutTNAEvent ,nReaderID) values (@AutoID,@UseID, @DateTime,@TNAEventID, @ReaderID )
END
else IF(@TNAEventID=0)
BEGIN
UPDATE Cen.WentOutLog Set nINDateTime =@DateTime,nInTNAEvent = @TNAEventID Where AutoID = (Select top (1) AutoID from Cen.WentOutLog where nINDateTime is null AND nOutDateTime<@DateTime AND nUserID=@UseID order by nOutDateTime desc)
END
else
begin
....
end
提前致谢。
你可以试试下面的代码,插入代码很容易使用。
You might need to change the UPDATE statement as I do not know what is your data:
INSERT INTO [Biostar].[Cen].[WentOutLog]
([AutoID], [nUserID], [nOutDateTime], [nOutTNAEvent], [nReaderID])
SELECT [AutoID], [nUserID], [nOutDateTime], [nOutTNAEvent], [nReaderID]
FROM INSERTED
WHERE TNAEventID IN (1, 2, 3)
UPDATE W
FROM [Cen].[WentOutLog]
SET W.[nINDateTime] = I.[DateTime],
W.[nInTNAEvent] = I.[TNAEventID]
INNER JOIN INSERTED I ON W.[AutoID] = I.[AutoID]
WHERE I.[TNAEventID] = 0
AND W.[nINDateTime] IS NULL
AND W.[nOutDateTime] < I.[DateTime]
AND W.[nUserID] = I.[UserID]