Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Dynamic Case Statement Expand / Collapse
Author
Message
Posted Tuesday, February 05, 2013 6:23 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 05, 2013 8:56 PM
Points: 3, Visits: 22
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
Post #1416195
Posted Tuesday, February 05, 2013 9:08 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:07 PM
Points: 2,340, Visits: 3,167
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)





No loops! No CURSORs! No RBAR! Hoo-uh!

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?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1416218
Posted Tuesday, February 05, 2013 9:59 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 05, 2013 8:56 PM
Points: 3, Visits: 22
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
Post #1416232
Posted Tuesday, February 05, 2013 10:10 PM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 6:07 PM
Points: 2,340, Visits: 3,167
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.



No loops! No CURSORs! No RBAR! Hoo-uh!

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?

Need to UNPIVOT? Why not CROSS APPLY VALUES instead?
Since random numbers are too important to be left to chance, let's generate some!
Are you too recursively challenged?
Splitting strings based on patterns can be fast!
Post #1416234
Posted Tuesday, February 05, 2013 10:31 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, March 05, 2013 8:56 PM
Points: 3, Visits: 22
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
Post #1416239
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse