Suppose you have this procedure:
PROCEDURE f (param VARCHAR2)
IS
var VARCHAR2(10);
BEGIN
var := 'hi';
END f;
I would like to understand why var
needs a length specified, but param
does not. I'm having a hard time finding information about this in the oracle docs.
"Oracle Database derives the length, precision, and scale of an argument from the environment from which the procedure is called."
Please check out this related question.
Reference: Oracle® Database SQL Reference 10g Release 2 (10.2) Please look under semantics / argument / datatype.
The difference is that subprogram headings have formal parameters that are replaced with actual parameters when the subprogram is called:
create or replace function f(
p_x in varchar2 /* a formal parameter */
,p_y in varchar2 /* a formal parameter */
) return varchar2 /* a formal parameter */
is
begin
return p_x || p_y;
end;
declare
v_z varchar2(10);
v_x constant varchar2(1) := 'X';
begin
v_z := f(v_x, 'Y'); /* actual parameters */
end;
The formal parameter is unconstrained (but constrained subtypes can be used) and includes also information about parameter mode and possible default value that are not relevant when a variable is declared.
The datatypes of formal and actual parameters have not to be the same but compatible.
There is plenty of other details too but they can be read from chapter PL/SQL Subprograms of PL/SQL Language Reference. See expecially Subprogram Parameters.
I don't know why the formal parameters have to be unconstrained but I'm a quite happy with that as it removes (unnecessary) details from a subprogram header making it a bit more abstract.
来源:https://stackoverflow.com/questions/22050189/why-does-an-oracle-plsql-varchar2-variable-need-a-size-but-a-parameter-does-not