2013-12-17 114 views
1

在SQL中,我需要发送四个提示:其中一个提示值为'none'。如果没有,我希望它返回所有结果。基本上没有人说没有where子句。但是如何?提示忽略where子句

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

回答

1

如果我正确理解'没有'会在你@Prompt变量这应该工作。

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

而不是做的分裂两次,我会把这些值转换为#TEMP表,然后子查询。

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) 
) 
0
where charindex('none',@Prompt) > 0 
    or GL_FK in (Select Value from dbo.split(@Prompt,',')) 

我认为这解决了它。

当然,还有一个问题可能不是其他词的子串,也就是说。 nonewrecords ......所以它实际上取决于有关细节,即什么样的格式PROMT有1个值,更多的则值1, 如果它的,分隔的列表,然后做这个

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

是一个简单的解决方案