高分求解,关于PB前端處理SQL游标的問題
作者:未知 来源:网络 更新时间:2018/5/16
以前用的是sybase 的ASA , 現在改用SQL,但發現不會用游標了.
比如以下這個前端函數,在使用ASA數據庫時是成功的,但在使用SQL時卻出錯(游標未打開):
(當然,兩個數據庫都存在同樣的存儲過程up_list)
// f_get_list(colname,tblname,wherestr)
string ls_liststr , ls_sql
ls_sql = "dbo.up_list @colname=?,@tblname=?, @wherestr=?"
DECLARE my_proc DYNAMIC PROCEDURE FOR SQLSA ;
PREPARE SQLSA FROM :ls_sql;
EXECUTE DYNAMIC my_proc USING :colname,:tblname,:wherestr ;
FETCH my_proc INTO :ls_liststr;
CLOSE my_proc ;
messagebox(ls_sql,ls_liststr)
return ls_liststr
為什麼呢?郁悶啊
------华软网友回答------
In addition to result sets, Sybase Systems 10.x and 11.x stored procedures may return a long integer return value and output parameters of any data type. After all of the result sets have been returned, PowerScript requires you to issue one final FETCH procedure_name INTO . . . statement to obtain these values. The order in which these values are returned is:
return value, output parm1, output parm2, ...
Example 1
The following stored procedure contains one input parameter (@deptno) and returns a result set containing employee names and salaries for that department. It also returns two output parameters (@totsal and @avgsal), and a return value that is the count of employees in the department.
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;
Example 2
The following PowerScript code fragment declares and executes the deptroster stored procedure, processes the result set, and then fetches the return value and output parameters.
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
// Execute successful. There is at least one
// result set. Loop to get the query result set
// from the table 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
// Issue an extra FETCH to get the Return Value
// and Output Parameters.
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
// Execute successful; no result set.
// Do not try to close.
MessageBox ("Execute Successful", "No result set")
CASE ELSE
MessageBox ("Execute Failed", &
string (SQLCA.sqldbcode) " = " &
SQLCA.sqlerrtext)
END CHOOSE
------华软网友回复------
up_list是存储过程吗? 如果是就不要用动态SQL语句执行了,直接声明SP来执行吧
declare procedure ....
------华软网友回复------
试试用这种方式声明sp
DECLARE sp_duration PROCEDURE FOR pr_date_diff_prd_ken
@var_date_1 = :ad_start,
@var_date_2 = :ad_end
execute .. 参数 ...
------华软网友回复------
暈,我怎麼就沒想到用這種聲明方式試一下呢,結貼,謝謝大家.
呵呵,我的分好賺吧
华软声明:本内容来自网络,如有侵犯您版权请来信指出,本站立即删除。
比如以下這個前端函數,在使用ASA數據庫時是成功的,但在使用SQL時卻出錯(游標未打開):
(當然,兩個數據庫都存在同樣的存儲過程up_list)
// f_get_list(colname,tblname,wherestr)
string ls_liststr , ls_sql
ls_sql = "dbo.up_list @colname=?,@tblname=?, @wherestr=?"
DECLARE my_proc DYNAMIC PROCEDURE FOR SQLSA ;
PREPARE SQLSA FROM :ls_sql;
EXECUTE DYNAMIC my_proc USING :colname,:tblname,:wherestr ;
FETCH my_proc INTO :ls_liststr;
CLOSE my_proc ;
messagebox(ls_sql,ls_liststr)
return ls_liststr
為什麼呢?郁悶啊
------华软网友回答------
In addition to result sets, Sybase Systems 10.x and 11.x stored procedures may return a long integer return value and output parameters of any data type. After all of the result sets have been returned, PowerScript requires you to issue one final FETCH procedure_name INTO . . . statement to obtain these values. The order in which these values are returned is:
return value, output parm1, output parm2, ...
Example 1
The following stored procedure contains one input parameter (@deptno) and returns a result set containing employee names and salaries for that department. It also returns two output parameters (@totsal and @avgsal), and a return value that is the count of employees in the department.
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;
Example 2
The following PowerScript code fragment declares and executes the deptroster stored procedure, processes the result set, and then fetches the return value and output parameters.
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
// Execute successful. There is at least one
// result set. Loop to get the query result set
// from the table 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
// Issue an extra FETCH to get the Return Value
// and Output Parameters.
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
// Execute successful; no result set.
// Do not try to close.
MessageBox ("Execute Successful", "No result set")
CASE ELSE
MessageBox ("Execute Failed", &
string (SQLCA.sqldbcode) " = " &
SQLCA.sqlerrtext)
END CHOOSE
------华软网友回复------
up_list是存储过程吗? 如果是就不要用动态SQL语句执行了,直接声明SP来执行吧
declare procedure ....
------华软网友回复------
试试用这种方式声明sp
DECLARE sp_duration PROCEDURE FOR pr_date_diff_prd_ken
@var_date_1 = :ad_start,
@var_date_2 = :ad_end
execute .. 参数 ...
------华软网友回复------
暈,我怎麼就沒想到用這種聲明方式試一下呢,結貼,謝謝大家.
呵呵,我的分好賺吧
华软声明:本内容来自网络,如有侵犯您版权请来信指出,本站立即删除。
- 上一篇文章: 好菜问题:PB9中居然不会设置数据窗口列项的背景颜色?
- 下一篇文章: 户口问题