Equivalent of Oracle's RowID in SQL Server

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

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

相关标签:
13条回答
  • 2020-11-27 05:12

    If you want to uniquely identify a row within the table rather than your result set, then you need to look at using something like an IDENTITY column. See "IDENTITY property" in the SQL Server help. SQL Server does not auto-generate an ID for each row in the table as Oracle does, so you have to go to the trouble of creating your own ID column and explicitly fetch it in your query.

    EDIT: for dynamic numbering of result set rows see below, but that would probably an equivalent for Oracle's ROWNUM and I assume from all the comments on the page that you want the stuff above. For SQL Server 2005 and later you can use the new Ranking Functions function to achieve dynamic numbering of rows.

    For example I do this on a query of mine:

    select row_number() over (order by rn_execution_date asc) as 'Row Number', rn_execution_date as 'Execution Date', count(*) as 'Count'
    from td.run
    where rn_execution_date >= '2009-05-19'
    group by rn_execution_date
    order by rn_execution_date asc
    

    Will give you:

    Row Number  Execution Date           Count
    ----------  -----------------        -----
    1          2009-05-19 00:00:00.000  280
    2          2009-05-20 00:00:00.000  269
    3          2009-05-21 00:00:00.000  279
    

    There's also an article on support.microsoft.com on dynamically numbering rows.

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

    From the Oracle docs

    ROWID Pseudocolumn

    For each row in the database, the ROWID pseudocolumn returns the address of the row. Oracle Database rowid values contain information necessary to locate a row:

    • The data object number of the object
    • The data block in the datafile in which the row resides
    • The position of the row in the data block (first row is 0)
    • The datafile in which the row resides (first file is 1). The file number is relative to the tablespace.

    The closest equivalent to this in SQL Server is the rid which has three components File:Page:Slot.

    In SQL Server 2008 it is possible to use the undocumented and unsupported %%physloc%% virtual column to see this. This returns a binary(8) value with the Page ID in the first four bytes, then 2 bytes for File ID, followed by 2 bytes for the slot location on the page.

    The scalar function sys.fn_PhysLocFormatter or the sys.fn_PhysLocCracker TVF can be used to convert this into a more readable form

    CREATE TABLE T(X INT);
    
    INSERT INTO T VALUES(1),(2)
    
    SELECT %%physloc%% AS [%%physloc%%],
           sys.fn_PhysLocFormatter(%%physloc%%) AS [File:Page:Slot]
    FROM T
    

    Example Output

    +--------------------+----------------+
    |    %%physloc%%     | File:Page:Slot |
    +--------------------+----------------+
    | 0x2926020001000000 | (1:140841:0)   |
    | 0x2926020001000100 | (1:140841:1)   |
    +--------------------+----------------+
    

    Note that this is not leveraged by the query processor. Whilst it is possible to use this in a WHERE clause

    SELECT *
    FROM T
    WHERE %%physloc%% = 0x2926020001000100 
    

    SQL Server will not directly seek to the specified row. Instead it will do a full table scan, evaluate %%physloc%% for each row and return the one that matches (if any do).

    To reverse the process carried out by the 2 previously mentioned functions and get the binary(8) value corresponding to known File,Page,Slot values the below can be used.

    DECLARE @FileId int = 1,
            @PageId int = 338,
            @Slot   int = 3
    
    SELECT CAST(REVERSE(CAST(@PageId AS BINARY(4))) AS BINARY(4)) +
           CAST(REVERSE(CAST(@FileId AS BINARY(2))) AS BINARY(2)) +
           CAST(REVERSE(CAST(@Slot   AS BINARY(2))) AS BINARY(2))
    
    0 讨论(0)
  • 2020-11-27 05:18

    ROWID is a hidden column on Oracle tables, so, for SQL Server, build your own. Add a column called ROWID with a default value of NEWID().

    How to do that: Add column, with default value, to existing table in SQL Server

    0 讨论(0)
  • Please try

    select NEWID()

    Source: https://docs.microsoft.com/en-us/sql/t-sql/data-types/uniqueidentifier-transact-sql

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

    From http://vyaskn.tripod.com/programming_faq.htm#q17:

    Oracle has a rownum to access rows of a table using row number or row id. Is there any equivalent for that in SQL Server? Or how to generate output with row number in SQL Server?

    There is no direct equivalent to Oracle's rownum or row id in SQL Server. Strictly speaking, in a relational database, rows within a table are not ordered and a row id won't really make sense. But if you need that functionality, consider the following three alternatives:

    • Add an IDENTITY column to your table.

    • Use the following query to generate a row number for each row. The following query generates a row number for each row in the authors table of pubs database. For this query to work, the table must have a unique key.

      SELECT (SELECT COUNT(i.au_id) 
              FROM pubs..authors i 
              WHERE i.au_id >= o.au_id ) AS RowID, 
             au_fname + ' ' + au_lname AS 'Author name'
      FROM          pubs..authors o
      ORDER BY      RowID
      
    • Use a temporary table approach, to store the entire resultset into a temporary table, along with a row id generated by the IDENTITY() function. Creating a temporary table will be costly, especially when you are working with large tables. Go for this approach, if you don't have a unique key in your table.

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

    You can get the ROWID by using the methods given below :

    1.Create a new table with auto increment field in it

    2.Use Row_Number analytical function to get the sequence based on your requirement.I would prefer this because it helps in situations where you are you want the row_id on ascending or descending manner of a specific field or combination of fields

    Sample:Row_Number() Over(Partition by Deptno order by sal desc)

    Above sample will give you the sequence number based on highest salary of each department.Partition by is optional and you can remove it according to your requirements

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