三张表
A
year mo
2007 04B
name sr
张三 2048
C
name, 01sr , 02sr, 03sr ……12sr 一共13列张三 2048 2130 2130李四 3209 3328
这三个表name是唯一的,在A表建触发器,当mo改变,不同月份时,B表插入或更新C表中mo相应的月份。
A表怎么建触发器, 用B表去更新C表,
当C表相应 mo月份 已经有值 则更新,没有则插入 (SR)薪水帮忙写一下,谢谢
A
year mo
2007 04B
name sr
张三 2048
C
name, 01sr , 02sr, 03sr ……12sr 一共13列张三 2048 2130 2130李四 3209 3328
这三个表name是唯一的,在A表建触发器,当mo改变,不同月份时,B表插入或更新C表中mo相应的月份。
A表怎么建触发器, 用B表去更新C表,
当C表相应 mo月份 已经有值 则更新,没有则插入 (SR)薪水帮忙写一下,谢谢
go
create table A([year] char(4),mo char(20))
insert into A
select '2007','04'create table B(name varchar(10),sr int)
insert into B
select '张三',2048create table C(name varchar(10),[01sr] int,[02sr] int,[03sr] int,[04sr] int,[05sr] int,[06sr] int,[07sr] int,[08sr] int,[09sr] int,[10sr] int,[11sr] int,[12sr] int)
insert into C(name,[01sr],[02sr],[03sr])
select '张三',2048,2130,2130
union all select '李四',3209,3328,NULLcreate trigger tu_A on A
for update
as
if update(mo) and exists(select 1 from inserted i,deleted d where i.[year]=d.[year] and isnull(i.mo,'')<>isnull(d.mo,''))
begin
declare @sql varchar(8000)
declare @mo char(2)
declare cur_tmp cursor for
select mo from inserted
open cur_tmp
fetch next from cur_tmp into @mo
while @@fetch_status=0
begin
select @sql='
update C
set ['+@mo+'sr]=B.sr
from C,B
where C.name=B.name'
exec(@sql)
fetch next from cur_tmp into @mo
end
close cur_tmp
deallocate cur_tmp
end
原来还能上MSN,现在MSN也上不了了.你发邮件到[email protected]吧,或留言.