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


Avoiding IF/ELSE


Avoiding IF/ELSE

Author
Message
Chrissy321
Chrissy321
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1232 Visits: 4706
--I believe the DDL and my commented out notes and questions should explain what I am hoping to achieve

CREATE TABLE #Companies (CompanyID int)
INSERT INTO #Companies (CompanyID) VALUES (1)
INSERT INTO #Companies (CompanyID) VALUES (2)
INSERT INTO #Companies (CompanyID) VALUES (3)

CREATE TABLE #CompanyGroups (CompanyGroupID char(1), CompanyID int)
INSERT INTO #CompanyGroups (CompanyGroupID,CompanyID) VALUES ('A',1)
INSERT INTO #CompanyGroups (CompanyGroupID,CompanyID) VALUES ('A',2)

DECLARE @CompanyID int
DECLARE @CompanyGroupID char(1)

--Scenario 1
--@CompanyID is populated, CompanyGroupID is NULL

SET @CompanyID = 1
SET @CompanyGroupID = NULL

IF @CompanyID IS NOT NULL
BEGIN
SELECT * FROM #Companies WHERE CompanyID = @CompanyID
END

--Scenario 2
--@CompanyID is NULL, CompanyGroupID is populated

SET @CompanyID = NULL
SET @CompanyGroupID = 'A'

IF @CompanyID IS NULL
BEGIN
SELECT * FROM #Companies WHERE CompanyID IN(Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID)
END

--So then I can do if else
IF @CompanyID IS NOT NULL
BEGIN
SELECT * FROM #Companies WHERE CompanyID = @CompanyID
END
ELSE
BEGIN
SELECT * FROM #Companies WHERE CompanyID IN(Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID)
END

--SELECT * FROM #Companies
--SELECT * FROM #CompanyGroups
DROP TABLE #Companies
DROP TABLE #CompanyGroups


--The question: My select statement is complicated and I'd prefer not to duplicate it in each part of the if/else statement.
--Can I build a single SELECT statement that will determine if @CompanyID IS NOT NULL and select the appropriate records??
mickyT
mickyT
SSCommitted
SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)SSCommitted (1.6K reputation)

Group: General Forum Members
Points: 1570 Visits: 3317
It's a bit ugly ... but you could do something like

;with companyNotNull as (
SELECT * FROM #Companies WHERE CompanyID = @CompanyID
),
companyNull as (
Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID
)
SELECT *
FROM companyNotNull
WHERE @companyID is not null
UNION ALL
SELECT *
FROM companyNull
WHERE @companyID is null



Of course this replicates the if ... then ... else structure. If both values are set then the companyID takes precedence.
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7235 Visits: 6431
In addition to MickyT's suggestion, you could do a catch all query or the dynamic SQL method suggested in this article by Gail Shaw:
http://sqlinthewild.co.za/index.php/2009/03/19/catch-all-queries/

Watch out for SQL injection if the parameter values come from the UI if you use the dynamic SQL method though.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
subbu1
subbu1
Ten Centuries
Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)Ten Centuries (1K reputation)

Group: General Forum Members
Points: 1049 Visits: 695
PLS TRY BELOW CODE
CREATE TABLE #Companies (CompanyID int)
INSERT INTO #Companies (CompanyID) VALUES (1)
INSERT INTO #Companies (CompanyID) VALUES (2)
INSERT INTO #Companies (CompanyID) VALUES (3)

CREATE TABLE #CompanyGroups (CompanyGroupID char(1), CompanyID int)
INSERT INTO #CompanyGroups (CompanyGroupID,CompanyID) VALUES ('A',1)
INSERT INTO #CompanyGroups (CompanyGroupID,CompanyID) VALUES ('A',2)
DECLARE @CompanyID int
DECLARE @CompanyGroupID char(1)
--SET @CompanyID = 1
--SET @CompanyGroupID = NULL
SET @CompanyID = NULL
SET @CompanyGroupID = 'A'

SELECT * FROM #Companies WHERE CompanyID = (CASE WHEN @CompanyID IS NULL THEN CompanyID ELSE @CompanyID END)
AND CompanyID IN(Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = CASE WHEN @CompanyGroupID IS NULL THEN CompanyGroupID ELSE @CompanyGroupID END)
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7235 Visits: 6431
mickyT (11/15/2012)
It's a bit ugly ... but you could do something like

;with companyNotNull as (
SELECT * FROM #Companies WHERE CompanyID = @CompanyID
),
companyNull as (
Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID
)
SELECT *
FROM companyNotNull
WHERE @companyID is not null
UNION ALL
SELECT *
FROM companyNull
WHERE @companyID is null



Of course this replicates the if ... then ... else structure. If both values are set then the companyID takes precedence.


It occurred to me on lookback that this is going to fail if #Companies has more than one column or that column has a data type that is incompatible with CompanyID from #CompanyGroups.


My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
dwain.c
dwain.c
SSCertifiable
SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)SSCertifiable (7.2K reputation)

Group: General Forum Members
Points: 7235 Visits: 6431
I think this is the "normal" catch all query form for this.


SELECT *
FROM #Companies
WHERE (@CompanyID IS NULL OR CompanyID = @CompanyID) AND
(@CompanyGroupID IS NULL OR
CompanyID IN(Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID))





My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!

My thought question: Have you ever been told that your query runs too fast?

My advice:
INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?
The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.


Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Learn to understand recursive CTEs by example.
Splitting strings based on patterns can be fast!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
elgatitto
elgatitto
Say Hey Kid
Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)

Group: General Forum Members
Points: 712 Visits: 130
Additional questions. What results should be for this?

CREATE TABLE #Companies (CompanyID int)
INSERT INTO #Companies (CompanyID) VALUES (1)
INSERT INTO #Companies (CompanyID) VALUES (2)
INSERT INTO #Companies (CompanyID) VALUES (3)

CREATE TABLE #CompanyGroups (CompanyGroupID char(1), CompanyID int)
INSERT INTO #CompanyGroups (CompanyGroupID,CompanyID) VALUES ('A',1)
INSERT INTO #CompanyGroups (CompanyGroupID,CompanyID) VALUES ('A',2)

a.
SET @CompanyID = 1
SET @CompanyGroupID = 'A'
b.
SET @CompanyID = 1
SET @CompanyGroupID = 'B'
c.
SET @CompanyID = 3
SET @CompanyGroupID = 'A'
d.
SET @CompanyID = 3
SET @CompanyGroupID = 'E'
e.
SET @CompanyID = NULL
SET @CompanyGroupID = NULL
Unsure
elgatitto
elgatitto
Say Hey Kid
Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)Say Hey Kid (712 reputation)

Group: General Forum Members
Points: 712 Visits: 130
You could think about something like this:

SELECT Comp.*
FROM #Companies AS Comp
LEFT JOIN #CompanyGroups AS Gr ON Comp.CompanyID = Gr.CompanyID
WHERE Comp.CompanyID = @CompanyID
OR GR.CompanyGroupID = @CompanyGroupID


Sony Francis @EY
Sony Francis @EY
SSC-Enthusiastic
SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)SSC-Enthusiastic (107 reputation)

Group: General Forum Members
Points: 107 Visits: 293
SELECT *
FROM
#Companies
WHERE CompanyID = @CompanyID
OR
(
CompanyID IS NULL AND
CompanyID IN(Select CompanyID FROM #CompanyGroups WHERE CompanyGroupID = @CompanyGroupID)

)
Chrissy321
Chrissy321
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1232 Visits: 4706
>>Additional questions. What results should be for this?

The parameters will be validated through an user interface so I don't expect any of those conditions to exist.
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