动态生成列名DECLARE @s NVARCHAR(4000)
SET @s = ''
SELECT  @s = @s + ',' + 'isnull('+QUOTENAME(name)+','''')'
FROM    syscolumns
WHERE   id = OBJECT_ID('表名')
        --AND name NOT IN ( '不希望显式的列' )
SET @s = STUFF(@s, 1, 1, '')
EXEC('select '+@s+' from 表名  ')

解决方案 »

  1.   

    第一个问题:
    将你的ID设置为种子列就可以了。第二个问题:select * into #tmp from t1 where 1 = 0 
    insert into #tmp select top 1 * from t1 order by id  
    insert into #tmp select top 1 * from t2 order by id 
    delete t2 where id =(select max(id) from t2) 
    insert into t2 select top 1* from #tmp order by -- 这里条件,你自己写上
    drop table #tmp 
      

  2.   

    第2个问题update table2 t2 set t2.col1 = t1.col1, t2.col2 = t1.col2
    from (select max(id) as id, max(col1) as col1, max(col2) as col2 from 
                 (select top 1 * from table1 order by id desc
                  union all
                  select top 1 * from table2 order by id desc) t
         ) t1
    where t2.id = (select max(id) from table2)