我又一段查询代码是这样的select * from news where nAuthor='pldj' union select * from ycjy where tAuthor='pldj' union select * from kxfzg where aAuthor='pldj' union select * from zyzt where zyztAuthor='pldj' 
现在的问题是我以前这四张表里面的主键现在在查询出的这张新标里面有重复,不能够成主键了,我现在想给这张查询出来的新表添加一个自增的主键,可以实现吗?高手指点我下,十分感谢!!!

解决方案 »

  1.   

    create table tb(id int identity(1,1),你其余的列)insert into tb
    select ……
      

  2.   


    select identity(int,1,1) as ID,*
    from (
    select col2,col3,col4 from news where nAuthor='pldj' --减少主键字段
    union 
    select col2,col3,col4 from ycjy where tAuthor='pldj'  --减少主键字段
    union 
    select col2,col3,col4 from kxfzg where aAuthor='pldj'  --减少主键字段
    union 
    select col2,col3,col4 from zyzt where zyztAuthor='pldj' --减少主键字段
    ) a
      

  3.   


    --不好意思,上面少了into新表
    select identity(int,1,1) as ID,*
    into 新表
    from (
    select col2,col3,col4 from news where nAuthor='pldj' --减少主键字段
    union 
    select col2,col3,col4 from ycjy where tAuthor='pldj'  --减少主键字段
    union 
    select col2,col3,col4 from kxfzg where aAuthor='pldj'  --减少主键字段
    union 
    select col2,col3,col4 from zyzt where zyztAuthor='pldj' --减少主键字段
    ) a
      

  4.   

    select identity(int,1,1) as ID,*
    from (
        select col2,col3,col4 from news where nAuthor='pldj' --减少主键字段
        union 
        select col2,col3,col4 from ycjy where tAuthor='pldj'  --减少主键字段
        union 
        select col2,col3,col4 from kxfzg where aAuthor='pldj'  --减少主键字段
        union 
        select col2,col3,col4 from zyzt where zyztAuthor='pldj' --减少主键字段
        ) a 
     
      

  5.   

    select IDENTITY(int,1,1) as id,*
    into 新表
    from(
    select * from news where nAuthor='pldj' 
    union 
    select * from ycjy where tAuthor='pldj' 
    union 
    select * from kxfzg where aAuthor='pldj' 
    union 
    select * from zyzt where zyztAuthor='pldj') l