如何在PB脚本当中获取存储过程的返回值

本文拟以SYBASE ASE 10.X和11.X数据库为例,说明如何在PB脚本当中获取存储过程的返回值。作为一个存储过程,其输出的结果数据可能包括三类:SELECT结果集、RETURN结果、OUTPUT参数。尽管输出方式众多,但PB脚本仅仅借助简单的FETCH…INTO…语句即可全部获取这些输出数据,具体方式如下:


(一)在SYBASE ASE 10.X和11.X数据库当中创建一个存储过程deptroster,其有一个输入参数@deptno、两个输出参数@totsal 和 @avgsal、一个RETURN值@number_of_emps以及包含职员姓名和工资的SELECT结果集,可见除了输入参数@deptno外,其他均为输出数据,我们需要在PB脚本中获取,具体代码如下:


 


CREATE PROCEDURE deptroster @deptno integer,


@totsal double precision output,


       @avgsal double precision output


AS


DECLARE @number_of_emps integer


SELECT emp_fname, emp_lname, salary FROM employee


       WHERE dept_id = @deptno


SELECT @totsal = sum(salary),


       @avgsal = avg(salary),


       @number_of_emps = COUNT(*) FROM employee


       WHERE dept_id = @deptno


RETURN @number_of_emps;


 


(二)PB脚本当中我们需要捕获SELECT结果集、RETURN值和两个输出参数,其输出顺序也是按照“SELECT结果集、RETURN值、输出参数”顺序输出,具体代码如下:


 


integer fetchcount = 0


long    lDeptno, rc


string  fname, lname


double  dSalary, dTotSal, dAvgSal


lDeptno = 100


 


//此处声明存储过程名称


DECLARE deptproc PROCEDURE FOR


       @rc = dbo.deptroster


       @deptno = :lDeptno,


       @totsal = 0 output,


       @avgsal = 0 output


USING SQLCA;


 


//此处开始执行存储过程


EXECUTE deptproc;


 


//判断执行结果


CHOOSE CASE SQLCA.sqlcode


CASE 0


       //如果返回0则表示执行成功,至少存在一个SELECT结果集


       //借助LOOP循环开始捕获这个SELECT结果集


       DO


              FETCH deptproc INTO :fname, :lname, :dSalary;


              CHOOSE CASE SQLCA.sqlcode


              CASE 0


                     fetchcount++


              CASE 100


                     MessageBox ("End of Result Set",  &


                            string (fetchcount) " rows fetched")


              CASE -1


                     MessageBox ("Fetch Failed",  &


                            string (SQLCA.sqldbcode) " = "  &


SQLCA.sqlerrtext)


              END CHOOSE


       LOOP WHILE SQLCA.sqlcode = 0


 


// 再单独执行一次FETCH语句以获取RETURN值和OUTPUT参数


       FETCH deptproc INTO :rc, :dTotSal, :dAvgSal;


       CHOOSE CASE SQLCA.sqlcode


       CASE 0


              MessageBox ("Fetch Return Value and Output"  &


                     "Parms SUCCESSFUL", "Return Value is: "  &


                     string (rc)  &


                     "~r~nTotal Salary: " string (dTotSal)  &


                     "~r~nAverage Sal:  " string (dAvgSal))


       CASE 100


              MessageBox ("Return Value and Output Parms"  &


                     "NOT FOUND", "")


       CASE ELSE


              MessageBox ("Fetch Return Value and Output"  &


"Parms FAILED", "SQLDBCode is "  &


                     string (SQLCA.sqldbcode) " = "  &


                     SQLCA.sqlerrtext)


       END CHOOSE


 


//此处关闭存储过程


CLOSE deptproc;


CASE 100


 


// 如果返回100,则表示没有返回结果集.


       // 此时不需要单独执行CLOSE语句.


       MessageBox ("Execute Successful", "No result set")


 


CASE ELSE


//其他情况则表示存储过程执行失败,提示用户即可


MessageBox ("Execute Failed",  &


              string (SQLCA.sqldbcode) " = "  &


              SQLCA.sqlerrtext)


 


END CHOOSE


pb调用存储过程中返回值:

PB   中如下调用 
String   ls_Sql; 

ls_Sql   =   Space(   2000   ); 
DECLARE lpro_test   PROCEDURE   FOR   spc_test   @strsql   =   :ls_sql   OUTPUT; 
SQLCA.AutoCommit   =   TRUE 
EXECUTE   lpro_test; 
SQLCA.AutoCommit   =   FALSE 
Fetch   lpro_test   INTO   :ls_sql; 


工作中的实际应用例子:

decimal ldc_monthly_income,ldc_year_end_award
decimal ldc_monthly_income_tax,ldc_year_end_award_tax,ldc_tax_percent
integer li_income_precision,li_fetchcount = 0


decimal ldc_monthly_mm, ldc_year_end_yy, ldc_tax_mm, ldc_tax_yy, ldc_tax_all, ldc_tax_save, ldc_monthly_income_rate


