第一條SQL語句執行沒有數據則執行第二條SQL語句,第二條SQL語句沒有數據則執行第三條語句
下面是SQL語句
第一句
select * into #TemPoraryBgoods from (
select distinct a.company,a.code,a.name,b.hyprice,a.comeFrom,finish=ISNULL(a.finish,''),c.smallfanwei,
dbo.GetDistancefromLnglat(113.273364,23.218945,lng,lat)/1000 as distance from Bgoods a,Bgoodsdtl b,shopperA c
where a.company=c.SEQ and a.code=b.code and a.company=b.company and (c.lng between 113.224569198891
and 113.322158801109) and (c.lat between 23.174029235794 and 23.263860764206) and a.KgCN='茶' and
b.display in ('1','2','3')
) a where distance<=5
select * from (
select ROW_NUMBER() over (order by distance) as 'rowNumber',* from #TemPoraryBgoods
) k where rowNumber between 1 and 20
select count(*) from #TemPoraryBgoods
第二句
select * into #TemPorarydhgoods from (
select distinct a.company,a.code,a.oldcode,a.name,b.hyprice,a.comeFrom,finish=ISNULL(a.finish,''),
dbo.GetDistancefromLnglat(113.273364,23.218945,lng,lat)/1000 as distance from prodh.dbo.G1982 a,prodh.dbo.G1982dtl b,
shq520.dbo.ghuser c where a.company=c.SEQ and a.code=b.code and (c.lng between 113.224569198891
and 113.322158801109) and (c.lat between 23.174029235794 and 23.263860764206) and a.name like '%茶%'
and b.dhdisplay='1'
) a where distance<=5
select * from (
select ROW_NUMBER() over (order by distance) as 'rowNumber',* from #TemPorarydhgoods
) k where rowNumber between 1 and 20
select count(*) from #TemPorarydhgoods
第三句
select distinct top 20 a.company,a.code,a.name,b.hyprice,a.comeFrom,finish=ISNULL(a.finish,''),c.smallfanwei,
dbo.GetDistancefromLnglat(113.273364,23.218945,lng,lat)/1000 as distance from Bgoods a,Bgoodsdtl b,shopperA c
where a.company=c.SEQ and a.code=b.code and a.company=b.company and b.display in ('1','2','3')
and (c.lng between 113.224569198891 and 113.322158801109) and (c.lat between 23.174029235794
and 23.263860764206) order by distance
第一條SQL語句執行沒有數據則執行第二條SQL語句,第二條SQL語句沒有數據則執行第三條語句 這是你要實現的功能吧。
我大致說下吧,
BEGING
第一句
exception when nodatefound then
begin
第二句
exception when nodatefound then
第三句
end;
end;
大概結構就是這樣。你去具體的寫完試試。當第一句沒數據時,會報異常,將其捕獲,並進行處理,也就是執行第二句。如果還是沒數據,繼續報異常,繼續處理。這是我的方法。請參考。