求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
支持
学习
最后不要用触发器了,用外键关联,手动添加.方便实用,维护也很好
相关问答:
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 ......
数据库某表,想将其中f1,f2两个字段的内容翻10倍,请教如何写法?
update tbl set f1= f1*10,f2= f2*10 where id=10451
这种写法会造成plsql卡死
不会吧,那个id=10451 的数据有多少啊~~~~
我估计sql是不 ......
比如 有一字段为:
Num
6111
6201
6520
65121
60087
46300
我想把6开头的全都替换成5开头
注意这是表里的数据,我想用SQL语句实现替换
SQL code:
update tb set
num = s ......
A机器(XP)(10.10.6.65)完整的计算机名是: DZD服务器
我装了一个个人版: SQLServer2000,创建了一个实例名为:MYINSTANCE
装了:dotNetFx35setup.exe
并打好了SP4补丁:SQL2000-KB884525-SP ......
数据类型:
Code char(6)
CreateTime datetime
Price float
数据如下:
Code CreateTime Price
031002 2008-10-17 15:00:15 ......