表结构
FItemNumber   FEntryId
001
001
001
002
003
003
求一SQL语句,更新FEntryId如下
FItemNumber   FEntryId
001           1    
001           2
001           3
002           1
003           1
003           2除了游标循环,还有什么好方法。

解决方案 »

  1.   

    if object_id('[tb]') is not null drop table [tb]
    go
    create table [tb]([FItemNumber] varchar(3),[FEntryId] int)
    insert [tb]
    select '001',null union all
    select '001',null union all
    select '001',null union all
    select '002',null union all
    select '003',null union all
    select '003',null
     
    alter table tb add  px int identity(1,1)
    goupdate t
    set t.fentryid=(select count(1)+1 from tb where FItemNumber=t.FItemNumber and px<t.px)
    from tb talter table tb drop column pxselect * from tb/**
    FItemNumber FEntryId
    ----------- -----------
    001         1
    001         2
    001         3
    002         1
    003         1
    003         2(6 行受影响)
    **/
      

  2.   

    ;with t as
    (
      select rn=row_number()over(partition by FItemNumber order by getdate()),*
      from tb
    )
    update t set FentryID=rn
      

  3.   

    --------------------------------------------------------------------------
    --  Author : htl258(Tony)
    --  Date   : 2010-04-18 10:05:10
    --  Version:Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86) 
    --          Jul  9 2008 14:43:34 
    --          Copyright (c) 1988-2008 Microsoft Corporation
    --          Developer Edition on Windows NT 5.1 <X86> (Build 2600: Service Pack 2)
    --------------------------------------------------------------------------
    --> 生成测试数据表:tbIF NOT OBJECT_ID('[tb]') IS NULL
    DROP TABLE [tb]
    GO
    CREATE TABLE [tb]([FItemNumber] NVARCHAR(10),[FEntryId] INT)
    INSERT [tb]
    SELECT '001',1 UNION ALL
    SELECT '001',NULL UNION ALL
    SELECT '001',NULL UNION ALL
    SELECT '002',NULL UNION ALL
    SELECT '003',NULL UNION ALL
    SELECT '003',NULL
    GO
    --SELECT * FROM [tb]-->SQL查询如下:
    ;with t as
    (
      select rn=row_number()over(partition by FItemNumber order by getdate()),*
      from tb
    )
    update t set FEntryID=rnselect * from tb
    /*
    FItemNumber FEntryId
    ----------- -----------
    001         1
    001         2
    001         3
    002         1
    003         1
    003         2(6 行受影响)*/
      

  4.   

     select rn=row_number()over(partition by FItemNumber order by getdate()),*
      from tb
      

  5.   

    --------------------SQL Server数据格式化工具-------------------
    ---------------------------------------------------------------
    -- DESIGNER :happycell188(喜喜)
    --       QQ :584738179
    -- Development Tool :Microsoft Visual C++ 6.0    C Language 
    -- FUNCTION :CONVERT DATA TO T-SQL
    ---------------------------------------------------------------
    -- Microsoft SQL Server  2005
    -- Developer Edition on Microsoft Windows XP [版本 5.1.2600]
    ---------------------------------------------------------------
    ---------------------------------------------------------------use test
    go
    if object_id('test.dbo.tb') is not null drop table tb
    -- 创建数据表
    create table tb
    (
    FItemNumber char(4),
    FEntryId int
    )
    go
    --插入测试数据
    insert into tb select '001',null
    union all select '001',null
    union all select '001',null
    union all select '002',null
    union all select '002',null
    union all select '003',null
    union all select '003',null
    union all select '003',null
    union all select '003',null
    go
    --代码实现
    with tt as(select idd=row_number()over(partition by FItemNumber order by getdate()),* from tb)
    update tt set FentryID=idd
    select * from tb
    /*测试结果FItemNumber FEntryId
    ---------------------
    001  1
    001  2
    001  3
    002  1
    002  2
    003  1
    003  2
    003  3
    003  4(9 行受影响)
    */