在一张表上,我想新增加一个序号列(例如:000001,000002.。),设列名为aa,表名为A,然后插入另一个新建的表AA,语句如下:
select *,right('000000'+convert(varchar(6),select count(1) from A),6) as ll into AA 
from A
但总提示我错误,请问应该如何修改一下?

解决方案 »

  1.   

    在一张表上,我想新增加一个序号列(例如:000001,000002.。),设列名为aa,表名为A,然后插入另一个新建的表AA,语句如下:
    select *,right('000000'+convert(varchar(6),select count(1) from A),6) as ll into AA 
    from A
    但总提示我错误,请问应该如何修改一下?select * , ll = identity(int,1,1) into tmp from a
    select col1,col2 , right('000000'+cast(ll as varchar) ,6) ll into aa from tmp
    drop table tmp
      

  2.   

    select *,right('000000'+convert(varchar(6),(select count(*) from 表 where 主键<=a.主键)),6) as ll into AA from 表 as a
      

  3.   

    select *,'000000'as ll into AA 
    from A
      

  4.   

    select *,right('000000'+ltrim((select count(1) from A where id<=b.id)),6) as ll  into aa
    from A b
      

  5.   

    你们也不先运行一下
    select 然后再试 select into
      

  6.   

    VCD老兄,你那个出来的很明显是N多个‘000000’、。。
      

  7.   

    select *,  right('000000'+convert(varchar(6),id),6) as ll into aa from A
      

  8.   


    create table a (id int)
    go
    insert into a 
    select 5 union all
    select 444 union all
    select 55 union all
    select 75 union all
    select 4385godeclare @i int
    set @i=0select *,'000000' as ll into aa from a
    update aa set ll=right(ll+cast(@i as varchar(10)),6),@i=@i+1select * from aa
    godrop table aa,a
    go
    id          ll     
    ----------- ------ 
    5           000001
    444         000002
    55          000003
    75          000004
    4385        000005(所影响的行数为 5 行)