平台:mssql2000
条件:1、已知有两表,表A和表B
2、表A中有字段,A1,A2
3、表B中有字段,B1,B2
问题:当更新表A中的A2字段的同时,更新表B中所有B2字段中的B2=A2的所有字段为新的A2。
要求:要有该触发器要有容错功能,且在建立之前会判断是否有同名触发器存在,有的话则删除同名的触发器在建立触发器。
条件:1、已知有两表,表A和表B
2、表A中有字段,A1,A2
3、表B中有字段,B1,B2
问题:当更新表A中的A2字段的同时,更新表B中所有B2字段中的B2=A2的所有字段为新的A2。
要求:要有该触发器要有容错功能,且在建立之前会判断是否有同名触发器存在,有的话则删除同名的触发器在建立触发器。
解决方案 »
- SQLServer2008关于二进制的问题
- 求一个关于left join连接三张表的sql语句
- 三个表关联操作两个触发器,遇到问题,SOS!
- SQL SERVER 启动之后,不需要重新建立()数据库
- 如何使用ODBC执行SQL语句
- ★有关一个数据统计算平均数问题
- 请教 SQL 如何在已有数据库 字段上增加IDENTITY属性
- 使用OLEDB无法更新数据库
- 小弟问一个很菜的问题,我如何能够通过sql的日志文件会滚数据库的内容,谢谢各位大侠!!!
- ★☆★学过ORACLE远程连接的请看★☆★
- 在打开'数据转换服务'中的'元数据'时提示'在尝试访问数据库信息时出错。无法打开MSDB数据库'
- ORDER BY 无法有效排序
as
update b set b2 = a.a2 from b,a where b.b1 = a.a1
for update
as
update B
set B2=i.A2
from
inserted i join deleted d on i.A1=D.A1
join B on B2=d.A2
insert into a select 1,1
insert into a select 2,2
create table b(b1 int,b2 int)
insert into b select 1,1
insert into b select 2,1
insert into b select 3,2
insert into b select 4,2
insert into b select 5,2create trigger tr_a
on a
for update
as
begin
update a set b2=b.a2 from b a,deleted c,inserted b
where a.b2=c.a2 and c.a1=b.a1
endupdate a set a2=5 where a1=1select * from aselect * from b
--a
a1 a2
1 5
2 2
--b
b1 b2
1 5
2 5
3 2
4 2
5 2
inserted i join deleted d on i.A1=D.A1
这句什么意思?
这句什么意思?inserted --别名为i表
deleted --别名为d表
insert into a select 1,1
insert into a select 2,2create table b(b1 char(1),b2 int)
insert into b select 'a',1
insert into b select 'b',1
insert into b select 'c',2
insert into b select 'd',2
insert into b select 'e',2
go
if exists(select 1 from sysobjects where name ='ut_a_update' and type='tr')
drop trigger ut_a_update
go
create trigger ut_a_update on a for update
as
update b set b2=i.a2
from inserted i join deleted d on i.a1=d.A1
join b on b.b2=d.a2
go
--测试
select * from b
update a set a2=3 where a2=1
select * from bdrop table a
drop table b
go