求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
支持
学习
最后不要用触发器了,用外键关联,手动添加.方便实用,维护也很好
相关问答:
INSERT INTO StudAchieve(studID,studName)
VALUES('2','abc'),('2','bcd'),('2','cde')
拼了半天也不对,及啊 。。。。
SQL c ......
sql语句:
select loan_plan_id,l_collect_plan.loan_id,l_collect_detail.collect_isfinished from l_collect_plan
left join l_collect_detail on l_collect_plan.return_times=l_collect_detail.collect_vo ......
比如 有一字段为:
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 ......