Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we use CASE statement outside select or update query. CASE statement on Input Parameters

I want to use CASE statement something like below code or any other suggestion to implement said scenario.

I will be using this in some SP where @UpdateStatus I will be getting as input parameter.

Please suggest.

DECLARE @UpdateStatus nvarchar(20)
SET @UpdateStatus='P'
 CASE 
    WHEN @UpdateStatus='P' THEN
     BEGIN
        BEGIN TRAN
            Print 'A' --Update some table
        COMMIT
     END
    WHEN @UpdateStatus='F' THEN 
     BEGIN
        BEGIN TRAN
            Print 'B' --Update some table
        COMMIT
     END
 END
like image 717
Anand Gupta Avatar asked Mar 01 '26 14:03

Anand Gupta


1 Answers

Use IF Else Statement to handle different value

DECLARE @UpdateStatus nvarchar(20)
SET @UpdateStatus='P'
 IF @UpdateStatus='P'
     BEGIN
        BEGIN TRAN
            Print 'A' --Update some table
        COMMIT
     END
 ELSE IF @UpdateStatus='F'
     BEGIN
        BEGIN TRAN
            Print 'B' --Update some table
        COMMIT
     END
like image 139
HaveNoDisplayName Avatar answered Mar 06 '26 00:03

HaveNoDisplayName



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!