Equivalent of Oracle's RowID in SQL Server

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

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

13条回答
  •  孤城傲影
    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
    

提交回复
热议问题