ldc_monthly_income = dec(em_monthly_income.text)
ldc_year_end_award = dec(em_year_end_award.text)
li_income_precision = integer(ddlb_income_precision.text)
if li_income_precision > 10000 OR li_income_precision < 1 then
MessageBox('提示','月收入计算递增精度错误!')
return
end if
if (ldc_monthly_income*12 + ldc_year_end_award) >= 900000 and li_income_precision < 10 then
MessageBox('提示','总计算金额太高,请将计算递增精度调至10元以上!')
return
end if
ldc_tax_percent = dec(em_tax_percent.text)
em_all_income.text = string(ldc_monthly_income*12 + ldc_year_end_award,'#,##0.00')
//messagebox('',string(ldc_monthly_income,'#,##0.00'))
dw_best_tax_calc_list.reset()
em_month_tax_percent.text = "0"
em_year_end_tax_percent.text = "0"
em_all_tax_percent.text = "0"


DECLARE best_tax_calc PROCEDURE FOR p_best_tax_calculate
   @monthly_income = :ldc_monthly_income,
@year_end_award = :ldc_year_end_award,
@income_precision = :li_income_precision,
@tax_percent = :ldc_tax_percent,
@mothly_income_tax = :ldc_monthly_income_tax OUTPUT,
@year_end_award_tax = :ldc_year_end_award_tax OUTPUT
USING SQLCA;
EXECUTE best_tax_calc;
If Sqlca.sqlCode >=0 Then
DO
FETCH best_tax_calc INTO 
:ldc_monthly_mm,:ldc_year_end_yy,:ldc_tax_mm,:ldc_tax_yy,
:ldc_tax_all,:ldc_tax_save,:ldc_monthly_income_rate;
CHOOSE CASE SQLCA.sqlcode
CASE 0
li_fetchcount++
dw_best_tax_calc_list.insertrow(li_fetchcount)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_monthly_mm',ldc_monthly_mm)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_year_end_yy',ldc_year_end_yy)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_tax_mm',ldc_tax_mm)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_tax_yy',ldc_tax_yy)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_tax_all',ldc_tax_all)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_tax_save',ldc_tax_save)
dw_best_tax_calc_list.setitem(li_fetchcount,'c_monthly_income_rate',ldc_monthly_income_rate)
if li_fetchcount = 1 then
em_min_tax.text = string(ldc_tax_all,'#,##0.00')
end if
CASE 100
// MessageBox("End of Result Set",&
// string(li_fetchcount)+" rows fetched")
if li_fetchcount = 0 then
MessageBox("提示","您所录入的金额不具备个人所得税最优化计算的意义~r~n~r~n或者已经是最优化的个人所得税方案了!")
end if
CASE -1
MessageBox("Fetch Failed",  &
string(SQLCA.sqldbcode)+" = "+  &
SQLCA.sqlerrtext)
END CHOOSE
LOOP WHILE SQLCA.sqlcode = 0
FETCH best_tax_calc INTO :ldc_monthly_income_tax, :ldc_year_end_award_tax;
CHOOSE CASE SQLCA.sqlcode
CASE 0
em_monthly_tax.text = string(ldc_monthly_income_tax,'#,##0.00')
em_year_end_tax.text = string(ldc_year_end_award_tax,'#,##0.00')
em_all_tax.text = string(ldc_monthly_income_tax * 12 + ldc_year_end_award_tax, '#,##0.00')
if ldc_monthly_income > 0 then
em_month_tax_percent.text = string(round(ldc_monthly_income_tax/ldc_monthly_income * 100,2),'#,##0.00')
end if
if ldc_year_end_award > 0 then
em_year_end_tax_percent.text = string(round(ldc_year_end_award_tax/ldc_year_end_award * 100,2),'#,##0.00')
end if
if ldc_monthly_income*12 + ldc_year_end_award > 0 then
em_all_tax_percent.text = string(round((ldc_monthly_income_tax * 12 + ldc_year_end_award_tax)/(ldc_monthly_income * 12 + ldc_year_end_award) * 100,2),'#,##0.00')
end if
if li_fetchcount = 0 then
em_min_tax.text = string(ldc_monthly_income_tax * 12 + ldc_year_end_award_tax,'#,##0.00')
end if
// MessageBox("Fetch Return Value and Output", "Return Value is: "+&
//                     "~r~nTotal Salary: " +string(ldc_monthly_income_tax) + &
//                     "~r~nAverage Sal:  " +string(ldc_year_end_award_tax))
CASE 100
MessageBox("Return Value and Output Parms NOT FOUND", "")




  CASE ELSE
MessageBox("Fetch Return Value and Output"  +&
"Parms FAILED", "SQLDBCode is "  +&
string(SQLCA.sqldbcode)+ " = "  +&
SQLCA.sqlerrtext)
END CHOOSE


//  commit;
Else
  Rollback;
End if


CLOSE best_tax_calc;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值