Home » SQL & PL/SQL » SQL & PL/SQL » how to call package using persistant package variable
how to call package using persistant package variable [message #609479] Fri, 07 March 2014 04:26 Go to next message
prasad2979
Messages: 1
Registered: March 2014
Location: bangalore
Junior Member
hi to all. I have a package, package purpose is update the planning hours already existing planning hours.
  • Attachment: empty.txt
    (Size: 1.26KB, Downloaded 1383 times)
Re: how to call package using persistant package variable [message #609480 is a reply to message #609479] Fri, 07 March 2014 04:58 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

OK and? What is the problem?

Re: how to call package using persistant package variable [message #609684 is a reply to message #609479] Tue, 11 March 2014 03:56 Go to previous messageGo to next message
vslabs
Messages: 26
Registered: March 2014
Location: Cape Town
Junior Member
How static variables can be used as parameters:

SQL> create or replace package FooLib as
  2          empNo   number;
  3          function GetEmpSurname( empNo number default FooLib.empNo ) return varchar2;
  4  end;
  5  /

Package created.

SQL> 
SQL> create or replace package body FooLib as
  2  
  3          function GetEmpSurname( empNo number default FooLib.empNo ) return varchar2 is
  4                  surname         emp.ename%Type;
  5          begin
  6                  select
  7                          e.ename into surname
  8                  from    emp e
  9                  where   e.empno = GetEmpSurname.empNo;
 10  
 11                  return( surname );
 12          end;
 13  
 14  
 15  end;
 16  /

Package body created.

SQL> -- use call with parameter
SQL> exec dbms_output.put_line( FooLib.GetEmpSurname(7499) );
ALLEN

PL/SQL procedure successfully completed.

SQL> -- use call via static package variable
SQL> exec FooLib.empNo := 7654; dbms_output.put_line( FooLib.GetEmpSurname );
MARTIN

PL/SQL procedure successfully completed.

SQL> 



The static approach is however questionable - and often lead to non thread safe code. If you ever programmed in C, you know how daft statics can be when, for example, tokenising strings and parsing.
Re: how to call package using persistant package variable [message #609714 is a reply to message #609684] Tue, 11 March 2014 07:57 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
And now in plain english - what's the problem? You don't want "static approach"? So why did you create globval package variable? Get rid of it alnog with default FooLib.empNo and the only way would be passing empno directly. However, keep in mind package scope is session, so that "static approach" affects just current session. Also, talking about following standards, you have getter but no setter. You should make empNo private and create SetEmpno procedure.

SY.
Re: how to call package using persistant package variable [message #609720 is a reply to message #609714] Tue, 11 March 2014 08:32 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Note that vslabs is not OP.

Re: how to call package using persistant package variable [message #609723 is a reply to message #609720] Tue, 11 March 2014 08:54 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
Ah, I see it now. vslabs hijacked the topic.

SY.
Re: how to call package using persistant package variable [message #609774 is a reply to message #609723] Wed, 12 March 2014 03:51 Go to previous messageGo to next message
vslabs
Messages: 26
Registered: March 2014
Location: Cape Town
Junior Member
Hijack?

Downloaded, and had a look, at the sample code attachment, spotted the lonely and unused static, and extrapolated from, given the OP's subject, what the OP was actually asking.

Or did I rub my crystal ball too hard? Wink
Re: how to call package using persistant package variable [message #609793 is a reply to message #609774] Wed, 12 March 2014 06:59 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
vslabs wrote on Wed, 12 March 2014 04:51
Hijack?

Downloaded, and had a look, at the sample code attachment, spotted the lonely and unused static, and extrapolated from, given the OP's subject, what the OP was actually asking.

Or did I rub my crystal ball too hard? Wink


Downloaded what? This is attachment from OP's post:

CREATE OR REPLACE PACKAGE update_planned_hrs
IS
      Planned_hours NUMBER(4);  
 
      PROCEDURE set_new_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER, p_hours IN NUMBER);
       FUNCTION existing_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER) RETURN NUMBER;
 
END update_planned_hrs;
/
 
CREATE OR REPLACE PACKAGE BODY update_planned_hrs
IS
 
PROCEDURE set_new_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER, p_hours IN NUMBER)
IS
BEGIN
   UPDATE employee_on_activity ea
   SET ea.ea_planned_hours = p_hours
   WHERE
            ea.ea_emp_id = p_emp_id            
            AND ea.ea_proj_id = p_project_id;
 
EXCEPTION
          WHEN NO_DATA_FOUND THEN
           RAISE_APPLICATION_ERROR (-20100, 'No such employee or project');
 
END set_new_planned;
 
FUNCTION existing_planned (p_emp_id IN NUMBER, p_project_id IN NUMBER) RETURN NUMBER
 
IS
 
existing_hours NUMBER(4);
 
BEGIN
   SELECT ea.ea_planned_hours INTO existing_hours 
   FROM employee_on_activity ea
   WHERE
            ea.ea_emp_id = p_emp_id     
            AND ea.ea_proj_id = p_project_id; 
 
   RETURN (existing_hours);
 
   EXCEPTION
          WHEN NO_DATA_FOUND THEN
           RAISE_APPLICATION_ERROR (-20100, 'No such employee or project');
 
 
   END existing_planned;
 
END update_planned_hrs;



So rub your crystal ball harder and try to find any resemblance with what you posted.

SY.
Re: how to call package using persistant package variable [message #609925 is a reply to message #609793] Thu, 13 March 2014 23:49 Go to previous message
vslabs
Messages: 26
Registered: March 2014
Location: Cape Town
Junior Member
What, you do not see the static and unused variable in that code, and associated it with the question "how to call package using persistant package variable"?

The example I posted is thus very specific to how to put that static variable to use, within the context of the thread's topic.

Hijacking? Bullshit.
Previous Topic: Calc elapsed time within a group of data
Next Topic: store and read xml file in an oracle table
Goto Forum:
  


Current Time: Wed Apr 24 10:18:05 CDT 2024