儲存過程根據業務場景自己摸索的寫法

2021-10-22 06:23:06 字數 3440 閱讀 9622

自己摸索的最low寫法

begin

declare v_htbh varchar

(255);

#上游合同編號

declare v_id integer

;#上游合同id

declare v_kbsj date

;declare no_more_products int

default0;

#declare mycursor cursor for select user_id from sys_user where dept_id=153;

#declare v_sxid select id from yqt_sxyhtdyb where xmid=(select xmmc from ms_fbht where syhtbh=v_id limit 1) and xyhtbh=(select htbh from ms_htxx_info where id=v_htbh) and htbh=(select htbh from ms_fbht where syhtbh=v_id limit 1);

declare mycursor cursor

forselect htbh,id from ms_htxx_info where request_id is

null

;#迴圈游標 上游合同的編號

-- 發展經營部合同台賬中的累計開票金額等於發票管理 ms_fpgl 中對應上游合同編號的 kpjey(開票金額元)累加

-- 累計收款金額等於合同收款確認單 ms_htskjs 對應的合同編號的收款金額(shje)合計 shje

-- 初期應收款等於合同金額

-- 末期應收賬款等於和合同金額-累計已收款金額

declare

continue

handler

fornot found set no_more_products =1;

open mycursor;

#開啟游標

repeat

#進入迴圈

#迴圈的游標賦值

fetch mycursor into v_htbh,v_id;

#賦值給變數

#-------------------------------

#插入上游合同

ifnot no_more_products then

#當no_more_products!=1繼續迴圈

insert

into yqt_fzjybhttz(htbh,htlb,htsx,sftzl,qdrq,htqsrq,htjzrq,htzj,yssj,yssx,sl,ljkpje,ljskje,qcyszk,qmysk)

values((

select htbh from ms_htxx_info where id=v_id)

,#合同編號

(select lbmc from ms_helb where id=

(select htlb from ms_htxx_info where id=v_id)),

#和類別

(select option_text from spms_option where table_name=

'ms_htxx_info'

and field_name=

'htsxnhw'

and option_val=

(select htsxnhw from ms_htxx_info where id=v_id )),

#合同屬性

(select option_text from spms_option where table_name=

'ms_htxx_info'

and field_name=

'htlb'

and option_val=

(select sftzl from ms_htxx_info where id=v_id)),

#是否投資類

(select htqdrq from ms_htxx_info where id=v_id)

,#合同簽訂日期

(select htksrq from ms_htxx_info where id=v_id)

,#合同起始日期

(select htjzrq from ms_htxx_info where id=v_id)

,#合同截至日期

(select htje from ms_htxx_info where id=v_id)

,#合同總價

null

,null

,#驗收時間,驗收手續

(select sl from ms_htxx_info where id=v_id)

,#稅率

(select

sum(ifnull(kpjey,0)

)from ms_fpgl where htbh=v_id and request_id is

null),

#累計開票金額

(select

sum(ifnull(shje,0)

)from ms_htskjs where htbh=v_id and request_id is

null),

#累計收款金額

(select htje from ms_htxx_info where id=v_id)

,#初期應收款((

select htje from ms_htxx_info where id=v_id)

-ifnull(

(select

sum(ifnull(shje,0)

)from ms_htskjs where htbh=v_id and request_id is

null),

0))#末期應收款 合同金額-已收款金額);

-- (select id from yqt_sxyhtdyb where xmid=(select xmmc from ms_fbht where syhtbh=v_id limit 1) and

-- xyhtbh=(select htbh from ms_htxx_info where id=v_htbh) and htbh=(select htbh from ms_fbht where syhtbh=v_id limit 1));

endif

;

until no_more_products =

1#結束迴圈

#--------------------------------

endrepeat

;close mycursor ;

end

只是為了記載本人寫儲存過程的印記,很low。歡迎指正

儲存過程與業務類實現業務的差異比較

以下比較不太全面,純粹是個人的理解。可能是針對前一篇文章的補充與說明 1 批量資料的處理比較 業務邏輯 單位a部門劃轉到b部門,業務規則是把a部門的100人的關聯單位改為b部門,同時在人員崗位變化子表裡增加一條變動記錄。業務實現 1 儲存過程實現 sp實現 兩個sql語句 insert into 崗...

根據儲存過程名建立對應的命令過程

using system using system.data sqlclient using system.data using system.collections using system.enterpriseservices namespace sev pant set private con...

自己除錯通過的儲存過程

create procedure getrecordfrompage tblname varchar 255 表名 fldname varchar 255 欄位名 pagesize int 10,頁尺寸 pageindex int 1,頁碼 iscount bit 0,返回記錄總數,非 0 值則返回...