Equivalent of Oracle's RowID in SQL Server

前端 未结 13 1201
长发绾君心
长发绾君心 2020-11-27 04:22

What\'s the equivalent of Oracle\'s RowID in SQL Server?

相关标签:
13条回答
  • 2020-11-27 04:57

    Check out the new ROW_NUMBER function. It works like this:

    SELECT ROW_NUMBER() OVER (ORDER BY EMPID ASC) AS ROWID, * FROM EMPLOYEE
    
    0 讨论(0)
  • 2020-11-27 04:59

    If you want to permanently number the rows in the table, Please don't use the RID solution for SQL Server. It will perform worse than Access on an old 386. For SQL Server simply create an IDENTITY column, and use that column as a clustered primary key. This will place a permanent, fast Integer B-Tree on the table, and more importantly every non-clustered index will use it to locate rows. If you try to develop in SQL Server as if it's Oracle you'll create a poorly performing database. You need to optimize for the engine, not pretend it's a different engine.

    also, please don't use the NewID() to populate the Primary Key with GUIDs, you'll kill insert performance. If you must use GUIDs use NewSequentialID() as the column default. But INT will still be faster.

    If on the other hand, you simply want to number the rows that result from a query, use the RowNumber Over() function as one of the query columns.

    0 讨论(0)
  • 2020-11-27 05:01

    I took this example from MS SQL example and you can see the @ID can be interchanged with integer or varchar or whatever. This was the same solution I was looking for, so I am sharing it. Enjoy!!

    -- UPDATE statement with CTE references that are correctly matched.
    DECLARE @x TABLE (ID int, Stad int, Value int, ison bit);
    INSERT @x VALUES (1, 0, 10, 0), (2, 1, 20, 0), (6, 0, 40, 0), (4, 1, 50, 0), (5, 3, 60, 0), (9, 6, 20, 0), (7, 5, 10, 0), (8, 8, 220, 0);
    DECLARE @Error int;
    DECLARE @id int;
    
    WITH cte AS (SELECT top 1 * FROM @x WHERE Stad=6)
    UPDATE x -- cte is referenced by the alias.
    SET ison=1, @id=x.ID
    FROM cte AS x
    
    SELECT *, @id as 'random' from @x
    GO
    
    0 讨论(0)
  • 2020-11-27 05:05

    I have to dedupe a very big table with many columns and speed is important. Thus I use this method which works for any table:

    delete T from 
    (select Row_Number() Over(Partition By BINARY_CHECKSUM(*) order by %%physloc%% ) As RowNumber, * From MyTable) T
    Where T.RowNumber > 1
    
    0 讨论(0)
  • 2020-11-27 05:06

    if you just want basic row numbering for a small dataset, how about someting like this?

    SELECT row_number() OVER (order by getdate()) as ROWID, * FROM Employees
    
    0 讨论(0)
  • 2020-11-27 05:09

    Several of the answers above will work around the lack of a direct reference to a specific row, but will not work if changes occur to the other rows in a table. That is my criteria for which answers fall technically short.

    A common use of Oracle's ROWID is to provide a (somewhat) stable method of selecting rows and later returning to the row to process it (e.g., to UPDATE it). The method of finding a row (complex joins, full-text searching, or browsing row-by-row and applying procedural tests against the data) may not be easily or safely re-used to qualify the UPDATE statement.

    The SQL Server RID seems to provide the same functionality, but does not provide the same performance. That is the only issue I see, and unfortunately the purpose of retaining a ROWID is to avoid repeating an expensive operation to find the row in, say, a very large table. Nonetheless, performance for many cases is acceptable. If Microsoft adjusts the optimizer in a future release, the performance issue could be addressed.

    It is also possible to simply use FOR UPDATE and keep the CURSOR open in a procedural program. However, this could prove expensive in large or complex batch processing.

    Caveat: Even Oracle's ROWID would not be stable if the DBA, between the SELECT and the UPDATE, for example, were to rebuild the database, because it is the physical row identifier. So the ROWID device should only be used within a well-scoped task.

    0 讨论(0)
提交回复
热议问题