有谁知道为什么我在触发器内的连接查询出现空?但是,如果我将相同的查询放在存储过程中并在触发器之外运行存储过程,则运行正常。但是如果你有一个触发器用连接表执行这个存储过程,它就会变空。如果直接在sql management studio中运行存储过程,它会提取数据。如果你在没有连接的情况下运行触发器,只在一个表上进行简单的选择,它运行正常。
问题似乎是它在触发器中执行select并且具有Join。
ALTER TRIGGER dbo.Emergency on dbo.Incident
AFTER INSERT, UPDATE
AS
SET NOCOUNT ON
BEGIN
DECLARE @ID as int;
SELECT top 1 @ID=InsertedRow.Id FROM inserted InsertedRow;
BEGIN
Declare @g as varchar(255);
--this doesn't work with JOIN
SELECT @g=Link
FROM MyDataTable as Incident JOIN MyOtherInformationTable
as Info on Incident.Id=Info.Id
WHERE (Incident.Id=@ID);
/* this works without join
SELECT @g=Link
FROM MyDataTable as Incident
WHERE (Incident.Id=@ID);
*/
END;
END;
答案 0 :(得分:1)
以下触发器将返回事件中新插入或更新的记录以及MyOtherInformationTable中的匹配数据。
ALTER TRIGGER dbo.Emergency on dbo.Incident
AFTER INSERT, UPDATE
AS
BEGIN
SET NOCOUNT ON
select *
from
inserted
left join MyOtherInformationTable on inserted.ID = MyOtherInformationTable.ID
END;