如何确定PL / SQL参数值是否默认?

时间:2009-05-19 12:32:00

标签: oracle plsql parameters default

假设我有一个PL / SQL存储过程,如下所示:

PROCEDURE do_something(foo VARCHAR2 DEFAULT NULL) IS
BEGIN
    /* Do something */
END;

现在,假设do_something以两种不同的方式调用:

/* Scenario 1: The 'foo' parameter defaults to NULL */
do_something();

/* Scenario 2: The 'foo' parameter is explicitly set to NULL */
do_something(foo => NULL)

如何定义do_something过程以确定调用它的方案?

编辑:澄清我对此程序的意图:

FUNCTION find_customer(name VARCHAR2 DEFAULT NULL, number VARCHAR2 DEFAULT NULL) RETURN NUMBER IS
BEGIN
    /* Query the "customer" table using only those parameters provided */
END;

以下是此过程与所需关联SQL子句的示例用法:

/* SELECT * FROM customer WHERE customer.name = 'Sam' */
find_customer(name => 'Sam')

/* SELECT * FROM customer WHERE customer.name = 'Sam' AND customer.number = '1588Z' */
find_customer(name => 'Sam', number => '1588Z')

/* SELECT * FROM customer WHERE customer.name = 'Sam' AND customer.number IS NULL */
find_customer(name => 'Sam', number => NULL)

/* SELECT * FROM customer WHERE customer.name IS NULL */
find_customer(name => NULL)

/* SELECT * FROM customer WHERE customer.name IS NULL AND customer.number IS NULL */
find_customer(name => NULL, number => NULL)

2 个答案:

答案 0 :(得分:8)

如果不是默认为null,将省略的参数值默认为在现实世界中永远不会使用的东西?您使用的值应属于某个域,因此请选择该域之外的值。

例如

PROCEDURE do_something(foo VARCHAR2 DEFAULT'*#@')IS

l_foo  VARCHAR2(32000); -- local copy of foo parm

BEGIN

IF foo = '*#@' THEN

-- I know the parm was omitted

   l_foo := NULL;

ELSE

   l_foo := foo;

END IF;

END;

答案 1 :(得分:7)

您可以重载该过程而不是使用默认值:

PROCEDURE do_something(foo VARCHAR2) IS
BEGIN
    /* Do something */
END;

PROCEDURE do_something IS
BEGIN
    /* here you know: no argument. Then call do_something(null) */
END;