sqlserver函數(shù)存儲過程游標以及while和if語句的嵌套使用_第1頁
sqlserver函數(shù)存儲過程游標以及while和if語句的嵌套使用_第2頁
全文預(yù)覽已結(jié)束

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、sqlserver函數(shù),存儲過程,游標以及whileif語句的嵌套使編寫這個存儲過程的主要原因是太懶,不愿意每天往表做數(shù)據(jù),所以就寫了他。不僅僅是學(xué)習(xí),也是的鍛煉。USE oaerpGO/* Object: StoredProcedure dbo.s_Get_AutoYh Script Date: 02/11/2015 17:17:35 */SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO- =- Author: - Create date: - Description: - =ALTER PROCEDURE dbo.s_Get_AutoYhsdtm

2、datetime=null,-開始的時間edtm datetime=null,-結(jié)束的時間DepoID char(10)=null,UserID char(20)=null,type varchar(30)=null,- 影響的數(shù)num int=null outputAS-般藥品養(yǎng)護記錄(ybyh)if (type=ybyh)BEGIN-創(chuàng)建張臨時表select a.* into #tmpyh from dbo.GetGoodsBatchListUnReal(DepoID,) a inner join ggoods b on a.goodsid=b.goodsidleft join gGood

3、sExt c on b.goodsid =c.goodsidwhere a.amount 0and b.catid like 01%and c.storeid !=07order by a.effdatedeclare days int -計算天數(shù)declare count int -統(tǒng)計要養(yǎng)護的數(shù)據(jù)declare Idx intdeclare n int -記錄每天需要插的數(shù)據(jù)declare m int -記錄執(zhí)的天數(shù)declare Doccode varchar(30)declare GoodsID char(10)declare Amount decimal(18,6)declare B

4、atchNo varchar(40)declare EffDate datetimedeclare StockArea char(2)declare Station char(2)declare CheckResult char(2)declare DoMethod char(2)declare MakID char(10)declare MakDtm datetimedeclare MakDtm datetime- 開始和結(jié)束時間的差就是本次需要添加養(yǎng)護記錄天數(shù)set days = datediff(day,sdtm,edtm)if days = 0beginset days = 1end-

5、得到總的記錄數(shù),即當(dāng)前店所有的品種select count = count(*) from #tmpyh-設(shè)置每天的記錄條數(shù),向下取整。set n=floor(count/30)set m=1-開始事務(wù)-begin tranbegin tryset makdtmwhile(days0)begindeclare ybyh cursor global- 定義個游標read_onlyfor select goodsid,batchno,effdate,amount from #tmpyhopen ybyh-如果執(zhí)的天數(shù)在輸?shù)臅r間范圍內(nèi)-while(m0)-begin-if not exists(se

6、lect * from zGspyhRecord where goodsid and isnull(batchno,)=isnull(batchno,) and isnull(effdate,)=isnull(effdate,)-beginselect stockarea = case a.otcidwhen 01 then 00when 01 then 00when 02 then 01when 03 then 02endfrom ggoods a where goodsidselect Idx=isnull(max(Idx),0)+1 from zGspyhRecord where-設(shè)置時

7、間和養(yǎng)護員信息set MakID=UserID-select DATEADD(dd,5550,2015-02-12 11:11:13)insert into zGspyhRecord(DocCode, DepoID, Idx, GoodsID, Amount,BatchNo, EffDate, StockArea, Station, CheckResult,DoMethod, MakID, MakDtm,CheID, CheDtm)values(DocCode,DepoID,Idx,GoodsID,Amount,BatchNo,EffDate,stockarea,00,00,03,MakID,

8、MakDtm,null,nset n=n-1-在秒上加上idx序號,作為新的時間的秒set Idx=Idx+1set MakDtm=DATEADD(SS,Idx,MakDtm)print(today had +str(n) )if n=0beginexec s_Get_DocCodeSerial_ByTradeID TradeID=60,depoID=DepoID,DocCode=DocCode outputset MakDtm=DATEADD(DD,1,MakDtm)set days -1set n=floor(count/30)if(days%30=0)beginprint(days:+str(days)breakendend-end- 執(zhí)游標,獲得數(shù)據(jù),直到讀取所有的數(shù)據(jù)集合fetch next from ybyh into goodsid,batchno,effdate,amount-end-END-set m=m+1 -設(shè)置天數(shù)增加1天-set MakDtm=DATEADD(DD,1,MakDtm)ENDclose ybyh - 關(guān)閉游標deall

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

最新文檔

評論

0/150

提交評論