@@@@@@@@@@SQL语句@@@@@@@@@@
我定义了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
可是什么都查不出来:(