Question

In SQL I need to send four prompts: one of the prompt values will be 'none'. If it is none, I want it to return all results. Basically none is saying there is no where clause. But how?

Select ID_PK
From STG_TBL_DIM
WHERE GL_FK in (Select Value from dbo.split(@Prompt,','))
Was it helpful?

Solution

If I'm understanding correctly that 'none' will be in you @Prompt variable this should work.

WHERE (
GL_FK in (Select Value from dbo.split(@Prompt,',')) 
or 
'none' in (Select Value from dbo.split(@Prompt,','))
)

Instead of doing the split twice I would put these values into a #temp table and then subqueries.

Select 
    Value
into #Values 
from dbo.split(@Prompt,',')


...    
the rest of your query
...

WHERE (
GL_FK in (Select Value from #Values) 
or 
'none' in (Select Value from #Values)
)

OTHER TIPS

where charindex('none',@Prompt) > 0
   or GL_FK in (Select Value from dbo.split(@Prompt,',')) 

i think this solves it.

Of course, there is the issue of maybe none being an substring of other words, ie. nonewrecords... so it really depends about details, ie, what format Promt has for 1 value and more then 1 value, if its a , separated list then doing this

where charindex(',none,',','+@Prompt+',') > 0
   or GL_FK in (Select Value from dbo.split(@Prompt,',')) 

is a simple solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top