Update SQL with consecutive numbering

前端 未结 10 539
庸人自扰
庸人自扰 2020-12-02 20:16

I want to update a table with consecutive numbering starting with 1. The update has a where clause so only results that meet the clause will be renumbered. Can I accomplish

相关标签:
10条回答
  • 2020-12-02 20:27

    Join to a Numbers table? It involves an extra table, but it wouldn't be temporary -- you'd keep the numbers table around as a utility.

    See http://web.archive.org/web/20150411042510/http://sqlserver2000.databases.aspfaq.com/why-should-i-consider-using-an-auxiliary-numbers-table.html

    or

    http://www.sqlservercentral.com/articles/Advanced+Querying/2547/

    (the latter requires a free registration, but I find it to be a very good source of tips & techniques for MS SQL Server, and a lot is applicable to any SQL implementation).

    0 讨论(0)
  • 2020-12-02 20:32

    To get the example by Shannon fully working I had to edit his answer:

    ; WITH CTE AS (
        SELECT ROW_NUMBER() OVER (ORDER BY [NameOfField]) as RowNumber, t1.ID
        FROM [ActualTableName] t1
    )
    UPDATE [ActualTableName]
        SET Name = 'Depersonalised Name ' + CONVERT(varchar(255), RowNumber)
    FROM CTE
        WHERE CTE.Id = [ActualTableName].ID
    

    as his answer was trying to update T, which in his case was the name of the Common Table Expression, and it throws an error.

    0 讨论(0)
  • 2020-12-02 20:32
    UPDATE TableName
    SET TableName.id = TableName.New_Id
    FROM (
      SELECT id, ROW_NUMBER() OVER (ORDER BY id) AS New_Id
      FROM TableName
      ) TableName
    
    0 讨论(0)
  • 2020-12-02 20:34

    As well as using a CTE or a WITH, it is also possible to use an update with a self-join to the same table:

    UPDATE a
    SET a.columnToBeSet = b.sequence
    FROM tableXxx a
    INNER JOIN
    (
       SELECT ROW_NUMBER() OVER ( ORDER BY columnX ) AS sequence, columnY, columnZ
       FROM tableXxx
       WHERE columnY = @groupId AND columnY = @lang2
    ) b ON b.columnY = a.columnY AND b.columnZ = a.columnZ
    

    The derived table, alias b, is used to generated the sequence via the ROW_NUMBER() function together with some other columns which form a virtual primary key. Typically, each row will require a unique sequence value.

    The WHERE clause is optional and limits the update to those rows that satisfy the specified conditions.

    The derived table is then joined to the same table, alias a, joining on the virtual primary key columns with the column to be updated set to the generated sequence.

    0 讨论(0)
  • 2020-12-02 20:40

    In oracle this works:

    update myTable set rowColum = rownum
    where something = something else
    

    http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/pseudocolumns009.htm#i1006297

    0 讨论(0)
  • 2020-12-02 20:44

    For Microsoft SQL Server 2005/2008. ROW_NUMBER() function was added in 2005.

    ; with T as (select ROW_NUMBER() over (order by ColumnToOrderBy) as RN
            , ColumnToHoldConsecutiveNumber from TableToUpdate
        where ...)
    update T
    set ColumnToHoldConsecutiveNumber = RN
    

    EDIT: For SQL Server 2000:

    declare @RN int
    set @RN = 0 
    
    Update T
    set ColumnToHoldConsecutiveNubmer = @RN
        , @RN = @RN + 1
    where ...
    

    NOTE: When I tested the increment of @RN appeared to happen prior to setting the the column to @RN, so the above gives numbers starting at 1.

    EDIT: I just noticed that is appears you want to create multiple sequential numbers within the table. Depending on the requirements, you may be able to do this in a single pass with SQL Server 2005/2008, by adding partition by to the over clause:

    ; with T as (select ROW_NUMBER() 
            over (partition by Client, City order by ColumnToOrderBy) as RN
         , ColumnToHoldConsecutiveNumber from TableToUpdate)
    update T
    set ColumnToHoldConsecutiveNumber = RN
    
    0 讨论(0)
提交回复
热议问题