I am writing a C++ program using OCI to call the stored procedure. In my PL/SQL stored procedure test, if not initializing an outbound variable, I might get “fetched column value is NULL” error because in case of foo != 0, bar is NULL. So in the first line, I initialize bar first. Is this the right way to handle outbound variable?
FUNCTION function1(
foo IN INTEGER,
bar OUT VARCHAR2
) RETURN INTEGER
IS
ret INTEGER;
BEGIN
bar := ' '; -- do I need to initialize this variable?
IF foo = 0 THEN
ret := 0;
bar := 'a';
ELSE
ret := 1;
END IF;
RETURN ret;
END function1;
If you don’t set BAR in your program then the variable will be initialized to the default value of its type (NULL in this case, and for any type except for a record type with a non-NULL default value). That’s just basic logic.
So your choices are:
Your posted code uses the first option. In the simple logic you present that is the approach I would take. If the internals were more complicated – an IF or a CASE with many branches – I would choose to have every branch explicitly set the value, because that would probably make the code’s intent clearer.