3.a,b表 结构一样 有主键
update a set a.col1=b.col1,a.col2=b.col2....a.coln=b.coln
from a inner join b on a.pk=b.pk
有没有简便的方法?

解决方案 »

  1.   

    比较两个数据库的表结构差异
    http://blog.csdn.net/zjcxc/archive/2004/01/04/20088.aspx
      

  2.   

    根据zj的改了改 大家看看存储过程能不能优化优化
    还有就是 要@tb1应该是远程数据库中的表 应该怎么写?
    create procedure pCompare
    @tb1 varchar(100),
    @tb2 varchar(100)
    as
    select * into #tb1 from(SELECT 
    字段名=a.name,
    类型=b.name,
            占用字节数=a.length
    FROM sai..syscolumns a
    left join sai..systypes b on a.xtype=b.xusertype 
    inner join sai..sysobjects d on a.id=d.id  and d.xtype='U' and  d.name=''+@tb1+'')bselect * into #tb2 from(SELECT 
    字段名=a.name,
    类型=b.name,
            占用字节数=a.length
    FROM sai..syscolumns a
    left join sai..systypes b on a.xtype=b.xusertype 
    inner join sai..sysobjects d on a.id=d.id  and d.xtype='U' and  d.name=''+@tb2+'')bdeclare @col  varchar(100)
    declare @type varchar(10)
    declare @len  int--增加字段
    declare cur_add cursor for select * from #tb1 where 字段名 not in (select 字段名 from #tb2)
    open cur_add
    fetch next from cur_add into @col,@type,@len
    while @@FETCH_STATUS=0
    begin
      if @type in('varchar','char')
        exec ('alter table recvmsg1 add '+@col+' '+@type+'('+@len+')')
      else if @type in('datetime','int')
        exec ('alter table recvmsg1 add '+@col+' '+@type)
      fetch next  from cur_addcol  into @col,@type,@len
    end
    close cur_add                                    
    deallocate cur_add 
    --更新字段
    declare cur_alter cursor for 
    select * from #tb1 
    where 字段名 in(
    select 字段名 from (
    select * from #tb1
    union
    select * from #tb2) m
    group by 字段名 having count(1)>1)
    open cur_alter
    fetch next from cur_alter into @col,@type,@len
    while @@fetch_status=0
    begin
      if @type in('varchar','char')
        exec ('alter table recvmsg1 alter column '+@col+' '+@type+'('+@len+')')
      else if @type in('datetime','int')
        exec ('alter table recvmsg1 alter column '+@col+' '+@type)
      fetch next  from cur_alter  into @col,@type,@len 
    end
    close cur_alter                                    
    deallocate cur_alter drop table #tb1
    drop table #tb2
      

  3.   

    --LYSSHE(蛇子) 
    --感觉你一条就可以拷定的句子还复杂吗?
    怎么一条搞定啊?
      

  4.   

    我写的是一个DLL,负责本地数据库与服务器的通信
    主要的功能就是读取数据 插入数据 还有就是数据库结构的同步。
    现在我有几个问题
    有很多CLIENT端可能在不同的地方访问服务器
    即客户端数据库不固定  我怎么写客户段的CONNSTRING啊?
    因为这个dll只负责通信。
      

  5.   

    在VB中调用存储过程的时候 编译错误 提示:异类查询要求为连接设置 ANSI_NULLS 和 ANSI_WARNINGS 选项。这将确保一致的查询语义。请启用这些选项,然后重新发出查询。
    那么SET ANSI_NULLS ON SET ANSI_WARNINGS ON GO 语句应该写在哪里?
    写在存储过程中会出错提示要申明变量