问题描述
- SQLSERVER存储过程,请各位帮忙看一下
-
第一条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;
大概结构就是这样。你去具体的写完试试。当第一句没数据时,会报异常,将其捕获,并进行处理,也就是执行第二句。如果还是没数据,继续报异常,继续处理。这是我的方法。请参考。
解决方案二:
希望你能把你的需求说出来
解决方案三:
我上面的就是代码啊。
时间: 2024-11-01 03:46:41