易截截图软件、单文件、免安装、纯绿色、仅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语句将数据库表某些字段的内容翻10倍

数据库某表,想将其中f1,f2两个字段的内容翻10倍,请教如何写法?
update tbl set f1= f1*10,f2= f2*10 where id=10451
这种写法会造成plsql卡死
不会吧,那个id=10451 的数据有多少啊~~~~

我估计sql是不 ......

sql字符串替换(有点挑战···)

比如 有一字段为:
Num
6111
6201
6520
65121
60087
46300

我想把6开头的全都替换成5开头

注意这是表里的数据,我想用SQL语句实现替换
SQL code:
update tb set
num = s ......

怎么连接安装时没有使用默认实例的SQL Server2000?

A机器(XP)(10.10.6.65)完整的计算机名是: DZD服务器

我装了一个个人版: SQLServer2000,创建了一个实例名为:MYINSTANCE

装了:dotNetFx35setup.exe

并打好了SP4补丁:SQL2000-KB884525-SP ......

求助一句SQL语句

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

数据如下:

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