日期:2014-05-17  浏览次数:20423 次

sql2000判断结果是否一致以及同步数据库
有语句1:
select * from table1 where po='12001'
和语句2:
select * from table2 where po='12001'
如何判断两条语句的结果一致?(15分)
如果不一致,那么需要把table1的数据,全部覆盖到table2中,请问该怎么做?(45分)

------解决方案--------------------
第一个问题:select * from table1 a where po='12001' and not exists (select 1 from table2 b where b.po='12001' and 这里把a和b的相同的列全部加上去,比如a.id=b.id and a.name=b.name等)
------解决方案--------------------
第二个问题:
if exists (这里把第一个问题那个语句放进去) is not null 
delete from table2 where po='12001'
insert into table2 (列名) select 列名 from table1 where po='12001'
------解决方案--------------------
--1
if((select count(*) from table1 where po='12001') =(select count(*) from table2 where po='12001')
and (select count(*) from table1 where po='12001')=select count(*) from 
(select * from table1 where po='12001'
union
select * from table2 where po='12001')t)
 print '一致'
else
 print '不一致'
--2
if('不一致')
delete table2 where po='12001'
insert into table2
select * from table1 where po='12001'
------解决方案--------------------
SQL code
IF(select CHECKSUM_AGG(CHECKSUM(*)) from table1 where po='12001')
    <>
  (select CHECKSUM_AGG(CHECKSUM(*)) from table2 where po='12001')
BEGIN 
    DELETE FROM table2 WHERE  po='12001'
    INSERT INTO table2 SELECT * FROM table1 WHERE po='12001'
END