ORACLE:使用可选参数创建ORACLE过程
在大多数asp.net应用程序中,页面带有过滤器是很常见的(通常有5个或5个以上的标准,例如:filter by:lastname、firstname、date等) 主要处理TSQL(SQLServer2000/2005)。我将在我的程序中处理标准,例如:ORACLE:使用可选参数创建ORACLE过程,oracle,plsql,optional-parameters,Oracle,Plsql,Optional Parameters,在大多数asp.net应用程序中,页面带有过滤器是很常见的(通常有5个或5个以上的标准,例如:filter by:lastname、firstname、date等) 主要处理TSQL(SQLServer2000/2005)。我将在我的程序中处理标准,例如: PARAMETERS Param1 VARCHAR(32), Param2 INT; SELECT columns FROM table WHERE ([Param1] IS NULL OR column1 = [Param1]
PARAMETERS Param1 VARCHAR(32), Param2 INT;
SELECT columns FROM table WHERE
([Param1] IS NULL OR column1 = [Param1])
AND
([Param2] IS NULL OR column2 = [Param2])
create or replace procedure optPar(a in number, b in number := null, c number := 999) is
begin
dbms_output.put_line(a || ', ' || b || ', ' || c);
end;
有人能告诉我如何用PL/SQL处理可选参数吗?谢谢,例如:
PARAMETERS Param1 VARCHAR(32), Param2 INT;
SELECT columns FROM table WHERE
([Param1] IS NULL OR column1 = [Param1])
AND
([Param2] IS NULL OR column2 = [Param2])
create or replace procedure optPar(a in number, b in number := null, c number := 999) is
begin
dbms_output.put_line(a || ', ' || b || ', ' || c);
end;
该过程需要a
参数:
SQL> exec optPar();
BEGIN optPar(); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'OPTPAR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL> exec optPar( a=> 1, c=> 3);
1, , 3
PL/SQL procedure successfully completed.
您可以省略b,c
:
SQL> exec optPar(1);
1, , 999
PL/SQL procedure successfully completed.
SQL> exec optPar(1, 2);
1, 2, 999
PL/SQL procedure successfully completed.
SQL> exec optPar(1, 2, 3);
1, 2, 3
PL/SQL procedure successfully completed.
在这种情况下,显式传递参数比不使用可选参数更为重要,以便轻松了解为每个参数指定的值:
SQL> exec optPar();
BEGIN optPar(); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'OPTPAR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL> exec optPar( a=> 1, c=> 3);
1, , 3
PL/SQL procedure successfully completed.
例如:
PARAMETERS Param1 VARCHAR(32), Param2 INT;
SELECT columns FROM table WHERE
([Param1] IS NULL OR column1 = [Param1])
AND
([Param2] IS NULL OR column2 = [Param2])
create or replace procedure optPar(a in number, b in number := null, c number := 999) is
begin
dbms_output.put_line(a || ', ' || b || ', ' || c);
end;
该过程需要a
参数:
SQL> exec optPar();
BEGIN optPar(); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'OPTPAR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL> exec optPar( a=> 1, c=> 3);
1, , 3
PL/SQL procedure successfully completed.
您可以省略b,c
:
SQL> exec optPar(1);
1, , 999
PL/SQL procedure successfully completed.
SQL> exec optPar(1, 2);
1, 2, 999
PL/SQL procedure successfully completed.
SQL> exec optPar(1, 2, 3);
1, 2, 3
PL/SQL procedure successfully completed.
在这种情况下,显式传递参数比不使用可选参数更为重要,以便轻松了解为每个参数指定的值:
SQL> exec optPar();
BEGIN optPar(); END;
*
ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00306: wrong number or types of arguments in call to 'OPTPAR'
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL> exec optPar( a=> 1, c=> 3);
1, , 3
PL/SQL procedure successfully completed.
差不多一样,差不多一样。