Sid*_*med 17 sql sql-server stored-procedures sql-server-2008
我正在使用SQL Server 2008 Express,并且我有一个SELECT基于参数从表中执行的存储过程.我有nvarchar参数和int参数.
这是我的问题,我的where子句看起来像这样:
WHERE [companies_SimpleList].[Description] Like @What
AND companies_SimpleList.Keywords Like @Keywords
AND companies_SimpleList.FullAdress Like @Where
AND companies_SimpleList.ActivityId = @ActivityId
AND companies_SimpleList.DepartementId = @DepartementId
AND companies_SimpleList.CityId = @CityId
Run Code Online (Sandbox Code Playgroud)
此参数是由我的ASP.NET MVC 3应用程序的用户设置的过滤器值,并且int参数不能设置,所以其价值将是0.这是我的问题,存储过程将搜索谁拥有的项目0为CityId为例如,为此,它返回错误的结果.因此,如果int参数的值大于0,那么能够拥有动态where子句会很好.
提前致谢
Mah*_*mal 38
试试这个:
WHERE 1 = 1
AND (@what IS NULL OR [companies_SimpleList].[Description] Like @What )
AND (@keywords IS NULL OR companies_SimpleList.Keywords Like @Keywords)
AND (@where IS NULL OR companies_SimpleList.FullAdress Like @Where)
...
Run Code Online (Sandbox Code Playgroud)
如果任何参数的@what,@where被发送到存储过程NULL值,那么所述条件将被忽略.您可以使用0而不是null作为测试值然后它将是类似的@what = 0 OR ...