易截截图软件、单文件、免安装、纯绿色、仅160KB

求sql触发器语句

users表
name  companyId  companyName


company表
companyId  companyName
1          a公司
2          b公司

当添加user表时,跟据所填的users.comapnyId,用触发器产生users.companyName对应的company表的companyName值


select companyName from company where companyId =@companyId 

SQL code:
create trigger tri_users_insert
on users
instead of insert
as
begin
insert users
select i.name,i.companyId,c.companyName
from inserted i,company c
where i.companyId=c.companyId
end

或者不用instead of,插入后用触发器进行更新

SQL code:
create trigger tri_users_insert
on users
after insert
as
begin
update u
set u.companyName=c.companyName
from users u,inserted i,company c
where i.companyId=c.companyId and i.companyId=u.companyId
end


支持

学习

最后不要用触发器了,用外键关联,手动添加.方便实用,维护也很好


相关问答:

SQL语句问题

create trigger TR_Delete_pm_WorkCard
on pm_WorkCard
for delete
as
if @@rowcount=0 return
begin tran
  update pm_PlanEntry
  set FDispatchQty=t4.FDispatchQty - t1.FDispat ......

sql 查询-更新 阻塞。。。。?

遇到的现象是 同一个表的query会阻塞update

sql  query 和update 操作 query会阻塞update操作吗

怎么解决

是不是什么设置不对还是怎的


如果会的话,那同一个表的频繁query and ......

求助一句SQL语句

数据类型:
Code char(6)
CreateTime datetime
Price float

数据如下:

Code      CreateTime          Price
031002 2008-10-17 15:00:15 ......

求助一句SQL语句

数据类型:
Code char(6)
CreateTime datetime
Price float

数据如下:

Code      CreateTime          Price
031021 2008-10-17 15:00:1 ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号