在这种情况下参数如何传递
In this case how the parameters pass
我在 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 immediate sqlstr into result using param_acTypeId;
DBMS_OUTPUT.PUT_LINE(result);
END;
但有时我想查询所有的数据,sql看起来像这样
select count (1) from table1 where 1 = 1 and AC_TYPE_LEVEL1_ID = AC_TYPE_LEVEL1_ID
,
那么参数应该如何传递,或者param_acTypeId应该有什么默认值?难道只有在拼接的时候才判断sql吗?
一个典型的方法是接受 NULL
作为意思 "all":
sqlstr := 'select count(1) from table1 where AC_TYPE_LEVEL1_ID = :acTypeId or :acTypeId is 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;
我在 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 immediate sqlstr into result using param_acTypeId;
DBMS_OUTPUT.PUT_LINE(result);
END;
但有时我想查询所有的数据,sql看起来像这样
select count (1) from table1 where 1 = 1 and AC_TYPE_LEVEL1_ID = AC_TYPE_LEVEL1_ID
,
那么参数应该如何传递,或者param_acTypeId应该有什么默认值?难道只有在拼接的时候才判断sql吗?
一个典型的方法是接受 NULL
作为意思 "all":
sqlstr := 'select count(1) from table1 where AC_TYPE_LEVEL1_ID = :acTypeId or :acTypeId is 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;