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


Dynamic Case Statement


Dynamic Case Statement

Author
Message
deftone_tim
deftone_tim
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 73
Hello all, I'm having some issues with a query I have which includes a dynamic case statement. I realize having a dynamic case statement is not the best practice but I'm having trouble understanding why it doesn't return any results.


Basically what's happening is this:


DECLARE @EMP VARCHAR(MAX) = 'EMP1'
DECLARE @MGR VARCHAR(MAX) = ''''+'MGR1'+'''' + ',' + ''''+'MGR2'+''''
DECLARE @FILTER VARCHAR(MAX) = @MGR
DECLARE @VIEW INT = 1



SELECT * FROM dbo.EMPLOYEE AS EMP WHERE EMP.MGR IN (@FILTER)
In this statement I return 0 results


When I select the parameter values it returns:

SELECT @FILTER
RETURNS: 'MGR1','MGR2'


Of course if I hard code the return results it returns data
SELECT * FROM dbo.EMPLOYEE AS EMP WHERE EMP.MGR IN ('MGR1','MGR2')

And if I limit the parameter to just one name it returns data fine
DECLARE @MGR VARCHAR(MAX) = ''''+'MGR1'+''''

SELECT * FROM dbo.EMPLOYEE AS EMP WHERE EMP.MGR IN (@FILTER)




I also tried placing the results into a table but that did not work either

DECLARE @FILTERS TABLE (Name NVARCHAR(MAX))
INSERT INTO @FILTERS
SELECT @FILTER



SELECT * FROM dbo.EMPLOYEE AS EMP WHERE EMP.MGR IN (SELECT Name FROM @FILTER)

Just having trouble understanding what is causing this to not return. Any insight would be appreciated
dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16755 Visits: 6431
I believe you'll need to do something like this:


DECLARE @FILTERS TABLE (Name NVARCHAR(MAX))
INSERT INTO @FILTERS
SELECT 'MGR1' UNION ALL SELECT 'MGR2'

SELECT *
FROM dbo.EMPLOYEE AS EMP
WHERE EMP.MGR IN (SELECT Name FROM @FILTER)





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
deftone_tim
deftone_tim
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 73
The only issue is I don't know the number of Manager names to be selected as it will be based on user defined parameter
dwain.c
dwain.c
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16755 Visits: 6431
Try this then:


DECLARE @FILTER VARCHAR(8000) = 'MGR1,MGR2'

SELECT *
FROM dbo.EMPLOYEE AS EMP
WHERE EMP.MGR IN (SELECT Item FROM DelimitedSplit8K(@FILTER, ','))




You can find the DelimitedSplit8K FUNCTION here:
Tally OH! An Improved SQL 8K “CSV Splitter” Function

Note that DelimitedSplit8K is designed to work with VARCHAR(8000) and not NVARCHAR(MAX) as in your original code. You'd need to either modify it to use that data type (not recommended by the author) or use a different delimited string splitter. Unless you need the UNICODE characters, it's pretty unlikely the user would enter a 2GB parameter string.


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
deftone_tim
deftone_tim
Grasshopper
Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)Grasshopper (20 reputation)

Group: General Forum Members
Points: 20 Visits: 73
I don't currently have sufficient permissions to create functions but I might be able to get this in front of someone who can add this. Thanks for the article
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