读书人

求救:关于表内容的复制,该如何处理

发布时间: 2012-01-07 21:41:55 作者: rapoo

求救:关于表内容的复制
表wlw
iname icount ncount
t1 4 3
t2 6 1
t3 23 9

表wlw2
iname count
t1 1
t2 5
t3 14

其中count是icount与ncount的差.

要求:
当表wlw2为空时,就将t1,t2,t3复制到表wlw2中的iname下,同时将t1,t2,t3对应的icount与ncount的差值复制到count字段下.

如上所示。

如果表wlw2中有数据,如下:
iname count
t2 4
t3 6
t4 7
就将表wlw的数据复制过来,将wlw2的数据替换掉,即:
iname count
t1 1
t2 5
t3 14

要求用通用的方法实现,谢谢



[解决办法]
truncate table wlw2
insert into wlw2
select iname,icount-ncount from wlw
[解决办法]
delete from wlw2
insert wlw2 select iname,icount-ncount from wlw
[解决办法]
insert into wlw2(iname,count) select iname,icount-ncount
from wlw

读书人网 >SQL Server

热点推荐