现有表icmo,表内有若干列,其中有fnote列为空,属性为string。fdate为数据建立时间希望设置icmo表内fdate在2010-01-01到2010-06-30的时间段的fnote值为递增的数字,格式为NO10*****之前问过递增的问题,虽然解答的很好,但是和我的情况有所区别,感谢高手的解答

解决方案 »

  1.   

    可以为列设置默认值为(数字,格式为NO10*****)
    希望设置icmo表内fdate在2010-01-01到2010-06-30的时间段 又是何意呢?
      

  2.   

    --> 测试数据:@t
    declare @t table([id] int,[B] NVARCHAR(200))
    insert @t
    select 1,null union all
    select 2,null union all
    select 3,null union all
    select 4,null union all
    select 5,null union all
    select 6,null union all
    select 7,null union all
    select 8,null union all
    select 9,null
     
    SELECT CASE b.number WHEN 1 THEN 'NO'+RIGHT(1000000+ISNULL(RIGHT((b.number),6),0),6)  
    else RIGHT(1000000+ISNULL(RIGHT((b.number),6),0),6) end
    FROM @t a,(select distinct number from master..spt_values where number > 0  )b 
    where a.id = b.number 
      

  3.   

    --> 测试数据:@t
    declare @t table([id] int,[B] NVARCHAR(200))
    insert @t
    select 1,null union all
    select 2,null union all
    select 3,null union all
    select 4,null union all
    select 5,null union all
    select 6,null union all
    select 7,null union all
    select 8,null union all
    select 9,null
     
    --SELECT CASE b.number WHEN 1 THEN 'NO'+RIGHT(1000000+ISNULL(RIGHT((b.number),6),0),6)  
    --else RIGHT(1000000+ISNULL(RIGHT((b.number),6),0),6) end
    --FROM @t a,(select distinct number from master..spt_values where number > 0  )b 
    --where a.id = b.number  UPDATE a
    SET B = M.PX
    from @t a ,(SELECT a.id,CASE b.number WHEN 1 THEN 'NO'+RIGHT(1000000+ISNULL(RIGHT((b.number),6),0),6)  
    else RIGHT(1000000+ISNULL(RIGHT((b.number),6),0),6) end as PX
    FROM @t a,(select distinct number from master..spt_values where number > 0  )b 
    where a.id = b.number ) M
    where a.id = M.id and a.fdate between '2010-01-01' and '2010-06-30'根据你的表再添加时间判断条件吧
      

  4.   

    --> 测试数据:@t
    declare @t table([a] int,fnote varchar(10))
    insert @t
    select 1,null union all
    select 2,null union all
    select 3,null union all
    select 4,null union all
    select 5,null union all
    select 6,null union all
    select 7,null union all
    select 8,null union all
    select 9,null
    declare @s int,@v varchar(10)set @s=0update  @t set @s=@s+1,@v='no10'+right('000000'+ltrim(@s ),6) ,fnote=@v
    --where fdate between '2010-01-01' and '2010-06-30'                       ---把这个条件启用就可以了
     
    select * from @t
    a           fnote
    ----------- ----------
    1           no10000001
    2           no10000002
    3           no10000003
    4           no10000004
    5           no10000005
    6           no10000006
    7           no10000007
    8           no10000008
    9           no10000009(9 行受影响)
      

  5.   

    可以先用select试试,不知道是否适用
    update t
    set t.fnote='NO'+ltrim(10000000+(select count(1)+1 from icmo where fdate<t.fdate))
    from icmo t 
    其他参考 update中变量的赋值用法 实现分组更新序号 
    http://blog.csdn.net/xys_777/archive/2010/07/21/5753206.aspx