*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
try a single command:
CREATE PROCEDURE dbo.getNewAds
(
@region --lazy, declare type!!
)
AS
BEGIN
UPDATE TOP (1) Adverts
SET adShown = adShown + 1
OUTPUT INSERTED.ID
WHERE adRegion = @region
END
UPDATE (Transact-SQL) says that:
TOP ( expression) [ PERCENT ]
Specifies the number or percent of rows that will be updated. expression can be either a number or a percent of the rows.
The rows referenced in the TOP expression that is used with INSERT, UPDATE, MERGE, or DELETE are not arranged in any order.
but in my limited testing (not many rows in the test table), it looks like it updates the same row each time, and that the OP is trying to update a different row each time.
so try this:
CREATE PROCEDURE dbo.getNewAds
(
@region --lazy, declare type!!
)
AS
BEGIN
DECLARE @ID int
--select row to update
SELECT TOP 1
@ID=Id
FROM Adverts
WHERE adRegion = @region
ORDER BY NEWID()
--update and return result set in one command
UPDATE TOP (1) Adverts
SET adShown = adShown + 1
OUTPUT INSERTED.ID
WHERE ID=@ID
END