where子句中的条件 [英] conditions in where clause

查看:177
本文介绍了where子句中的条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望以下查询根据@date和@abc传递返回记录。如果我没有为param @abc传递任何值,那么查询应该只考虑日期结果并根据该返回记录。做什么更改要做在下面的查询中?

目前我有以下查询..

select * from table where date = @ date and abc = @ abc

I want the following query to return records according to @date and @abc passed.If i did not pass any value for param @abc then query should just consider the date conndition and return records according to that.what changes to do in following query?
at present i have the following query..
select * from table where date=@date and abc=@abc

推荐答案

尝试:

Try:
SELECT * FROM table WHERE date=@date AND (@abc IS NULL OR abc=@abc)


你应该检查这样一个条件你的代码并执行一个不同的查询,即

You should check such a a condition in your code and execute a different query, namely
select * from table where date=@date



用于忽略'empty' abc 参数。


您可以使用if ... else ...条件来检查它是否有数据。



You can use if...else... condition to check whether it is having data or not.

if @date and @abc is not null
begin
    "Your select statement" Where date=@date and abc=@abc
end
if @date is not null and @abc is null
begin
    "Your select statement" Where date=@date 
end
if @date is null and @abc is not null
begin
    "Your select statement" Where abc=@abc
end


这篇关于where子句中的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