前面寫過一篇 通過乙個實際的例子學習oracle儲存過程,現在再來一篇 通過乙個實際的例子學習sqlserver儲存過程。
所謂應用而學。
--定義儲存過程
create
procedure ******xx_p
(
--傳遞引數
@ym char(6)
)
as--定義變數,@xx表示區域性變數,@@xx表示全域性變數。定義多個變數用","號分割
declare @ym_ln char(6)
declare @cpcode char(10),
@cpname char(50),
@swcode char(10),
@swname char(50),
@czgscode char(10),
@czgscode_ char(10),
@czgsname char(50),
@qylx char(2),
@qyxz char(30)
declare @tdcode char(10),
@sb_amt numeric,
@sb_ln_amt numeric,
@sh_amt numeric,
@sh_ln_amt numeric,
@ts_amt numeric,
@ts_ln_amt numeric
declare @ybmy numeric(12,6),
@jljg numeric(12,6),
@other numeric(12,6),
@ybmy_ln numeric(12,6),
@jljg_ln numeric(12,6),
@other_ln numeric(12,6)
declare @rowcount int
--刪除表中現有符合ym=@ym的資料
delete
from cs_******xx where ym=@ym
--給變數賦初值,用到了cast,substring函式。cast用於型別轉換,substring使用者擷取字串
set @ym_ln=cast((substring(@ym,1,4)-1) as
char(4)) +substring(@ym,5,2)
--宣告乙個游標
declare cur_***x cursor
forselect cpcode.code as cpcode ,cpcode.name
as cpname ,cpcode.swcode as swcode,swcode.name
as swname,cs_swcode_czgs.czgs as czgscode,cpcode.qylx as qylx from cpcode
left
join cs_swcode_czgs on cpcode.swcode=cs_swcode_czgs.swcode
left
join swcode on cpcode.swcode=swcode.code where cpcode.swcode<>''
--打來游標
open cur_***x
--取游標中第一行記錄並且寫入變數。
fetch
next
from cur_***x
into @cpcode,@cpname,@swcode,@swname,@czgscode,@qylx
--當@@fetch_status = 0即取出了有效行時處理,用到了while語句,結構while xx begin *** end
while @@fetch_status = 0
begin
--用select語句給變數賦值
select @czgscode_=czgs from cs_cpcode_czgs where cpcode=@cpcode
--if語句,完整結構if xx begin *** end
if @czgscode_ is
notnull
set @czgscode=@czgscode_
select @czgsname=name
from cs_czgs where code=@czgscode
--if else結構,完整結構 if xx begin *** else ***x end
if @qylx='11'
set @qyxz='內資企業'
else
set @qyxz='外商投資企業'
--用select語句給變數賦值,用到了isnull函式。
select @sb_amt=isnull(sum(mdtse),0) from mdtsb where sb_ym=@ym and cpcode=@cpcode
select @sb_ln_amt=isnull(sum(mdtse),0) from mdtsb where sb_ym=@ym_ln and cpcode=@cpcode
-- --省略n行類似賦值語句
-- --goto語句,跳轉到insertmodule
goto insertmodule
-- --省略n行同類處理語句
-- insertmodule:
--用is null表示式判斷是否為null
if @sb_amt is
null
set @sb_amt =0
if @sb_ln_amt is
null
set @sb_ln_amt =0
if @sh_amt is
null
set @sh_amt =0
if @sh_ln_amt is
null
set @sh_ln_amt =0
if @ts_amt is
null
set @ts_amt =0
if @ts_ln_amt is
null
set @ts_ln_amt =0
--插入一般**
select @ybmy=zb from cs_scqybl where tdcode='一般**' and ym=@ym and cpcode=@cpcode
select @ybmy_ln=zb from cs_scqybl where tdcode='一般**' and ym=@ym_ln and cpcode=@cpcode
if @ybmy is
null
begin
--在儲存過程中執行儲存過程
exec ***bl @cpcode,@ym
endif @ybmy_ln is
null
begin
exec ***xbl @cpcode,@ym_ln
endselect @jljg=zb from cs_scqybl where tdcode='進料加工' and ym=@ym and cpcode=@cpcode
select @jljg_ln=zb from cs_scqybl where tdcode='進料加工' and ym=@ym_ln and cpcode=@cpcode
select @other=zb from cs_scqybl where tdcode='其他' and ym=@ym and cpcode=@cpcode
select @other_ln=zb from cs_scqybl where tdcode='其他' and ym=@ym_ln and cpcode=@cpcode
--把上面各個步驟運算得到的值insert進表中cs_******xx
insert
into cs_******xx (xx,***,***x) values(vv,vvv,vvvv )
--取取游標中下一行記錄並寫入變數
fetch
next
from cur_***x into @cpcode,@cpname,@swcode,@swname,@czgscode,@qylx
--結束while迴圈
end--關閉游標
close cur_***x
--刪除游標
deallocate cur_***x
go
通過乙個實際的例子學習Oracle儲存過程
建立儲存過程 create or replace procedure xx p 引數in表示輸入引數,out表示輸入引數,型別可以使用任意oracle中的合法型別。is ym in char as 定義變數 vs msg varchar2 4000 錯誤資訊變數 vs ym beg char 6 起...
通過乙個實際的例子學習Oracle儲存過程
create or replace procedure xx p 引數in表示輸入引數,out表示輸入引數,型別可以使用任意oracle中的合法型別。is ym in char as 定義變數 vs msg varchar2 4000 錯誤資訊變數 vs ym beg char 6 起始月份 vs ...
通過乙個實際的例子學習Oracle儲存過程
建立儲存過程 create or replace procedure xx p 引數in表示輸入引數,out表示輸入引數,型別可以使用任意oracle中的合法型別。is ym in char as 定義變數 vs msg varchar2 4000 錯誤資訊變數 vs ym beg char 6 起...