Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pl/sql stored procedure: parameter name same as column name

I have a Stored Procedure like this

procedure P_IssueUpdate (     Id in integer,     ModifiedDate in date,     Solution in varchar2 ) AS BEGIN update T_Issue Set   ModifiedDate = ModifiedDate,   Solution = Solution where id = id; END P_IssueUpdate; 

my problem is that the parameter name is the same name as the Table column name. Is there a way to instruct the sql that the value after the "=" should be the parameter and not the column?

Thanks for your help

like image 346
gsharp Avatar asked Oct 22 '09 15:10

gsharp


People also ask

Can a procedure and function have same name?

No. This is clearly stated in the MSDN documentation: Procedure names must comply with the rules for identifiers and must be unique within the schema.

What are the 3 modes of parameter in PL SQL?

PL/SQL procedure parameters can have one of three possible modes: IN, OUT, or IN OUT. PL/SQL function parameters can only be IN. An IN formal parameter is initialized to the actual parameter with which it was called, unless it was explicitly initialized with a default value.

Why %type is used in PL SQL?

The %TYPE attribute, used in PL/SQL variable and parameter declarations, is supported by the data server. Use of this attribute ensures that type compatibility between table columns and PL/SQL variables is maintained.

Can we pass parameters in named Plsql blocks?

You can pass a constant, literal, initialized variable, or expression as an IN parameter. IN parameters can be initialized to default values, which are used if those parameters are omitted from the subprogram call.


2 Answers

You can prefix parameter and variable names with the name of the procedure like this:

SQL> declare   2     procedure p (empno number) is   3        ename varchar2(10);   4     begin   5        select ename   6        into p.ename   7        from emp   8        where empno = p.empno;   9        dbms_output.put_line(p.ename);  10     end;  11  begin  12     p (7839);  13  end;  14  / KING  PL/SQL procedure successfully completed. 
like image 95
Tony Andrews Avatar answered Sep 19 '22 11:09

Tony Andrews


what you described is called variable shadowing. It can happen in any language. You were given good workarounds but the common solution is to design a naming scheme so that it will never happen.

For example, name your columns without prefix and have your variables with a prefix that depends upon their scope (P_ for parameters, L_ for local variables, G_ for global package variables, etc...). This will have the added benefit of making the code more readable by giving you additional information.

like image 43
Vincent Malgrat Avatar answered Sep 20 '22 11:09

Vincent Malgrat