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

解决方案 »

  1.   

    create table a(c1 int,c2 int,c3 int)
    create table b(c1 int,c2 int)
    declare @c nvarchar(100)
    set @c = (select name from syscolumns where id = (select id from sysobjects where name = 'a') and name not in (select name from syscolumns where id = (select id from sysobjects where name = 'b')))
    declare @sql nvarchar(200)
    set @sql = ' select ' + @c + ' from a'
    exec (@sql)
      

  2.   

    --try
    declare @col varchar(4000),@s varchar(4000)
    set @col=''
    select @col=@col+'+'+''':'''+ '+cast('+name+' as varchar)' from syscolumns where  id=object_id('a')
    set @col=stuff(@col,1,1,'')
    --print @col
    set @s='select a.* from a where '+@col+' not in (select '+@col+'from b ) '
    exec(@s)