简体   繁体   English

触发器,用于修改差异表上的多行,然后在SQL Server 2005中对其进行调用

[英]Trigger that modifies multiple rows on diffrent table then it was invoked on in SQL Server 2005

I have tried to perform update on table which was trigger by update on other table and I got error message: 我试图在被其他表更新触发的表上执行更新,但出现错误消息:

The row value(s) updated or deleted either do not make the row unique or they alter multiple rows. 更新或删除的行值不会使该行唯一,或者会更改多行。

For example I have this tables: 例如,我有此表:

table_1
===========
int id   (primary_key,identity)
nchar(10)  state_name

table_2
===========
int number

And after updating table_2 I want to change all values in column 'state_name' to 'false' 在更新table_2之后,我想将“ state_name”列中的所有值更改为“ false”

create trigger tr on table_2
after update
as
update table_1 set state_name = 'false'

And when I try to update table_2 I receive error message. 当我尝试更新table_2时,收到错误消息。 Is there a way to walk around this limitation? 有没有办法解决这个限制?

在Table_2中添加一个主键约束(例如auto inc no),就可以了。

create table table_1(id int identity(1,1) primary key, state_name char(10)) 创建表table_1(id int identity(1,1)主键,state_name char(10))

create table table_2 ( number int) go 创建表table_2(number int)去

create trigger tr on table_2 after update as update table_1 set state_name = 'false' go 更新后在table_2上创建触发器tr,因为update table_1设置了state_name ='false'go

insert table_1 select 'true' insert table_2 select 1 插入表_1选择“ true”插入表_2选择1

go

update table_2 set number = 2 更新表_2设置数量= 2

select * from table_1 从表_1中选择*

select * from table_2 从表_2中选择*

Which version do you use? 您使用哪个版本? It worked out nicely in SQL 2K8 & SQL 2K5. 在SQL 2K8和SQL 2K5中效果很好。 Check your code again. 再次检查您的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM