我目前正在处理以下查询中的一个小错误处理问题:
declare @DayCounter int
declare @rows int
set @DayCounter = -2
set @rows = 0
while @rows = 0
begin
select
coalesce(p.name, case when len(ol.action)>1 then substring(ol.action,1,40)+'<br />'+substring(ol.action,41,80) else ca.code end) as description
, convert(varchar,DateAdd(minute,
15 * ((60 * Datepart(hour, latest_payment_status_change) +
Datepart(Minute, latest_payment_status_change)+
Case When DatePart(second, latest_payment_status_change) < 30
Then 7 Else 8 End) / 15),
DateAdd(day, DateDiff(day, 0, latest_payment_status_change), 0)),126) as date_time
, sum(num_of_persons) as num_tickets
from tick_order o
join tick_orderline ol on ol.order_id=o.id
join (
select olt.id
from tick_orderline_type olt
join tick_case ca on ca.id=olt.case_id
join tick_client tcl on tcl.id = ca.client_id
where tcl.id = 'CLIENT_TEST'
union
select orderline_type_id
from rpt_client_orderline_type rcot
join tick_client tcl on tcl.id = rcot.client_id
where tcl.id = 'CLIENT_TEST'
union
select olt.id
from tick_orderline_type olt
join rpt_client_case rcc on rcc.case_id=olt.case_id
join tick_client cl on cl.id=rcc.client_id
where cl.id = 'CLIENT_TEST'
) olt2 on olt2.id=ol.orderline_type_id
join tick_orderline_type olt on olt.id=ol.orderline_type_id
join tick_case ca on ca.id=olt.case_id
join tick_client cl on cl.id=ca.client_id
join tick_user_case uc on uc.case_id=ca.id
join tick_user u on u.id=uc.user_id
left join tick_promotion_code pc on pc.id=o.promotion_code_id
left join tick_promotion p on p.id=pc.promotion_id
where o.latest_payment_status_change>=dateadd(day,@DayCounter,current_timestamp)
and ((o.latest_payment_status_code = 'ORDER_PAYMENT_OK')
or (o.latest_payment_status_change > dateadd(minute,30,current_timestamp)
and o.latest_payment_status_code = 'ORDER_PAYMENT_WAITING' ))
group by
coalesce(p.name, case when len(ol.action)>1 then substring(ol.action,1,40)+'<br />'+substring(ol.action,41,80) else ca.code end)
, convert(varchar,DateAdd(minute,
15 * ((60 * Datepart(hour, latest_payment_status_change) +
Datepart(Minute, latest_payment_status_change)+
Case When DatePart(second, latest_payment_status_change) < 30
Then 7 Else 8 End) / 15),
DateAdd(day, DateDiff(day, 0, latest_payment_status_change), 0)),126)
order by 1,2
set @DayCounter = @DayCounter - 1;
if @@rowcount <> 0
begin
set @rows = @@rowcount;
print @DayCounter;
end;
end;
检索到的数据用于显示过去两天每个销售类型的销售额的图表。我现在要做的是:如果过去两天没有销售(@@ rowcount = 0),请每次再检查一次,直到找到数据。 现在的查询返回类似这样的内容(我尝试使图像正常工作,但我不知道如何;改为使用链接):
https://www.dropbox.com/s/0hculuegrc88c69/SQL_Result.png?dl=0
并且它不会停止,因为由于某种原因,@rows变量保持为0,尽管查询明确返回行。即使使用 print @@ rowcount ,也会返回行。
所以我该如何解决这个问题?我应该使用完全不同的方法吗?
-Zubaja
答案 0 :(得分:0)
我似乎并不完全明白雇主的意思。他不想在SQL中进行错误处理,而是在网站上:当过去两天没有找到数据时,只需显示一个空图。