Oracle 在这种情况下,参数是如何传递的
我在oracle中有一个程序Oracle 在这种情况下,参数是如何传递的,oracle,procedure,Oracle,Procedure,我在oracle中有一个程序 CREATE OR REPLACE PROCEDURE ReportCalculate ( param_acTypeId in NUMBER ) AS sqlstr VARCHAR2(500); result NUMBER; BEGIN sqlstr:='select count(1) from table1 where 1=1 and AC_TYPE_LEVEL1_ID=:acTypeId'; execute
CREATE OR REPLACE
PROCEDURE ReportCalculate
(
param_acTypeId in NUMBER
)
AS
sqlstr VARCHAR2(500);
result NUMBER;
BEGIN
sqlstr:='select count(1) from table1 where 1=1 and AC_TYPE_LEVEL1_ID=:acTypeId';
execute immediate sqlstr into result using param_acTypeId;
DBMS_OUTPUT.PUT_LINE(result);
END;
但有时我想查询所有的数据,sql是这样的
从表1中选择计数(1),其中1=1,AC_类型_级别1_ID=AC_类型_级别1_ID
,
那么参数应该如何传递,或者param_acTypeId应该有任何默认值?是否只有在缝合sql中才能判断它?一种典型的方法是接受
NULL
表示“全部”:
我应该注意,这个版本禁止使用索引。如果性能是一个问题,则使用两个查询:
if param_acTypeId is null then
sqlstr := 'select count(1) from table1';
execute immediate sqlstr into result;
else
sqlstr := 'select count(1) from table1 where AC_TYPE_LEVEL1_ID = :acTypeId';
execute immediate sqlstr into result using param_acTypeId;
end if;
DBMS_OUTPUT.PUT_LINE(result);
典型的方法是接受
NULL
表示“全部”:
我应该注意,这个版本禁止使用索引。如果性能是一个问题,则使用两个查询:
if param_acTypeId is null then
sqlstr := 'select count(1) from table1';
execute immediate sqlstr into result;
else
sqlstr := 'select count(1) from table1 where AC_TYPE_LEVEL1_ID = :acTypeId';
execute immediate sqlstr into result using param_acTypeId;
end if;
DBMS_OUTPUT.PUT_LINE(result);
您不需要动态SQL。如果传入
NULL
,则它将统计所有行:
CREATE OR REPLACE PROCEDURE ReportCalculate (
param_acTypeId in NUMBER
)
AS
result NUMBER;
BEGIN
select count(1)
into result
from table1
where ( param_acTypeId IS NULL OR AC_TYPE_LEVEL1_ID = param_acTypeId );
DBMS_OUTPUT.PUT_LINE(result);
END;
您不需要动态SQL。如果传入
NULL
,则它将统计所有行:
CREATE OR REPLACE PROCEDURE ReportCalculate (
param_acTypeId in NUMBER
)
AS
result NUMBER;
BEGIN
select count(1)
into result
from table1
where ( param_acTypeId IS NULL OR AC_TYPE_LEVEL1_ID = param_acTypeId );
DBMS_OUTPUT.PUT_LINE(result);
END;