有两个表
表A
id,name,sourceid,sourcebillno表B
id,sourcebillno1,field1,field2在表A创建一个触发器,当更新或插入数据时,A表的sourceid的值是根据字段sourcebillno在表B查出的对应的ID。
即:表A的sourceid=select id from b where sourcebillno=sourcebillno1
这个触发器怎么写啊.
表A
id,name,sourceid,sourcebillno表B
id,sourcebillno1,field1,field2在表A创建一个触发器,当更新或插入数据时,A表的sourceid的值是根据字段sourcebillno在表B查出的对应的ID。
即:表A的sourceid=select id from b where sourcebillno=sourcebillno1
这个触发器怎么写啊.
解决方案 »
- 求MSSQL的面试题
- 在VB中我用access,下面的查询语句能正确执行,但升迁成SQL SERVER 2000后却报错。如何在SQL SERVER 2000也能实现下列语句的效果?select * from 书店 ORDER BY 类型='物理' , 类型='化学' ,
- 我的sqlserver2000怎么用windows身份验证连接不上啊?
- 都说到注册表里把Sqlserver清除干净安装就没问题,到底要清除哪些东西才叫清除干净?
- 如何用存储过程实现两表之间数据插入
- 为什么修改不了数据库中的某些数据。(在线等待)
- 安装完win2000,又安装了天网防火墙,再安装SQL server 2000,快完成时启动服务警告“无法定位程序输入点”
- SQL多表联查存储过程分页
- 聚合函数MAX的使用
- 如何把数据同时插入多个表,并且保证表之间关系正确?
- 在SQL2000中,如何一次附加超过十六个文件的库?
- 效率问题
create trigger name on 表A
after update,insert
as
update 表A
set sourceid=b.id
from inserted a left join 表B b
on a.sourcebillno=b.sourcebillno1
for insert
as
begin
update 表A
set sourceid=b.id
from inserted a inner join 表B b
on a.sourcebillno=b.sourcebillno1
end
create table t1
(id int,name varchar(10),sourceid int, sourcebillno int)
insert into t1
select 1,'zhang','1','1'
create table t2
(id int,sourcebillno1 int,field1 int,field2 int)
insert into t2
select 3,1,2,3 union all
select 5,3,2,3 union all
select 6,4,2,3 union all
select 8,8,2,3 union all
select 10,5,2,3 union all
select 11,9,2,3 create trigger name on t1
after update,insert
as
update t1
set sourceid=b.id
from inserted a join t2 b
on a.sourcebillno=b.sourcebillno1
where t1.id=a.idupdate t1
set name='zhao'
where id=1select * from t1 --sourceid由1 变成3/*
id name sourceid sourcebillno
----------- ---------- ----------- ------------
1 zhao 3 1(所影响的行数为 1 行)
*/insert into t1
values(5,'zhang',12,9)select * from t1/*
id name sourceid sourcebillno
----------- ---------- ----------- ------------
1 zhao 3 1
5 zhang 11 9(所影响的行数为 2 行)*/
FOR INSERT, UPDATE
AS
update A set sourceid = b.id from a,b where a.sourcebillno=b.sourcebillno1
GO
create trigger t on A
for insert,update
as
update A
set sourceid=select id from b where sourcebillno=sourcebillno1