Why does an oracle plsql varchar2 variable need a size but a parameter does not?

余生颓废 提交于 2019-11-28 01:16:07

问题


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.


回答1:


"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.




回答2:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!