create trigger tr_update on 表 for update as declare @主键 int if update(某列) --检查有没有被更新 begin select @主键=主键 from inserted where 某列=3 if @@rowcount=0 --如果=0,则是没有被更新为3 print '没有被更新为3' else begin select 被更新的主键=@主键 end end
正如邹老大所讲,多条记录同时执行时出现了问题,于是我把代码改成如下create trigger tr_update on 表 for update as declare @主键 int if update(某列) --检查有没有被更新 begin select @主键=主键 from inserted where 某列=3 if @@rowcount>1---如果总数大于1 begin DECLARE tables_cursor CURSOR FOR select 主码 from inserted where 某列=3 begin open tables_cursor FETCH NEXT FROM tables_cursor INTO 变量2 WHILE (@@FETCH_STATUS = 0) BEGIN -------执行相关代码 FETCH NEXT FROM tables_cursor INTO 变量2 END close tables_cursor end if @@rowcount=1------如果总数等于1 ----执行相关代码 end结果我的代码执行没有任何效果晕了。还是麻烦各位老大了
create trigger tr_update on 表 for update as declare @主键 int if update(某列) --检查有没有被更新 begin if exists(select 1 from inserted where 某列=3) begin DECLARE tables_cursor CURSOR FOR select 主码 from inserted where 某列=3 open tables_cursor FETCH NEXT FROM tables_cursor INTO 变量2 WHILE (@@FETCH_STATUS = 0) BEGIN -------执行相关代码 FETCH NEXT FROM tables_cursor INTO 变量2 END close tables_cursor deallocate tables_cursor end else begin ----执行相关代码 end end
COL_NAME
返回数据库列的名称,该列具有相应的表标识号和列标识号。语法
COL_NAME ( table_id , column_id )
for update
as
declare @主键 int
if update(某列) --检查有没有被更新
begin
select @主键=主键 from inserted where 某列=3
if @@rowcount=0 --如果=0,则是没有被更新为3
print '没有被更新为3'
else
begin
select 被更新的主键=@主键
end
end
for update
as
declare @主键 int
if update(某列) --检查有没有被更新
begin
select @主键=主键 from inserted where 某列=3 if @@rowcount>1---如果总数大于1
begin
DECLARE tables_cursor CURSOR FOR select 主码 from inserted where 某列=3
begin
open tables_cursor
FETCH NEXT FROM tables_cursor INTO 变量2
WHILE (@@FETCH_STATUS = 0)
BEGIN
-------执行相关代码
FETCH NEXT FROM tables_cursor INTO 变量2
END
close tables_cursor
end if @@rowcount=1------如果总数等于1
----执行相关代码
end结果我的代码执行没有任何效果晕了。还是麻烦各位老大了
for update
as
declare @主键 int
if update(某列) --检查有没有被更新
begin
if exists(select 1 from inserted where 某列=3)
begin
DECLARE tables_cursor CURSOR FOR
select 主码 from inserted where 某列=3 open tables_cursor
FETCH NEXT FROM tables_cursor INTO 变量2
WHILE (@@FETCH_STATUS = 0)
BEGIN
-------执行相关代码
FETCH NEXT FROM tables_cursor INTO 变量2
END
close tables_cursor
deallocate tables_cursor
end
else
begin
----执行相关代码 end
end