|
-
Nov 27th, 2006, 09:02 AM
#1
Thread Starter
Giants World Champs!!!!
[RESOLVED] CASE Statment in SQL WHERE Clause
I have the following SQL String:
VB Code:
Declare @UNIT NVARCHAR(50)
Declare @CASENUM NVARCHAR(50)
SET @UNIT = Null /*'Sales'*/
Set @CASENUM = Null /*'C0020'*/
SELECT INC.Unit, INC.CASENUM
FROM INCIDENTS INC INNER JOIN CASES CS ON INC.CASENUM = CS.CASENUM
WHERE
(CASE @CASENUM
WHEN Null THEN 1
WHEN @CASENUM THEN (CASE WHEN INC.CASENUM = @CASENUM THEN 1 ELSE 0 END) END) = 1
OR
(CASE @UNIT
WHEN Null THEN 1
WHEN @UNIT THEN (CASE WHEN INC.UNIT = @UNIT THEN 1 ELSE 0 END) END) = 1
ORDER BY INC.UNIT
This code works fine as long as I supply at least one of the Variables, but What I want to do if both variables are null I want all of the records returned. When both variables are omitted this SQL String returns no records.
Any ideas?
Last edited by Mark Gambo; Nov 27th, 2006 at 09:38 AM.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 09:36 AM
#2
Re: CASE Statment in SQL WHERE Clause
Doesn't this do the same thing and handle the "both supplied situation?
Code:
Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
and (@InvUnit is Null or @InvUnit=Inc.Unit)
btw - what's with @InvUnit and @Unit?
-
Nov 27th, 2006, 09:41 AM
#3
Thread Starter
Giants World Champs!!!!
Re: CASE Statment in SQL WHERE Clause
 Originally Posted by szlamany
. . .btw - what's with @InvUnit and @Unit?
Thanks again, Just a typo @InvUnit is really @Unit in disguise
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 09:50 AM
#4
Thread Starter
Giants World Champs!!!!
Re: CASE Statment in SQL WHERE Clause
Steve I forgot one statement in the sql string, if the @Unit is not Null I want all records that equal the @UNIT variable and that rows Status Column doesn't contain 'Completed'. If the @Unit is null I want all of the records returned based upon
VB Code:
Declare @UNIT NVARCHAR(50)
Declare @CASENUM NVARCHAR(50)
SET @UNIT = Null /*'Sales'*/
Set @CASENUM = Null /*'C0020'*/
SELECT INC.Unit, INC.CASENUM
FROM INCIDENTS INC INNER JOIN CASES CS ON INC.CASENUM = CS.CASENUM
WHERE
(CASE @CASENUM
WHEN Null THEN 1
WHEN @CASENUM THEN (CASE WHEN INC.CASENUM = @CASENUM THEN 1 ELSE 0 END) END) = 1
OR
(CASE @UNIT
WHEN Null THEN 1
WHEN @UNIT THEN (CASE WHEN (INC.UNIT = @UNIT AND [B]INC.STATUS <> 'Completed'[/B]) THEN 1 ELSE 0 END) END) = 1
ORDER BY INC.UNIT
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 09:52 AM
#5
Re: CASE Statment in SQL WHERE Clause
So would this do it?
Code:
Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
and (@Unit is Null or (@Unit=Inc.Unit and INC.STATUS <> 'Completed'))
-
Nov 27th, 2006, 09:58 AM
#6
Thread Starter
Giants World Champs!!!!
Re: CASE Statement in SQL WHERE Clause
No dice, I am getting all the records irregardless of each records Status type
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 10:01 AM
#7
Re: CASE Statement in SQL WHERE Clause
 Originally Posted by Mark Gambo
No dice, I am getting all the records irregardless of each records Status type
When you specify @CaseNum or @Unit?
btw - the problem with your WHERE/CASE block logic might be because you are missing ELSE statements in the outer-CASE. Without an ELSE a CASE/block will return a null if conditions are not met...
-
Nov 27th, 2006, 10:02 AM
#8
Thread Starter
Giants World Champs!!!!
Re: CASE Statement in SQL WHERE Clause
 Originally Posted by szlamany
