日期:2014-05-19  浏览次数:20535 次

从数据库A的表中插入到当前数据库的同样表中,条件是A数据库表有记录,而当前没有
大家看,下面这句有什么问题,SQL提示:对象名   ztjpos.dbo.tbhyda   无效

exec   sp_addlinkedserver     'srv_lnk ', ' ', 'SQLOLEDB ',A            

exec   sp_addlinkedsrvlogin     'srv_lnk ', 'false ',null, 'sa ', ' '

insert   into   tbhyda  
              select   *   from     OPENQUERY(srv_lnk,   'SELECT   *   FROM   ztjpos.dbo.tbhyda ')  
              where   f_hykh   not   in(select   f_hykh   from   tbhyda)


exec   sp_dropserver   'srv_lnk ', 'droplogins '



------解决方案--------------------
INSERT INTO B SELECT DISTINCT A.* FROM A INNER JOIN B ON CAST(ISNULL(A.COL1, '| ') AS VARCHAR)+CAST(ISNULL(A.COL2 '| ') AS VARCHAR)+CAST(ISNULL(A.COL3 '| ') AS VARCHAR) <> CAST(ISNULL
(B.COL1, '| ') AS VARCHAR)+CAST(ISNULL(B.COL2 '| ') AS VARCHAR)+CAST(ISNULL(B.COL3 '| ') AS VARCHAR)