程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> 數據-SQLSERVER存儲過程,請各位幫忙看一下

數據-SQLSERVER存儲過程,請各位幫忙看一下

編輯:編程解疑
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;

大概結構就是這樣。你去具體的寫完試試。當第一句沒數據時,會報異常,將其捕獲,並進行處理,也就是執行第二句。如果還是沒數據,繼續報異常,繼續處理。這是我的方法。請參考。

etywu
SPE_Andy
etywu
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved