我有二个数据库,一个为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脚本的方法能完成吗?

解决方案 »

  1.   

    两个库之间的数据,怎么来判断是否一样呢?
    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
      

  2.   

    --关于两个库,可以使用:数据库名..表名来表示create table #user1 (col1 int, col2 int)
    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
      

  3.   

    delete from u2..user where not exists(select 1 from u1..user1 where u2..user.co1=u1..user.co1)update u1..user set co2=a.co2 from u2..user a where u1..user.co1=a.co1
      

  4.   

    delete #user2 where col1 not in (select col1 from #user1)
    这条语句是没,可执行后返回为0行啊,可我USER2表中有6000条记录,User表中有3000条记录啊
      

  5.   

    delete #user2 where col1 not in (select col1 from #user1)
    --------------------------------------------------------------------------------要根据你实际的库名和表名改进这个语句
    假设你实际的库名分别是U1,U2,表名都是userdelete U2..[user] where col1 not in (select col1 from U1..[user])
      

  6.   

    是我啊,我是,我查询分析器连接的数据库是dy。所以我就没用dy.dbo.station
    delete  station where end_station not in (select zhan  from bak.dbo.us)
    这样的,没错吧
      

  7.   

    --先执行看看有没有符合条件的数据
    select * from station where end_station not in (select zhan from bak.dbo.us)
      

  8.   

    可能是空值的原因,zhan、end_station有空值吗
      

  9.   

    主  题:  高分跪求一个问题!!在线等待  
    作  者:  xiaobifei (小菲)  
    等  级:    
    信 誉 值:  98  
    所属社区:  MS-SQL Server 基础类  
    问题点数:  20  
    回复次数:  16  
    发表时间:  2006-03-28 16:39:22  
    搂主没见过分吧,20分也敢说高分