我想向一个表一个字段里写入一列数据,比说001,002,003....011,012等,我想用CASE 语句写,这样写老是报错,不知道能不能这样写,到底行不行?能帮我纠正一吗?谢谢了
declare @i int
set @i=1
while @i<100
begin
case len(@i)
when 1 then insert into ab (userid) values ('00'+cast(@i as varchar(1)))
when 2 then insert into ab (userid) values ('0'+cast(@i as varchar(2)))
when 3 then insert into ab (userid) values (cast(@i as varchar(3)))
end
set @i=@i+1
end

解决方案 »

  1.   

    insert into ab (userid) values (right('00'+cast(@i as varchar),3))
      

  2.   

    declare @i int
    set @i=1
    while @i<100
    begin
      print right('00'+cast(@i as varchar),3)
      set @i=@i+1
    end/*
    001
    002
    003
    004
    005
    006
    007
    008
    009
    010
    011
    012
    013
    014
    015
    016
    017
    018
    019
    020
    021
    022
    023
    024
    025
    026
    027
    028
    029
    030
    031
    032
    033
    034
    035
    036
    037
    038
    039
    040
    041
    042
    043
    044
    045
    046
    047
    048
    049
    050
    051
    052
    053
    054
    055
    056
    057
    058
    059
    060
    061
    062
    063
    064
    065
    066
    067
    068
    069
    070
    071
    072
    073
    074
    075
    076
    077
    078
    079
    080
    081
    082
    083
    084
    085
    086
    087
    088
    089
    090
    091
    092
    093
    094
    095
    096
    097
    098
    099*/
      

  3.   

    --得到新编号的函数
    CREATE FUNCTION f_NextBH()
    RETURNS char(8)
    AS
    BEGIN
    RETURN(SELECT 'BH'+RIGHT(1000001+ISNULL(RIGHT(MAX(BH),6),0),6) FROM tb WITH(XLOCK,PAGLOCK))
    END
    GO--在表中应用函数
    CREATE TABLE tb(
    BH char(8) PRIMARY KEY DEFAULT dbo.f_NextBH(),
    col int)--插入资料
    BEGIN TRAN
    INSERT tb(col) VALUES(1)
    INSERT tb(col) VALUES(2)
    INSERT tb(col) VALUES(3)
    DELETE tb WHERE col=3
    INSERT tb(col) VALUES(4)
    INSERT tb(BH,col) VALUES(dbo.f_NextBH(),14)
    COMMIT TRAN--显示结果
    SELECT * FROM tb
    /*--结果
    BH         col 
    ---------------- ----------- 
    BH000001  1
    BH000002  2
    BH000003  4
    BH000004  14
    --*/
      

  4.   

    declare @i int
    set @i=1
    while @i<100
    begin
    insert into ab (userid) values (right('00'+ltrim(@i),3))
    set @i=@i+1
    end
      

  5.   

    declare @i int
    set @i=1
    while @i<100
    begin
    insert into ab (userid) select 
    case len(@i)
    when 1 then  '00'+cast(@i as varchar(1)
    when 2 then '0'+cast(@i as varchar(2))
    when 3 then cast(@i as varchar(3))
    end
    set @i=@i+1
    end
      

  6.   

    declare @i int
    set @i=1
    while @i<100
    begin
    insert into ab (userid) select 
    case len(@i)
    when 1 then  '00'+cast(@i as varchar(1))
    when 2 then '0'+cast(@i as varchar(2))
    when 3 then cast(@i as varchar(3))
    end
    set @i=@i+1
    end
      

  7.   

    declare @i int
    set @i=1
    while @i<100
    begin
    insert  ab (userid)select 
    case 
    when len(@i)=1 then '00'+cast(@i as varchar(1))
    when len(@i)=2 then '0'+cast(@i as varchar(2))
    when len(@i)=3 then cast(@i as varchar(3))
    end
    set @i=@i+1
    end
      

  8.   

    declare @tb table (id int identity(1000,1), t char)
    insert into @tb
    select top 1000 'c'
    from sysobjects a, sysobjects bselect right(cast(id as varchar),3)
    from @tb
      

  9.   

    您老要多少? 超过4位的话:
    ... select top xxxx 'c' from sysobjects a, sysobjects b, sysobjects c ...