如何在PB脚本当中获取存储过程的返回值
2012-07-29 22:42
330 查看
本文拟以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;
(一)在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;
相关文章推荐
- 如何在PB脚本当中获取存储过程的返回值(翻译)
- 如何在PB脚本当中获取存储过程的返回值(翻译) 选择自 oury 的 Blog
- 如何在asp.net中获取存储过程的返回值。
- SqlServer如何获取存储过程的返回值
- 如何获取存储过程的返回值和输出值
- 如何在asp.net中获取存储过程的返回值。
- SQL存储过程测试(8)——当待测存储过程没有返回值的时候 如何判断测试结果是否通过
- Enterprise Library 2.0系列:如何获取存储过程的output参数值?
- 『草稿』如何获取 SQL Server 存储过程的详细语法
- MyBaits:如何接收Mysql存储过程多个返回集
- ado.net获取存储过程的返回值
- 获取手机内外部存储空间的参数 && 如何决定一个apk的安装位置 && APK安装过程及原理详解
- SqlServer获取存储过程的返回值
- 在存储过程中如何使用另一个存储过程返回的结果集
- 如何从服务器上利用存储过程返回数据集
- C#获取存储过程的返回值
- shell脚本 如何调用Mysql的存储过程 解决方案
- oracle的存储过程如何返回结果集
- SSMS获取存储过程中的返回值和output值
- oracle存储过程如何返回list,并用jdbc调用