When you specify @CaseNum or @Unit?
When I don't specify either of these variables (Null)
 Originally Posted by szlamany
btw - the problem with your WHERE/CASE block logic might be because you are missing ELSE statements in the outer-CASE. Without an ELSE a CASE/block will return a null if conditions are not met...
Hmm, I'll take a look at that, thanks!
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 10:05 AM
#9
Re: CASE Statment in SQL WHERE Clause
Code:
Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
and (@Unit is Null or (@Unit=Inc.Unit and INC.STATUS <> 'Completed'))
and (@CaseNum is not null or @Unit is not null)
So adding this third AND condition - that either of the two must not be null - should take care of that situation...
-
Nov 27th, 2006, 10:17 AM
#10
Thread Starter
Giants World Champs!!!!
Re: CASE Statment in SQL WHERE Clause
Small change to you code:
VB Code:
Where (@CaseNum is Null or @CaseNum=Inc.CaseNum)
and ((@Unit is Null and INC.STATUS <> 'Completed') or (@Unit=Inc.Unit and INC.STATUS <> 'Completed'))
Thanks again Steve, you are a miracle worker
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 10:20 AM
#11
Thread Starter
Giants World Champs!!!!
Re: [RESOLVED] CASE Statment in SQL WHERE Clause
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 10:36 AM
#12
Re: [RESOLVED] CASE Statment in SQL WHERE Clause
-
Nov 27th, 2006, 11:01 AM
#13
Thread Starter
Giants World Champs!!!!
Re: [RESOLVED] CASE Statment in SQL WHERE Clause
Nope spoke to soon, I came up with this workaround until I can figure it out:
VB Code:
Declare @UNIT NVARCHAR(50)
Declare @CASENUM NVARCHAR(50)
SET @UNIT = Null --'Group 32'
Set @CASENUM = Null --'C01-0020'
IF @CASENUM is Not Null
SELECT INC.Unit, INC.CASENUM, PCR.PRIMARY_CASE_DT, INC.STATUS
FROM IADATA.dbo.INCIDENTS INC INNER JOIN eGrpMgmt.dbo.CASES CS ON INC.CASENUM = CS.CASENUM
INNER JOIN IADATA.IA_ADM.PRIMARY_CASE_REC PCR ON PCR.PC_INCNUM = INC.INCNUM
Where Inc.CaseNum = @CaseNum
ORDER BY INC.UNIT
IF @UNIT is Not Null
SELECT INC.Unit, INC.CASENUM, PCR.PRIMARY_CASE_DT, INC.STATUS
FROM IADATA.dbo.INCIDENTS INC INNER JOIN eGrpMgmt.dbo.CASES CS ON INC.CASENUM = CS.CASENUM
INNER JOIN IADATA.IA_ADM.PRIMARY_CASE_REC PCR ON PCR.PC_INCNUM = INC.INCNUM
Where Inc.Unit = @UNIT and INC.STATUS <> 'Completed'
ORDER BY INC.UNIT
IF @CASENUM is Null AND @UNIT is Null
SELECT INC.UNIT, INC.CASENUM, PCR.PRIMARY_CASE_DT, INC.STATUS
FROM IADATA.dbo.INCIDENTS INC INNER JOIN eGrpMgmt.dbo.CASES CS ON INC.CASENUM = CS.CASENUM
INNER JOIN IADATA.IA_ADM.PRIMARY_CASE_REC PCR ON PCR.PC_INCNUM = INC.INCNUM
Where INC.STATUS <> 'Completed'
ORDER BY INC.UNIT
A little cumbersome but it works for me
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Nov 27th, 2006, 11:06 AM
#14
Re: [RESOLVED] CASE Statment in SQL WHERE Clause
Mark - that method has it's benefits...
The QUERY OPTIMIZER gets a much simpler WHERE clause to analyze.
It's probably easier to maintain in the future.
There is the downside of having to keep the three select's the same - but otherwise...
-
Nov 27th, 2006, 12:57 PM
#15
Thread Starter
Giants World Champs!!!!
Re: [RESOLVED] CASE Statment in SQL WHERE Clause
Thanks for your help again Steve.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|