SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to use CASE statement in Procedure


How to use CASE statement in Procedure

Author
Message
MSKR
MSKR
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 128
Hi All

I have to 3 sql statements to compare the values in the dimension table.

All i need is i want do is i need to create the SQL procedure which return either 0 or 1 by keeping these 3 SQL statements in the CASE one by one where it has to return either 0 or 1 i.e if 1st,2nd or 3rd select statement returns any value it has to return '1' else it has to return '0' then the procedures also has to return either 0 or 1 accordingly.


Pls help me out in this issue..
Thanks In Advance
terrance.steadman
terrance.steadman
Mr or Mrs. 500
Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)

Group: General Forum Members
Points: 543 Visits: 538
Greetings,

Not sure if you are asking for putting a CASE inside a stored procedure or if you want a single SELECT to use the CASE statement. It sounds like you already have 3 different SELECT statements to return your values and just want to have a flag turned on if any of them return a result. If so, then maybe this will help you.



DECLARE @CheckFlag tinyint

SET @CheckFlag = 0

IF EXISTS(SELECT * FROM MyTable WHERE Checks1 = true)
BEGIN
SET @ChecksFlag = 1
END

IF EXISTS(SELECT * FROM MyTable WHERE Checks2 = true)
BEGIN
SET @ChecksFlag = 1
END

IF EXISTS(SELECT * FROM MyTable WHERE Checks3 = true)
BEGIN
SET @ChecksFlag = 1
END

RETURN(@ChecksFlag) -- OR Just use it from the procedure.



MSKR
MSKR
Valued Member
Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)Valued Member (54 reputation)

Group: General Forum Members
Points: 54 Visits: 128
Hi Thanks For the reply

ya i have 3 sql statements , it may return the value or may not return the value .If the 1st statement returns value then end the procedure by returning flag as "1" else go to 2nd SQL statement and it may return the value or may not return the value and if returns any value then end the procedure and return the flag as "1" and same in the case of 3rd...if all the 3 sql statements dont return the value end the procedure which returns "0" .... hope u got my problem...pls can u provide a proceedure for this?


Thanks in advance
GilaMonster
GilaMonster
SSC Guru
SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)SSC Guru (218K reputation)

Group: General Forum Members
Points: 218229 Visits: 46278
Case is only used within a query. You'll have to use IF statements, like what Terrance has shown you.

Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


terrance.steadman
terrance.steadman
Mr or Mrs. 500
Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)Mr or Mrs. 500 (543 reputation)

Group: General Forum Members
Points: 543 Visits: 538
Greetings,

If your concern is to not run all of the checks, then you can modify the IF statements as such:

IF EXISTS(SELECT * FROM MyTable WHERE Checks2 = true) AND @ChecksFlag = 0
BEGIN
SET @ChecksFlag = 1
END


Another option may be to set @ChecksFlag to a value between 1 and 3 to show which check it was validated against if you needed or wanted to know.

Have a good day.

Terry Steadman
manish.singh-1128425
manish.singh-1128425
SSC-Enthusiastic
SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)SSC-Enthusiastic (126 reputation)

Group: General Forum Members
Points: 126 Visits: 106
Hi,

This thing can be acheived using one querry also. I am not sure that all the three conditions need to be checked in the same table or different tables.

-- If it is same table
SELECT COUNT(TOP 1 1) AS Check
FROM MyTable
WHERE Checks1 = true
OR Checks2 = true
OR Checks3 = true

-- If it is in different tables
SELECT COUNT(TOP 1 1) AS Check -- 'Here i have used 1 as a contant value so that it
returns 1 if any of ur conditions are satisfied
FROM MyTable1
WHERE Checks1 = true
OR EXISTS(SELECT 1
FROM MyTable2 WHERE Checks2 = true)
OR EXISTS(SELECT 1
FROM MyTable3 WHERE Checks3 = true)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search