Domanda

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,','))
È stato utile?

Soluzione

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)
)

Altri suggerimenti

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

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top