如何改变'之间'和'和'条件在哪里条件 [英] how to change 'between' and 'and' condition in where condition

查看:95
本文介绍了如何改变'之间'和'和'条件在哪里条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

create procedure sp_get_report(@datefrom date=null,@dateto date=null,@customerfrom nvarchar(40)=null,@customerto nvarchar(40)=null,@salesrepfrom nvarchar(40)=null,@salesrepto nvarchar(40)=null)
as

begin

select total from employee where alldate between @datefrom and @dateto
and customer between @customerfrom and @customerto 
and sales between @salesrepfrom and @salesrepto

end







exec sp_get_report'2014-10-01',' 2014-10-03' - 如果我没有通过@ customerfrom,@ customerto,@ salesrepfrom,@ salesrepto,我会得到null。因为我使用了条件的可选参数。



所以如果任何一组和条件之间为空,那么特别是不应该使用。



我们需要在两者之间使用和条件。我猜它可以用任何其他方式使用案例。但我只是在学习过程中。



如果有人知道,请帮助我。




exec sp_get_report '2014-10-01','2014-10-03' -- if i did not pass @customerfrom,@customerto,@salesrepfrom,@salesrepto, i get null. becuase i used optional parameter for condition.

so if any set of between and condition is null, that particular betwenn and should not use.

we need use between and condition whatever is not null only. i guess it could be done using case any other way. but i am in learing process only.

if anyone knows, help me.

推荐答案

试试这个



SELECT

*

来自员工e

WHERE(e.FromDate > = @datefrom

或@datefrom IS NULL)

AND(e.ToDate< @dateto

或@dateto IS NULL)
try this

SELECT
*
FROM employee e
WHERE (e.FromDate >= @datefrom
OR @datefrom IS NULL)
AND (e.ToDate < @dateto
OR @dateto IS NULL)


如下所示:(alldate IS NULL或@datefrom和@dateto之间的对齐)


我只想添加一个条件语句,将日期设置为荒谬的范围。我讨厌使用大于,小于日期(由于某种原因)



即...

i would just add a conditional statement to set dates to an absurd range when they are missing. i hate using greater than, less than in dates (for some reason)

ie...
create procedure sp_get_report(@datefrom date=null,@dateto date=null,@customerfrom nvarchar(40)=null,@customerto nvarchar(40)=null,@salesrepfrom nvarchar(40)=null,@salesrepto nvarchar(40)=null)
as

begin
if @datefrom is null set @datefrom = '1/1/1900'
if @dateto is null set @dateto = '1/1/2050'
etc...
select total from employee where alldate between @datefrom and @dateto
and customer between @customerfrom and @customerto
and sales between @salesrepfrom and @salesrepto

end


这篇关于如何改变'之间'和'和'条件在哪里条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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