Home » SQL & PL/SQL » SQL & PL/SQL » package spec/body
package spec/body [message #193297] Fri, 15 September 2006 10:31 Go to next message
nagsindhu
Messages: 14
Registered: August 2006
Location: Newjersey
Junior Member

Hi experts,

Why do we need package specification and package body separately?

Thanx in Advance
SindhuReddy
Re: package spec/body [message #193319 is a reply to message #193297] Fri, 15 September 2006 12:23 Go to previous messageGo to next message
shoblock
Messages: 325
Registered: April 2004
Senior Member
because oracle defined them that way.

a benefit to oracle approach is that if you change some code within the body, you don't need to change the spec. and if you don't change the spec, then everything that references it does NOT become invalidated.

another benefit is that you can put some procedures/functions in the spec, making them public. but you can have MORE code that resides only in the body, and is therefore private.

good enough?
Re: package spec/body [message #193344 is a reply to message #193297] Fri, 15 September 2006 15:40 Go to previous messageGo to next message
nagsindhu
Messages: 14
Registered: August 2006
Location: Newjersey
Junior Member

Hi,
i guess, each time we hav to execute package body and no need of executing pakage specification..am i wrong here?

Regards
SindhuReddy

Re: package spec/body [message #193370 is a reply to message #193297] Fri, 15 September 2006 22:13 Go to previous messageGo to next message
shoblock
Messages: 325
Registered: April 2004
Senior Member
if you make a change to the body, you can compile the body and not the spec. but the body and spec need to be in synch - if you change a procedure/function declaration in the body, and it exists in the spec, you must also change the spec to match and recompile the spec.
Re: package spec/body [message #193425 is a reply to message #193297] Sat, 16 September 2006 12:40 Go to previous messageGo to next message
Liza79
Messages: 74
Registered: September 2006
Member
nagsindhu wrote on Fri, 15 September 2006 15:40

Hi,
i guess, each time we hav to execute package body and no need of executing pakage specification..am i wrong here?



lets make this very clear here, a package is never executed itself, only the member FUNCTIONS and PROCEDURES execute.

The body depends upon the spec , so when ever there is a change in the spec the body has to re-compile, even if there is no change been made in the body.

Now coming back to your original question, the specification has to be separate for information hiding. That means, if you give execute rights on your package to some user, and he tries to see the source code of the package he cannot, he will only see the specification code.
Re: package spec/body [message #193552 is a reply to message #193425] Mon, 18 September 2006 03:55 Go to previous messageGo to next message
JRowbottom
Messages: 5933
Registered: June 2006
Location: Sunny North Yorkshire, ho...
Senior Member
Not entirely true.
You can add a BEGIN statment to a package body, after the code for all the package body procedures, and any code between that BEGIN and the END at the bottom of the package body will be executed when the package is first accessed for a given session.

CREATE OR REPLACE PACKAGE PKG_TEST AS
  procedure do_very_little;
END;
/

CREATE OR REPLACE PACKAGE BODY PKG_TEST AS
  procedure do_very_little is
  begin
    dbms_output.put_line ('Do Very Little Called');
  end do_Very_little;
  
BEGIN
  dbms_output.put_line('Package Body Executed');
END;
/

Test Code:

begin
  pkg_test.do_very_little;
  
  pkg_test.do_very_little;
end;

Results:
Package Body Executed
Do Very Little Called
Do Very Little Called
Re: package spec/body [message #194210 is a reply to message #193297] Thu, 21 September 2006 03:39 Go to previous message
Liza79
Messages: 74
Registered: September 2006
Member
hmmmm Smile you are talking about the one time only procedure, ooooopss it is also called a procedure, member of the package body.
And is executed automatically at the first time when a member of the package in the session is executed and is not executed on later execution of the members.
That does not prove my statemet not to be true. What i said is that a package is never executed itself. i.e. you cannot write
EXECUTE package_name;
and more over it was a comment on this quote
Quote:

Hi,
i guess, each time we hav to execute package body and no need of executing pakage specification..am i wrong here?



he is saying each time we execute package body, which is wrong. It is never executed. There is only ONE-TIME-ONLY procedure, that is called by oracle and used to perform some necessary initialization actions in the session.

Liza.
Previous Topic: give parameters into function
Next Topic: Matrix Type Results from Query (I could use some help)
Goto Forum:
  


Current Time: Fri Dec 02 14:15:28 CST 2016

Total time taken to generate the page: 0.09365 seconds