我有二个数据库,一个为U1,另一个为U2,U2下有一个表User,U1也有一个表User,我现在是想把U2下的User表的记录与U1下的User表记录做一个比较,从第一记录开始当U2下User表列为CO1的记录在U1下User列CO1记录中没有时,删除U2下User表的这条记录,如果有,把U1下User表中列为CO2的值替换U2下User表列CO2的值,依次循环到记录末尾,请问,不用写程序的方法,用SQL脚本的方法能完成吗?
调试欢乐多
co1是不重复的吗?如果不重复,即可以用co1来唯一决定一行的话,可以用这样的脚本delete from u1.dbo.user where u1.dbo.user.co1 not in (select co1 from u2.dbo.user)
update user set co2=u2u.co2
from u1.dbo.user inner join u2.dbo.user u2u on user.co1=u2u.co1
create table #user2 (col1 int, col2 int)--删除user2中有,而user1中没有的数据
delete #user2 where col1 not in (select col1 from #user1)--更新user2中的col2值
update #user2 set col2=a.col1 from #user1 a inner join #user2 b on a.col1=b.col1drop table #user1,#user2
这条语句是没,可执行后返回为0行啊,可我USER2表中有6000条记录,User表中有3000条记录啊
--------------------------------------------------------------------------------要根据你实际的库名和表名改进这个语句
假设你实际的库名分别是U1,U2,表名都是userdelete U2..[user] where col1 not in (select col1 from U1..[user])
delete station where end_station not in (select zhan from bak.dbo.us)
这样的,没错吧
select * from station where end_station not in (select zhan from bak.dbo.us)
作 者: xiaobifei (小菲)
等 级:
信 誉 值: 98
所属社区: MS-SQL Server 基础类
问题点数: 20
回复次数: 16
发表时间: 2006-03-28 16:39:22
搂主没见过分吧,20分也敢说高分