USING SEQUENCE IN PACKAGE SPEC

I Want to Use Sequence.NEXTVAL & Sequence.CURRVAL throughout the package. How can i declare the global sequence variable and make them available for all the stored procedures inside the package ?
Any help highly appreciated..
Thank you all in advance

Data Boy wrote:
No i know that but using directly SEQUENCE.CURRVAL inside the Insert statement is not a good practice.
We need to declare inside the procedure a variable and then fetch the SEQUENCE.CURRVAL into that variable
and use that variable inside the insert statement. But we that variable scope will be available only inside that procedure.
if i want the same CURRVAL in another stored procedure. How can i get it ..that is my Question.What exactly are you trying to achieve?
If it's a case of needing to know the value of the sequence number assigned to an inserted record you would typically use the RETURNING clause on the insert statement...
SQL> create table temp (id number, val varchar2(20));
Table created.
SQL> create sequence temp_seq;
Sequence created.
SQL> ed
Wrote file afiedt.buf
  1  create trigger trg_temp before insert on temp
  2  for each row
  3  begin
  4    select temp_seq.nextval into :new.id from dual;
  5* end;
SQL> /
Trigger created.
SQL> set serverout on
SQL> declare
  2    v_num number;
  3  begin
  4    insert into temp (val) values ('Fred') returning id into v_num;
  5    dbms_output.put_line('ID inserted was: '||to_char(v_num));
  6  end;
  7  /
ID inserted was: 1
PL/SQL procedure successfully completed.
SQL> select * from temp;
        ID VAL
         1 Fred
SQL>

Similar Messages

  • Schema name not present on filename for "Save Package Spec and Body"

    In versions previous to 3.0 EA, the filename defaulted to schema.object.sql when using the "Save Package Spec and Body" on the right click of the package/body. This appears to have disappeared. Also, it now defaults to the .PLS ext/type, which I prefer to save them as .SQL (which i can override, but it would be nice in the file type dropdown). Also, I had posted a suggestion about the actual file not including the schema name prefixing the object name when using the "Save Package Spec and Body". i.e. it does create or replace package reader_package instead of what it should be doing which is create or replace package schema.reader_package

    Would be nice indeed having the real name as default, and all supported PL/SQL types (as in the preferences) in the extensions dropdown.
    As for the schema name inside, I reckon that would do damage for more users than it would do good for others. But a preference would be best of course.
    K.

  • Common use of Sequence in Package

    Hi,
    I need to get the NEXTVAL from a sequence and i have to pass that to many packages inside of a package.
    If i write SELECT seq.nexval into v_varil from dual; means the value wont change sequentially.
    How can i get the NEXTVAL sequentialy and declare the SELECT stmt commonly.
    Can anyone help me in this.
    Thanks
    Radha K

    Could not understand your question, please be more specific,
    Anyway I have tried to answer it with 2 different assumptions with a common understanding that you don't want to code "SELECT seq.nexval into v_varil from dual;" every time you want to use the sequence instead want to have one common declaration for it so that you can reuse.
    Assumption1.
    You want same value of the sequence to be available in the package for a single oracle session.
    Create a function say get_seq_value return number in the package spec and define it in the body as follows
    then call this package in the initialization section of the package as follows
    CREATE OR REPLACE PACKAGE BODY abc_pkg
    g_seq IS NUMBER;
    FUNCTION get_seq_value return number
    IS
    v_varil IS NUMBER;
    BEGIN
    SELECT seq.nexval into v_varil from dual;
    RETURN v_varil;
    END;
    --here you will have all you internal package and procedures and functons.....
    BEGIN
    g_seq := get_seq_value;
    END abc_pkg;
    You can use global variable g_seq throughout this main package elements.
    Assumption2.
    You want different value of the sequence based on need purpose in the package but don't want to code the select statement, in this case just call the function get_seq_value whereever you need it in the package.
    Thanks
    SP

  • Problem with "Package Spec" please help.

    Hello once again.
    I'm developing a master-detail form, so I'm using a "Package Spec" variable to hold the primary key value from the master form. The primary key value for the master is generated by a sequence.
    Here's the package spec:
    PACKAGE primary_keygen IS
    pkey varchar2(15); 
    END;Now the master table has the following "Before Insert" Trigger .
    CREATE OR REPLACE TRIGGER MASTER_NUM_GEN
    BEFORE INSERT
    ON MASTER
    FOR EACH ROW
    DECLARE
    primary_key_value varchar2(15);
    BEGIN
    select lpad(to_char(ref_gen.nextval), 4,'0')
    into primary_key_value from dual;
    primary_keygen.pkey:='ABC/'||primary_key_value;
    :new.Ref_Number:=primary_keygen.pkey;
    END;For the detail block. I have the following "Before Insert Trigger" to generate the primary key values.
    CREATE OR REPLACE TRIGGER DET1_NUM_GEN
    BEFORE INSERT
    ON DETAIL1
    FOR EACH ROW
    BEGIN
    if :new.M_ref_number is NULL THEN
    :new.M_ref_number:=primary_keygen.pkey;
    ENd if;
    END;Works quite fine if I have only one detail block. But if I have multiple detail blocks. Depending on the user's selection. i.e. After entering data into the master block, the user selects a detail block ('Letter type'- using stacked canvases for this purpose and radio buttons for selecting the view) and then Inserts data into it. here's what I do in the Detail block2.
    CREATE OR REPLACE TRIGGER OREF_NUM_GEN
    BEFORE INSERT
    ON DETAIL2
    FOR EACH ROW
    BEGIN
    if :new.O_ref_number is NULL THEN
    :new.O_ref_number:=pkey_gen.master_key;
    ENd if;
    END;Now the problem is that When I enter one record into detail1, works fine, but for the second time, when I try to insert another record. the master table gets a new reference number (primary key value) while the detail block gets the previous value that was used in the first record!, so that means 'pkey_gen.master_key' is holding the old value, while in my opinion it should hold the new value, I dont know whats wrong here. If I try to insert two consecutive records into the same detail table, I get an error saying "Unique Constraint voilated", becuase the variable is holding the old values.
    And lastly after it inserts the record into the database, I get a dialog box saying, "successfuly inserted 2 records into the database" and when I click ok, the Form closes by itself, any ideas on how to stop this?
    I'm really stuck here. Please help me out on this.
    Thanks.
    Note: I'm using Form6i with Database 10g.
    Message was edited by:
    fahimkhan82

    Hi,
    Maybe the best way to start is to try building a new form from scratch. For simplicity I will assume that you have one master and one detail table. Master table has a primary key and the detail table has a foreign key to the master. Based on this, with the forms you can create a database block based on your master table. Use the wizard. Again using the wizard, create a second block based on the detail table. You'll see a screen to prompt to create a relationship. Create one by selecting the master table and the right foreign key relation. Now take a close look to all triggers automatically created by the form. Also check the "Relations" object found in you master block object tree. Note that the foreight key column in the detail table has the "Copy Value From Item" property set to monitor the master block's primary key.
    This is the basic. You don't need the database triggers, except (in case you want to insert through, say, SLQPlus) the one for the master table, which will look like this:
    CREATE OR REPLACE TRIGGER MASTER_NUM_GEN
    BEFORE INSERT
    ON MASTER
    FOR EACH ROW
    BEGIN
    IF :new.ref_number IS NULL THEN
    select 'ABC/'||pad(to_char(ref_gen.nextval), 4,'0')
    into :new.ref_number
    from dual;
    END IF;
    END;
    Now you have to take care about the inserts in your form. Create PRE-INSERT trigger on the master block.
    IF :master.ref_number IS NULL THEN
    SELECT 'ABC/'||pad(to_char(ref_gen.NEXTVAL), 4,'0')
    INTO :master.ref_number
    FROM dual;
    END IF;
    Run the form and explore different situations to see how the form is keeping the data integrity.
    Compile with Shift+K (incremental). Sometimes forms blow just because not all of the trigers were compiled properly.
    Hope this helps...

  • Wrong sequence of Packages in the tp buffer

    Hi,
    We are upgrading our solution manager 7.0 system to EHP1. We have
    already upgraded kernel to 701 and java patches to EHP1. Then all addon
    patches have been copied into the EPS/in . Then add-on upgrade
    operation have been started from SAINT transaction. But we have an
    error in DDIC_IMPORT phase like below:
    Import phase 'DDIC_IMPORT' (04.01.2011, 20:46:30)
    Wrong sequence of Packages in the tp buffer (e.g. SAPK-50001INCRMUIF)
    Abort the import due to an error situation (04.01.2011, 20:46:33)
    How can we solve and continue to Solution manager upgrade operation?
    Best regards,

    Hi,
    I solved problem. But now I have another problem in phase DDIC_ACTIVATION. Error information is like below:
    Error during executing the tp command 'tp ACT ALL TRS ...'
    tp return code: '0012' , tp message: 'A tool used by tp aborted' , tp output:
      initial value of NLS_LANG: 'AMERICAN_AMERICA.UTF8'
      This is tp version 372.04.88 (release 701, unicode enabled)
      Warning: Parameter DBLIBPATH is no longer used.
      standard output from tp and from tools called by tp:
    Details of the error situation can also be found in the import logs
    Abort the import due to an error situation (05.01.2011, 22:55:34)
    How can I solve it?
    Best regards,

  • Copying Packaging Spec

    When I am copying packaging spec, the fields  'Category', 'Sub-Category' & 'Group' are getting copied over, however the fields under 'Available UOM', 'UOM Conversions' and Cross Reference' are not getting copied, whether I choose to keep the copied Spec linked to the Template or not. [Both the Spec copied from and the Template used has those UOM/Cross reference fields defined] . Aren't those UOM/Cross reference fields suppose to be copied over to the new Spec?  When I create a new Spec from Template, system brings over all fields [UOM/Cross reference etc.] from Template, so I was expecting the same behavior while copying too, since I am linking to the Template during the copying process. Can someone shed some light on this issue please?

    I believe that the functional use cases are different. When you are copying from a Template, you are not copying a real existing specification - rather, you are creating a spec with values that should be there by default. These values from a template are typically not meant to be specific to one single specification, but rather to a grouping/classification of specs. Perhaps a Beverage type specification, created from a Beverage template, would have certain Available UOMs that a Sauce specification would not. Cross References on a template may be used for pack size purposes, which may also be specific to a template.
    When you copy an existing specification, the assumption is that the specification is linked to some external system(s) using the cross references - if those were copied over, you could have an possible discrepancy. Also, since you are doing a copy and not a new revision, we assumed the UOM info would be different so is is cleared out.

  • Split a PL/SQL Package Spec and Body

    I hope this was not discussed in some other thread somewhere (haven't found it), but my problem is:
    How to split the Package Spec and Package Body in JDeveloper if you want to have both (for database deployment) as files in e.g. a subversion repository?
    The problem arises if you have a couple of packages that are dependent on each other, so that you have to deploy the specs first to have the "public" part in place and afterwards the bodies to define the package functions/procedures.
    Is there a way to do this?
    TIA.
    --Ciao, FD.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

    If you want to store for example the package "mypackage" source code in a subversion repository
    one possibility is to define 2 svn files:
    - mypackage.pks (which contains the package "specification") and
    - mypackage.pkb ( package "body").
    I haven't tried this from JDev 11.2 but it works for example when using a single-user
    svn repository from Sql Developer 2.1.1. ( in SQL Dev if you specify a database
    connection you can compile the file based pakage specification and after then the
    package body ...)
    Regards
    user_alex

  • Can we declare a Cursor in Package Specs?

    Dear buddies
    Can I Declare a Cursor in Package Specs so that I can call that cursor and use its data in some procedures and functions of package. Otherwise I've to write that cursor for every sub-program of a package which I don't feel a smart way to accomplish the job.

    Hi,
    here is a short example with the whole way down. Maybe the concept is getting clearer with this:
    first of all, if you do not have the table emp, here the DDL for this example.
    Be carefull, works only for german clients because of the names of months, sorry for that.
    CREATE TABLE EMP
    (EMPNO NUMBER(4) NOT NULL,
    ENAME VARCHAR2(10),
    JOB VARCHAR2(9),
    MGR NUMBER(4),
    HIREDATE DATE,
    SAL NUMBER(7, 2),
    COMM NUMBER(7, 2),
    DEPTNO NUMBER(2));
    set echo on
    INSERT INTO EMP VALUES
    (7369, 'SMITH', 'CLERK', 7902,
    TO_DATE('17-DEZ-1980', 'DD-MON-YYYY'), 800, NULL, 20);
    INSERT INTO EMP VALUES
    (7499, 'ALLEN', 'SALESMAN', 7698,
    TO_DATE('20-FEB-1981', 'DD-MON-YYYY'), 1600, 300, 30);
    INSERT INTO EMP VALUES
    (7521, 'WARD', 'SALESMAN', 7698,
    TO_DATE('22-FEB-1981', 'DD-MON-YYYY'), 1250, 500, 30);
    INSERT INTO EMP VALUES
    (7566, 'JONES', 'MANAGER', 7839,
    TO_DATE('2-APR-1981', 'DD-MON-YYYY'), 2975, NULL, 20);
    INSERT INTO EMP VALUES
    (7654, 'MARTIN', 'SALESMAN', 7698,
    TO_DATE('28-SEP-1981', 'DD-MON-YYYY'), 1250, 1400, 30);
    INSERT INTO EMP VALUES
    (7698, 'BLAKE', 'MANAGER', 7839,
    TO_DATE('1-MAI-1981', 'DD-MON-YYYY'), 2850, NULL, 30);
    INSERT INTO EMP VALUES
    (7782, 'CLARK', 'MANAGER', 7839,
    TO_DATE('9-JUN-1981', 'DD-MON-YYYY'), 2450, NULL, 10);
    INSERT INTO EMP VALUES
    (7788, 'SCOTT', 'ANALYST', 7566,
    TO_DATE('09-DEZ-1982', 'DD-MON-YYYY'), 3000, NULL, 20);
    INSERT INTO EMP VALUES
    (7839, 'KING', 'PRESIDENT', NULL,
    TO_DATE('17-NOV-1981', 'DD-MON-YYYY'), 5000, NULL, 10);
    INSERT INTO EMP VALUES
    (7844, 'TURNER', 'SALESMAN', 7698,
    TO_DATE('8-SEP-1981', 'DD-MON-YYYY'), 1500, 0, 30);
    INSERT INTO EMP VALUES
    (7876, 'ADAMS', 'CLERK', 7788,
    TO_DATE('12-JAN-1983', 'DD-MON-YYYY'), 1100, NULL, 20);
    INSERT INTO EMP VALUES
    (7900, 'JAMES', 'CLERK', 7698,
    TO_DATE('3-DEZ-1981', 'DD-MON-YYYY'), 950, NULL, 30);
    INSERT INTO EMP VALUES
    (7902, 'FORD', 'ANALYST', 7566,
    TO_DATE('3-DEZ-1981', 'DD-MON-YYYY'), 3000, NULL, 20);
    INSERT INTO EMP VALUES
    (7934, 'MILLER', 'CLERK', 7782,
    TO_DATE('23-JAN-1982', 'DD-MON-YYYY'), 1300, NULL, 10);2. Package Spec:
    create or replace
    package test_cursor as
      --Type for the returncode of the function
      TYPE typ_emp IS TABLE OF emp%rowtype;
      --Array for fetching, of course also possible in the body
      t_emp typ_emp;
      --function wich returns the array from fetching the cursor
      function get_emp return typ_emp;
      --function for manupilation data retrieved by the function
      PROCEDURE man_emp;
    end test_cursor;3. Package Body
    create or replace
    package body test_cursor as
      FUNCTION get_emp RETURN typ_emp AS
      cursor c_emp is select * from emp;
      BEGIN
        open c_emp;
        fetch c_emp BULK COLLECT INTO t_emp;
        CLOSE c_emp;
        --t_emp returns the whole table set from emp
        return t_emp;
      end get_emp;
      PROCEDURE man_emp AS
      --just for not confusing names, is the same as t_emp of course
      v_emp_array typ_emp;
      BEGIN
        --call the function and retrieve the whole data set
        v_emp_array := get_emp;
        --now manipulate the data, in this case just write the names to the calling client
        FOR rec IN v_emp_array.FIRST .. v_emp_array.LAST
        loop
          dbms_output.put_line(v_emp_array(rec).ename);
        end loop;
      end man_emp;
    end test_cursor;4. Calling the procedure
    SET serveroutput ON
    exec test_cursor.man_emp;5. And this is the result:
    anonymer Block abgeschlossen
    SMITH
    ALLEN
    WARD
    JONES
    MARTIN
    BLAKE
    CLARK
    SCOTT
    KING
    TURNER
    ADAMS
    JAMES
    FORD
    MILLERPlease be aware, this is just for demonstration purpose, of course it makes no sense to display the names this way. But how to call a funktion returning arrays with datasets from fetching cursors is shown here.
    Hth
    Joerg

  • Saving package spec and body

    In previous versions of SQL Developer, there was always an All Files option in the dropdown when saving package spec and body. Now we're using the latest version 3.1.07.42 and the only option available is to save as a .pls extension. We do not use .pls files in our system, and it's annoying to have to save as .pls and then copy the code into another file. Does anyone know a way around this, and also why Oracle has put such a restriction on the save function?

    Hi,
    Welcome to the forum. This behavior of the chooser UI for Save Package Spec and Body changed between 3.0 and 3.1, mostly for the better, I assume, except perhaps for the issue on which you comment. In general, I believe there was an effort to make the user experience more consistent across the product when opening or saving files.
    Checking this area in the code line up next for release, I see that pls and sql are the file extension choices, but not all files. Will that help?
    Regards,
    Gary
    SQL Developer Team

  • EA2 - Export Wizard - No forward slash after package spec and body in sql

    I exported a schema with the export wizard. My stand alone functions and sql types in the script have have a forward slash after the "end;" statement Great. However, package specs and body create statements do not have a forward slash after the "end;" and I believe they also should have this.

    This appears to be a DB issue as we are just using DBMS_METADATA to generate the ddl. Please test manually using worksheet or sqlplus
    exec DBMS_METADATA.SET_TRANSFORM_PARAM( DBMS_METADATA.SESSION_TRANSFORM, 'SQLTERMINATOR' , TRUE );
    select dbms_metadata.get_ddl('PACKAGE', 'PACKAGE_NAME','SCHEMA') from dual;
    I looked through the DB bugs briefly and noticed a few on other objects that were fixed in 9.2.0.6 but I didn't see one for Packages...
    This is not something I would fix in SQL Developer as the core issue is with DBMS_MEATADATA and your version of the DB.
    As this is fixed in 10 I'm sure you can work with support and request a backport if it's not a currently available patch.

  • Save package spec and body in lower case ?

    I'm using sqldeveloper 3.1.07. We have the convention to save all source files in lower case. However, when I do "Save package spec and body" the filename that is suggested is always upper case. Is there a setting for this that I missed or any way I can change this ?

    Buntoro,
    thank you very much - that worked just fine (if I start from the Tools menu and then can select only the packages and package body options - and then in the next step do the name filtering for the selected objects).
    I was trying to use shift key and select multiple packages from the browser and then unload - that does not give you the "package body" selection option. In that particular case, the related package bodies for the selected packages should be automatically included - but I don't know if that can be classified as a bug or even an improvement, given that this can be achieved from the Tools menu (then unload).
    I understand what you mean by
    ========================================================
    "Note that the Package Spec displays only the choosen ones.
    But, why on earth unloading all of package body instead of the ones which we chose before?
    Simply forward, the output of this caused all of the package body to be exported.
    So please be aware before using this."
    ========================================================
    I also believe this is a bug and needs to be fixed.
    Buntoro, thanx again.
    R/ Zaf
    Edited by: zaferaktan on Jan 12, 2011 10:57 AM

  • 30EA2 Save package spec and body - saves only spec?

    It seems that the "Save package spec and body" in the popup menu on the spec in the object tree view actually only saves the spec (.pls).
    Is that so?

    Buntoro,
    thank you very much - that worked just fine (if I start from the Tools menu and then can select only the packages and package body options - and then in the next step do the name filtering for the selected objects).
    I was trying to use shift key and select multiple packages from the browser and then unload - that does not give you the "package body" selection option. In that particular case, the related package bodies for the selected packages should be automatically included - but I don't know if that can be classified as a bug or even an improvement, given that this can be achieved from the Tools menu (then unload).
    I understand what you mean by
    ========================================================
    "Note that the Package Spec displays only the choosen ones.
    But, why on earth unloading all of package body instead of the ones which we chose before?
    Simply forward, the output of this caused all of the package body to be exported.
    So please be aware before using this."
    ========================================================
    I also believe this is a bug and needs to be fixed.
    Buntoro, thanx again.
    R/ Zaf
    Edited by: zaferaktan on Jan 12, 2011 10:57 AM

  • Export tables, sequence, and package question

    Hi all,
    I've export table like: exp username/password file=export.dmp log=export.log tables=A statistics=none.
    The export statement above just export only the table "A" structure not the table "A" data. So, how can I move all the data from table "A" to table "B".
    How do I export the sequence, and package and its data also?
    Thank you very much
    Kevin

    Can you create a DBlink in order to do the insert into B select * from A.
    Also, you can use the SQL*Plus Copy command.
    Here are some links for COPY command:
    <br>Copying Data
    <br>copy command
    <br>copy command vs sql*loader
    <br><br>
    Here is a link for moving code:
    <br>exporting packages,function etc. from one user to another.

  • Package spec hanging up

    database 9i
    package spec is hanging up with only one session in operation
    I am unable to find the reason.
    There are no locks or dead lock deducted.
    Following is the contents of trace file in udump.
    *** SESSION ID:(9.1) 2006-08-05 10:06:59.000
    (blkno = 0x7a, size = 276, max = 1, in-use = 1, last-recid= 0)
    (blkno = 0x7b, size = 56, max = 145, in-use = 1, last-recid= 1)
    Thread checkpoint rba:0x000044.00000002.0010 scn:0x0000.002cbf4d
    On-disk rba:0x000044.00019790.0000 scn:0x0000.002d28f9
    Use incremental checkpoint cache-low RBA
    Thread 1 recovery from rba:0x000044.000155ec.0000 scn:0x0000.00000000
    ----- Redo read statistics for thread 1 -----
    Read rate (ASYNC) = 1400Kb/sec => 16804 blocks in 6s
    Read buffer = 8192Kb (16384 blocks)
    Longest record = 9Kb
    Record moves = 2/30438 (0%)
    Is there anything wrong in it?

    Thanks Pat,
    I thought I'd seen it on a BT email but this link will do for starters.
    Quote --
    Line rental saver, calls to mobiles, weekend calls and BT Infinity Bundle, Broadband and Anytime Calls Bundle, and entry level TV package prices remain unchanged.
    Source: BT
    All we need now is a mod (or rocker I don't mind) to respond. If not I'll click on a link.
    Michael

  • Package Spec

    1. what is the purpose of creating bodyless package ? what are the advantages of creating like this ?
    2. what is the reason for using "CREATE OR REPLACE PACKAGE" instead of dropping and creating ?

    Balarenganathan wrote:
    1. what is the purpose of creating bodyless package ? what are the advantages of creating like this ?Here's one example of a user for creating a bodyless package...
    Packages tend to fail because of their "package state". A package has a "state" when it contains package level variables/constants etc. and the package is called. Upon first calling the package, the "state" is created in memory to hold the values of those variables etc. If an object that the package depends upon e.g. a table is altered in some way e.g. dropped and recreated, then because of the database dependencies, the package takes on an INVALID status. When you next make a call to the package, Oracle looks at the status and sees that it is invalid, then determines that the package has a "state". Because something has altered that the package depended upon, the state is taken as being out of date and is discarded, thus causing the "Package state has been discarded" error message.
    If a package does not have package level variables etc. i.e. the "state" then, taking the same example above, the package takes on an INVALID status, but when you next make a call to the package, Oracle sees it as Invalid, but knows that there is no "state" attached to it, and so is able to recompile the package automatically and then carry on execution without causing any error messages. The only exception here is if the thing that the package was dependant on has changes in such a way that the package cannot compile, in which case you'll get an Invalid package type of error.
    And if you want to know how to prevent discarded package states....
    Move all constants and variables into a stand-alone package spec and reference those from your initial package. Thus when the status of your original package is invlidated for whatever reason, it has no package state and can be recompiled automatically, however the package containing the vars/const will not become invalidated as it has no dependencies, so the state that is in memory for that package will remain and can continue to be used.
    As for having package level cursors, you'll need to make these local to the procedures/functions using them as you won't be able to reference cursors across packages like that (not sure about using REF CURSORS though.... there's one for me to investigate!)
    This first example shows the package state being invalided by the addition of a new column on the table, and causing it to give a "Package state discarded" error...
    SQL> set serveroutput on
    SQL>
    SQL> create table dependonme (x number)
      2  /
    Table created.
    SQL>
    SQL> insert into dependonme values (5)
      2  /
    1 row created.
    SQL>
    SQL> create or replace package mypkg is
      2    procedure myproc;
      3  end mypkg;
      4  /
    Package created.
    SQL>
    SQL> create or replace package body mypkg is
      2    v_statevar number := 5; -- this means my package has a state
      3
      4    procedure myproc is
      5      myval number;
      6    begin
      7      select x
      8      into myval
      9      from dependonme;
    10
    11      myval := myval * v_statevar;
    12      DBMS_OUTPUT.PUT_LINE('My Result is: '||myval);
    13    end;
    14  end mypkg;
    15  /
    Package body created.
    SQL>
    SQL> exec mypkg.myproc
    My Result is: 25
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select object_name, object_type, status from user_objects where object_name = 'MYPKG'
      2  /
    OBJECT_NAME
    OBJECT_TYPE         STATUS
    MYPKG
    PACKAGE             VALID
    MYPKG
    PACKAGE BODY        VALID
    SQL>
    SQL>
    SQL> alter table dependonme add (y number)
      2  /
    Table altered.
    SQL>
    SQL> select object_name, object_type, status from user_objects where object_name = 'MYPKG'
      2  /
    OBJECT_NAME
    OBJECT_TYPE         STATUS
    MYPKG
    PACKAGE             VALID
    MYPKG
    PACKAGE BODY        INVALID
    SQL>
    SQL> exec mypkg.myproc
    BEGIN mypkg.myproc; END;
    ERROR at line 1:
    ORA-04068: existing state of packages has been discarded
    ORA-04061: existing state of package body "SCOTT.MYPKG" has been invalidated
    ORA-06508: PL/SQL: could not find program unit being called: "SCOTT.MYPKG"
    ORA-06512: at line 1
    SQL>
    SQL> select object_name, object_type, status from user_objects where object_name = 'MYPKG'
      2  /
    OBJECT_NAME
    OBJECT_TYPE         STATUS
    MYPKG
    PACKAGE             VALID
    MYPKG
    PACKAGE BODY        INVALID
    SQL>
    SQL> exec mypkg.myproc
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select object_name, object_type, status from user_objects where object_name = 'MYPKG'
      2  /
    OBJECT_NAME
    OBJECT_TYPE         STATUS
    MYPKG
    PACKAGE             VALID
    MYPKG
    PACKAGE BODY        VALIDAnd this next example shows how having the package variables in their own package spec, allows the package to automatically recompile when it is called even though it became invalidated by the action of adding a column to the table.
    SQL> drop table dependonme
      2  /
    Table dropped.
    SQL>
    SQL> drop package mypkg
      2  /
    Package dropped.
    SQL>
    SQL> set serveroutput on
    SQL>
    SQL> create table dependonme (x number)
      2  /
    Table created.
    SQL>
    SQL> insert into dependonme values (5)
      2  /
    1 row created.
    SQL>
    SQL> create or replace package mypkg is
      2    procedure myproc;
      3  end mypkg;
      4  /
    Package created.
    SQL>
    SQL> create or replace package mypkg_state is
      2    v_statevar number := 5; -- package state in seperate package spec
      3  end mypkg_state;
      4  /
    Package created.
    SQL>
    SQL> create or replace package body mypkg is
      2    -- this package has no state area
      3
      4    procedure myproc is
      5      myval number;
      6    begin
      7      select x
      8      into myval
      9      from dependonme;
    10
    11      myval := myval * mypkg_state.v_statevar;  -- note: references the mypkg_state package
    12      DBMS_OUTPUT.PUT_LINE('My Result is: '||myval);
    13    end;
    14  end mypkg;
    15  /
    Package body created.
    SQL>
    SQL> exec mypkg.myproc
    My Result is: 25
    PL/SQL procedure successfully completed.
    SQL>
    SQL> select object_name, object_type, status from user_objects where object_name = 'MYPKG'
      2  /
    OBJECT_NAME
    OBJECT_TYPE         STATUS
    MYPKG
    PACKAGE             VALID
    MYPKG
    PACKAGE BODY        VALID
    SQL>
    SQL> alter table dependonme add (y number)
      2  /
    Table altered.
    SQL>
    SQL> select object_name, object_type, status from user_objects where object_name = 'MYPKG'
      2  /
    OBJECT_NAME
    OBJECT_TYPE         STATUS
    MYPKG
    PACKAGE             VALID
    MYPKG
    PACKAGE BODY        INVALID
    SQL>
    SQL> exec mypkg.myproc
    My Result is: 25
    PL/SQL procedure successfully completed.The other uses are of course that you just want to create a load of type definitions etc. to be used by multiple packages elsewhere. ;)
    2. what is the reason for using "CREATE OR REPLACE PACKAGE" instead of dropping and creating ?What's the reason for dropping and recreating when you've got the option to create or replace? ;)

Maybe you are looking for

  • I need help with CSS and floating

    Okay, I know I need to get up on CSS and get rid of tables in my sites. However, I'm running up against a problem, and after banging my head against it for a while, I realize I need help. I've stripped this down so as to only show the area where I'm

  • Queuehandling to serial port

    How do I make a queue for sending commands to the serial port? Regards Frode Efteland

  • Adobe application manager update server not responding

    I have installed the Creative Suite 6 Master Collection from CDs, and activated the license. Now when I run Photoshop > Help > Updates, the Adobe Application Manager runs, but gives the error message "The update server is not responding. The server m

  • Alternative for STRING_CENTER function module

    Hi, Can any one give me the Alternative for STRING_CENTER function module. it is there in 4.7 when i am using the same  in ecc 6.0 it is giving as absolute statement . Please give the solution for this.

  • Best way to archive RAW Images and keep JPG?

    Hello everyone, I have what I believe to be a fairly simple question.  My iPhoto library, comprised mainly of RAW images, is taking up a majority of my MacBook's 500GB storage capacity, and I want to fix this. I want to keep all the photos in my libr