Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 How to Write Maltiple quries in Case Statement

Author  Topic 

dheresantosh9
Starting Member

3 Posts

Posted - 2013-09-19 : 02:41:11
HI Experts,

I have to do following scenario,

if 1st query Then 2nd Query
Else 'Msg'

How Can i do this using Case Statement??
Or can you please tell me how can do this by Other way??

Thank you.

santosh dhere

bandi
Master Smack Fu Yak Hacker

2242 Posts

Posted - 2013-09-19 : 02:47:49
we can use recursive/derived queries for that scenario..
Can you elaborate the scenario which you have with sample data and expected output

--
Chandu
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-22 : 02:42:11
whats it that you want to check from first query? a boolean result whether it returns row or value? if former use IF EXISTS otherwise use a variable to hold value and then use it in IF condition match

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2013-09-22 : 12:19:34
You need to provide more information to help you.Explain what you are going to do with query1 and query2

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page

dheresantosh9
Starting Member

3 Posts

Posted - 2013-09-25 : 00:31:41
hi experts,

Thank you for replay. my question is,
if User is Manager then only balance of that vendor will checked,
i have done following query,

SELECT CASE
When (SELECT T0.USER_CODE='manager' FROM OUSR T0 WHERE INTERNAL_K = $[USER])
Then (SELECT CASE WHEN T0.[Balance] >= 0 AND T0.[Balance] <=10000 THEN 'Normal'
WHEN T0.[Balance] < 0 AND T0.[Balance] >-10000 THEN 'NORMAL'
WHEN T0.[Balance] > 10000 THEN 'High'
WHEN T0.[Balance] <-10000 THEN 'HIGH'
ELSE 'Returns'
FROM OCRD T0 WHERE T0.[CardCode] = $[$4.0.0] END)
ELSE 'RETURNS' END

if i run dis two query differently then it working fine.
How can i put it in one query.??
Thanks in advance.

santosh dhere
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-09-25 : 07:47:47
Assuming you're using SQL Server

SELECT CASE
When OU.USER_CODE='manager'
Then CASE WHEN T0.[Balance] >= 0 AND T0.[Balance] <=10000 THEN 'Normal'
WHEN T0.[Balance] < 0 AND T0.[Balance] >-10000 THEN 'NORMAL'
WHEN T0.[Balance] > 10000 THEN 'High'
WHEN T0.[Balance] <-10000 THEN 'HIGH'
ELSE 'Returns'
END
ELSE 'RETURNS'
END
FROM OCRD T0
INNER JOIN OUSR OU
ON OU.USER = T0.INTERNAL_K
WHERE T0.[CardCode] = $[$4.0.0] )


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -