1

For example:

CREATE PROCEDURE [dbo].[procGetTable]
    (
      @SetUPDLOCK BIT,
      @RecordId BigInt
    ) 
AS

SELECT * 
FROM MYTABLE WITH (CASE WHEN @SetUPDLOCK = 1 THEN 'UPDLOCK' ELSE '' END)
WHERE MYTABLE.RecordId = @RecordId

/* P.S. I Know the above does not work it conceptual only */

1 Answer 1

2
CREATE PROCEDURE [dbo].[procGetTable]
      @SetUPDLOCK BIT,
      @RecordId BigInt
AS
BEGIN
   SET NOCOUNT ON; 

    IF (@SetUPDLOCK = 1 )
      BEGIN
            SELECT * 
            FROM MYTABLE WITH (UPDLOCK)
            WHERE MYTABLE.RecordId = @RecordId
       END
    ELSE 
      BEGIN
            SELECT * 
            FROM MYTABLE
            WHERE MYTABLE.RecordId = @RecordId
       END
END
Sign up to request clarification or add additional context in comments.

3 Comments

Cheers any way to do it without duplicating query statements
only other way is the dynamic sql then, here you are not duplicating the query only one of the queries will be executed.
Ok cheer I will make this one as the correct answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.