MS-SQL Query - Update record retrieved via Stored Procedure

前端 未结 2 865
时光说笑
时光说笑 2021-01-24 07:52

*UPDATE *

The scope has now developed slightly, and I now need to retreive the fields \'Id, uri, linkTo\'. How does this change things???

I\'m u

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-24 08:34

    There is a quirky update syntax that will let you assign the value to a variable at the same time as performing an update, but I'm not going to show it to you because it is undocumented, unsupported, and I can't guarantee that it will continue to work in future versions of SQL Server. Here is the method I would recommend (assuming Id is PK or at least unique):

    DECLARE 
      @Id INT, 
      @uri VARCHAR(2048),
      @linkTo VARCHAR(2048);
    
    SELECT TOP 1 @Id = Id, @uri = uri, @linkTo = linkTo
      FROM dbo.Adverts
      WHERE adRegion = @region
      ORDER BY NEWID();
    
    UPDATE dbo.Adverts
      SET adShown = adShown + 1
      WHERE Id = @Id;
    
    SELECT Id = @Id, uri = @uri, linkTo = @linkTo;
    

提交回复
热议问题