从另一个 table 删除触发器和获取字段
Delete trigger and getting field from another table
我在 SQL 数据库上有这个删除触发器。该记录当前删除并写入审计 table。我被要求在此历史记录 table 中包含另一个 table 的字段,该字段与基于 SurveyID 删除的记录相关。我以为我可以做类似
的事情
select @Status = Status from table where Survey = deleted.Survey
但这是不正确的语法。
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
Declare @SurveyId int
Declare @StudentUIC varchar(10)
Declare @Status varchar(10)
select @SurveyId = deleted.SurveyID,
@StudentUIC = deleted.StudentUIC
from deleted
select @Status = Status from tbly when SurveyID = deleted.SurveyID
insert into fupSurveyAudit
values(@SurveyId,@StudentUIC,@Status)
End
每个语句(删除、插入、更新)触发一次触发器,而不是语句中的每一行。
您不能在此处使用变量,因为当从 table 中删除多行时,只有一行将插入审计 table 中,因为变量只能保存一个值。
您只需要将已删除的 table 简单地插入到审计 table 中,就像这样....
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
insert into fupSurveyAudit(SurveyId, StudentUIC,[Status])
select d.SurveyID
,d.StudentUIC
,y.[Status]
from deleted d
INNER JOIN tbly y ON y.SurveyID = deleted.SurveyID
End
试试这个
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
insert into fupSurveyAudit -- Better listed the column list here
select
d.SurveyID, d.StudentUIC, y.Status
from
deleted d JOIN tbly y ON d.SurveyID = y.SurveyID
End
哎呀。我想你想要这个 insert
在你的触发器中(没有别的):
insert into fupSurveyAudit(SurveyId, StudentUIC, status)
select d.SurveyId, d.StudentUIC, y.status
from deleted d left join
tbly y
on d.SurveyId = y.SurveyId;
备注:
deleted
可能包含不止一行,因此假设它只有一行可能会导致 运行 次错误或不正确的结果。
- 如果状态没有匹配行,则需要
left join
。
- 您应该始终在
insert
中包含这些列
- 您的存档 table 应该有额外的列,例如标识列和插入日期,它们是自动设置的(因此不是插入的明确部分)。
我在 SQL 数据库上有这个删除触发器。该记录当前删除并写入审计 table。我被要求在此历史记录 table 中包含另一个 table 的字段,该字段与基于 SurveyID 删除的记录相关。我以为我可以做类似
的事情select @Status = Status from table where Survey = deleted.Survey
但这是不正确的语法。
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
Declare @SurveyId int
Declare @StudentUIC varchar(10)
Declare @Status varchar(10)
select @SurveyId = deleted.SurveyID,
@StudentUIC = deleted.StudentUIC
from deleted
select @Status = Status from tbly when SurveyID = deleted.SurveyID
insert into fupSurveyAudit
values(@SurveyId,@StudentUIC,@Status)
End
每个语句(删除、插入、更新)触发一次触发器,而不是语句中的每一行。
您不能在此处使用变量,因为当从 table 中删除多行时,只有一行将插入审计 table 中,因为变量只能保存一个值。
您只需要将已删除的 table 简单地插入到审计 table 中,就像这样....
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
insert into fupSurveyAudit(SurveyId, StudentUIC,[Status])
select d.SurveyID
,d.StudentUIC
,y.[Status]
from deleted d
INNER JOIN tbly y ON y.SurveyID = deleted.SurveyID
End
试试这个
ALTER trigger [dbo].[table_Selfdelete]
on [dbo].[table]
after delete
as
Begin
Set nocount on;
insert into fupSurveyAudit -- Better listed the column list here
select
d.SurveyID, d.StudentUIC, y.Status
from
deleted d JOIN tbly y ON d.SurveyID = y.SurveyID
End
哎呀。我想你想要这个 insert
在你的触发器中(没有别的):
insert into fupSurveyAudit(SurveyId, StudentUIC, status)
select d.SurveyId, d.StudentUIC, y.status
from deleted d left join
tbly y
on d.SurveyId = y.SurveyId;
备注:
deleted
可能包含不止一行,因此假设它只有一行可能会导致 运行 次错误或不正确的结果。- 如果状态没有匹配行,则需要
left join
。 - 您应该始终在
insert
中包含这些列
- 您的存档 table 应该有额外的列,例如标识列和插入日期,它们是自动设置的(因此不是插入的明确部分)。