@@@@@@@@@@SQL语句@@@@@@@@@@

share1011 2006-07-25 09:24:07
我定义了4个变量,
@petition_date1 datetime,--开始时间
@petition_date2 datetime,--结束时间
@petition_mode char(1),
@petition_type char(1),
@state char(1)
我希望这4个变量用来查询时,如果哪个为空就把该条件去掉,就是说如果@state的值是空,那么查询条件中就不用@state做查询条件。如果都为空时,就查询出所有数据。我是这样写的:
if @petition_date1 ='' and @petition_date2=''
begin
select state,petition_id,petition_date,petition_mode,petition_type,consumer_name,consumer_city,petition_pro,petition_content from pet_records1
where petition_mode like '%'+@petition_mode+'%' and petition_type like '%'+@petition_type+'%' and state like'%'+@state+'%'
end
else if @petition_date1 <>'' and @petition_date2<>''
begin
select state, petition_id,petition_date,petition_mode,petition_type,consumer_name,consumer_city,petition_pro,petition_content from pet_records1
where petition_mode like '%'+@petition_mode+'%' and petition_type like '%'+@petition_type+'%' and state like'%'+@state+'%'
and ( petition_date between @petition_date1 and @petition_date2 )
end
可是什么都查不出来:(
...全文
161 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
昵称被占用了 2006-07-25
  • 打赏
  • 举报
回复
还有
if @petition_date1 ='' and @petition_date2=''
datetime类型不能这样判断

最好把参数位置调一下

@petition_mode char(1),
@petition_type char(1),
@state char(1),
@petition_date1 datetime=null, --开始时间
@petition_date2 datetime=null --结束时间


if @petition_date1 is null and @petition_date2 is null判断




share1011 2006-07-25
  • 打赏
  • 举报
回复
嘿嘿,谢谢了
LouisXIV 2006-07-25
  • 打赏
  • 举报
回复
Please confirm the values of parameters are not null

because null+char=null
昵称被占用了 2006-07-25
  • 打赏
  • 举报
回复
把参数char(1)改为varchar(1)

34,590

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