举个例子吧。
表1(2月份工资表)工号   工资年月   基本工资   绩效工资
001     2011/2    4000          2000
002     2011/2    5000          2500
003     2011/2    4500          2300
表2 (3月份工资表)
工号   工资年月   基本工资   绩效工资
001     2011/3    4000          2000
002     2011/3    5000          2600
003     2011/3    4500          2300想查询出来的结果:
工号   工资年月   基本工资   绩效工资
002     2011/3    5000          2600
1,001、003两个月工资都没有变化,结果不要
2,不能使用 ( where 表1.基本工资<>表2.基本工资 or where 表1.绩效工资<>表2.绩效工资),因为这个月客户可能想使用这两个值做比较,但下个月就可能不是了,即列“工资年月”后面的列名、列数不定。但两个表结构肯定是一样的。
SQL Server 2005中有没有办法比较两个结构相同的表,把不同的行作为结果查询出来?可以忽略“工资年月”这列,比对完毕后再加上固定列值2011/3就行了。

解决方案 »

  1.   


    select * from 表2
    except
    select * from 表1试试
      

  2.   

    declare @t1 table 
    (工号 varchar(3),工资年月 varchar(6),基本工资 int,绩效工资 int)
    insert into @t1
    select '001','2011/2',4000,2000 union all
    select '002','2011/2',5000,2500 union all
    select '003','2011/2',4500,2300declare @t2 table 
    (工号 varchar(3),工资年月 varchar(6),基本工资 int,绩效工资 int)
    insert into @t2
    select '001','2011/3',4000,2000 union all
    select '002','2011/3',5000,2600 union all
    select '003','2011/3',4500,2300select '2011/3' as 工资年月,工号,基本工资,绩效工资 from @t2
    except
    select '2011/3',工号,基本工资,绩效工资 from @t1/*
    工资年月   工号   基本工资        绩效工资
    ------ ---- ----------- -----------
    2011/3 002  5000        2600
    */
      

  3.   

    select
      *
    from
      b
    where
      checksum(*)
    not in
      (select checksum(*) from a)