如何确定是否默认了PL/SQL参数值?
-
22-08-2019 - |
题
假设我有一个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)
解决方案
您可以超载该过程而不是使用默认值:
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;
其他提示
如何默认为null,而是默认为您在现实世界中永远不会使用的东西遗漏的参数值呢?您使用的值应属于某些域,因此在该域之外选择值。
例如
过程do_something(foo varchar2默认'*#@')是
l_foo VARCHAR2(32000); -- local copy of foo parm
开始
IF foo = '*#@' THEN
-- I know the parm was omitted
l_foo := NULL;
ELSE
l_foo := foo;
END IF;
结尾;
不隶属于 StackOverflow