[英]The dynamic sql in the stored procedure
I would like to pass in a dynamic column name to calculate its standard deviation,in oracle, the following is the code: 我想传入一个动态列名称以计算其标准偏差,在oracle中,以下是代码:
CREATE OR REPLACE
PROCEDURE ReportCalculate
(
param_columnName IN VARCHAR2 DEFAULT 'COMPUTED_CP_MLIFTOFF_KNOT9OR10'
)
AS
sqlstr VARCHAR2(500);
result NUMBER;
BEGIN
sqlstr:='select stddev(:col) from LIDISUDUXIAO where 1=1 and NO_10_LIMITSIGN_DEPART_ID<>0';
execute immediate sqlstr into result using param_columnName;
DBMS_OUTPUT.PUT_LINE(result);
END;
I call it with the default parameters,the error message is: 我用默认参数调用它,错误消息是:
Procedure execution failed
ORA-01722: invalid number
ORA-06512: at "AGS.REPORTCALCULATE", line 10
ORA-06512: at line 1
How can i solve it? 我该如何解决?
A column name cannot be used as parameter of a query. 列名不能用作查询的参数。 You need to edit the sqlstr variable to include the provided param_columnName in the query itself before executing it:
您需要编辑sqlstr变量,以在执行查询之前将提供的param_columnName包括在查询中:
CREATE OR REPLACE
PROCEDURE ReportCalculate
(
param_columnName IN VARCHAR2 DEFAULT 'COMPUTED_CP_MLIFTOFF_KNOT9OR10'
)
AS
sqlstr VARCHAR2(500);
result NUMBER;
BEGIN
sqlstr:='select stddev(' || param_columnName || ') from LIDISUDUXIAO where 1=1 and NO_10_LIMITSIGN_DEPART_ID<>0';
execute immediate sqlstr into result;
DBMS_OUTPUT.PUT_LINE(result);
END;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.