Getting the Id of a row I updated in Sql Server

前端 未结 4 757
别那么骄傲
别那么骄傲 2020-12-01 10:27

I\'m trying to return the Id of a row I update in sql

UPDATE ITS2_UserNames
  SET AupIp = @AupIp
  WHERE @Customer_ID = TCID AND @Handle_ID = ID

  SELECT @         


        
4条回答
  •  星月不相逢
    2020-12-01 11:23

    Wanted to mention here that if you want to take affected row's primary key in variable then you can use OUTPUT clause which can put this in a table variable. Execute below statements for example...

    CREATE TABLE ItemTable(ID INT IDENTITY(1,1),Model varchar(500) NULL, Color VARCHAR(50) null)

    INSERT INTO ItemTable(Model, Color) VALUES('SomeModel', 'Yellow')

    INSERT INTO ItemTable(Model, Color) VALUES('SomeModel', 'Red')

    INSERT INTO ItemTable(Model, Color) VALUES('SomeModel', 'Pink')

    DECLARE @LastUpdateID TABLE(ItemID INT null)

    UPDATE ItemTable SET model = 'abc' OUTPUT INSERTED.ID INTO @LastUpdateID WHERE Color = 'Red'

    SELECT ItemID FROM @LastUpdateID

提交回复
热议问题