Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Correct way to assign PK
Message
General information
Forum:
Microsoft SQL Server
Category:
Stored procedures, Triggers, UDFs
Title:
Correct way to assign PK
Miscellaneous
Thread ID:
00673548
Message ID:
00673548
Views:
38
I have this SP to retrieve PK values from a table that has a row for each user table:
BEGIN TRANSACTION
-- Note the HOLDLOCK!
SELECT @cNext = NextId
  FROM IdTable (HOLDLOCK)
  WHERE Table = @tcTablename
IF (@@rowcount = 1)
  BEGIN
    -- get the next Pk
    UPDATE IdTable
      SET NextId = NextId + 1
      WHERE Table = @tcTablename
    COMMIT TRANSACTION
  END
ELSE
    SET @cNext = NULL
    ROLLBACK TRANSACTION
END
Intermittantly, no PK value is being assigned. I am wondering if this could be a problem with the HOLDLOCK? If another query runs this SP while the first one is in a transaction will the second one block or will it fall into the ELSE clause? Will an UPDATE lock fix it?

TIA
Next
Reply
Map
View

Click here to load this message in the networking platform