如何获取更新行的PK_ID

时间:2010-08-05 07:04:14

标签: sql sql-server

如何获取更新行的PK_ID

UPDATE 
    [M_RATINGS] 
SET 
    [RATING] = @rating 
WHERE 
    [POST_ID] = @postId AND 
    [USER_ID] = @userId; 

1 个答案:

答案 0 :(得分:4)

使用OUTPUT子句http://msdn.microsoft.com/en-us/library/ms177564.aspx

DECLARE @MyTableVar table(
ID int NOT NULL
); 

UPDATE 
   [M_RATINGS] 
SET   
   [RATING] = @rating 
OUTPUT INSERTED.YourPK_ID INTO @MyTableVar
WHERE 
   [POST_ID] = @postId AND 
   [USER_ID] = @userId; 

来自@MyTableVar的SELECT *;