将程序参数作为变量触发

时间:2014-12-07 11:34:29

标签: sql-server triggers

我有一张名为artist_log&的表格。它有以下结构

[action_type     char(6)]
[artist_id       int]
[mod_date        datetime    default current_timestamp]

创建了如下所述的程序:

create procedure update_artists 
    @m_artist_id int, 
    @m_place_of_birth varchar(60)
as
begin
    update artists
    set place_of_birth = @m_place_of_birth 
    where artist_id = @m_artist_id ;
end ;

并使用以下语句

执行它
execute update_artists 2019, 'Delhi' ;

我想创建一个触发器UPDT_LOG,它会将记录添加到表artist_log,其值为UPDATEDartist_id列的值应与参数viz相同。传递执行上述过程的@m_artist_id。有什么帮助吗?

1 个答案:

答案 0 :(得分:1)

你不必通过'触发器的参数。无论您更新哪些行,都可以使用触发器主体中的DELETED和INSERTED表进行访问。

create trigger updt_log on artists for update as
insert into artist_log (action_type, artist_id)
select 'UPDATE', artist_id
from inserted

这是触发器的最简单形式。如果artist_id可以通过设计进行更新,那么您也必须处理该案例。

相关问题