Hi there everyone,

I have an insert query like so:
$insertquery = "insert into mailinglist values (mailinglist_id.NextVal, '$name', '$email', 'N')";

for which I'm trying to insert a row into a table using a sequence to generate a unique PK... it works fine.

but....

in this same script I want to reference the value of 'mailinglist_id.NextVal'.

eg:
print"<a href='myscript.php?id=mailinglist_id.NextVal'>click me</a>

obviously this won't work, but I want to use whatever value was used as the PK for the record I just inserted, and I don't know how to get it...

does anyone have any ideas on how to go about solving this problem? preferably without having to make another query on the database... (that seems to be the only thing I can think of...)

thanks 🙂

    Oracle does great stored procedures and as these are optimised it's generally better to call a stored procedure than to pass a query in a string.

    That, in itself, solves your problem. With one hit to the database you pass your data and get back the id

    the stored procedure gets the nextval into a variable, then uses it to do the insert. As the nextval is in a variable it's also available to pass back.

      Thanks Sarah 🙂

      I haven't used Oracle stored procedures before... so I guess I'll have to learn about them first... but thanks for the reply.

      cya

        Sarah... I\'ve been trying to use the stored function in oracle, similar to what you said ... but I can\'t get it to work.

        do you or anyone else know what I\'m doing wrong?

        just to clarify things if they aren\'t already.. I have a table called mailinglist, and it\'s pk is mailinglist_id...

        and I use a sequence to increment the pk (mailinglist_id.NextVal)

        CREATE OR REPLACE FUNCTION sf_insertmail
        RETURN mailinglist.mailinglist_id%type
        AS
        id mailinglist.mailinglist_id%type;
        BEGIN
        id := mailinglist_id.NextVal;
        RETURN id;
        END;
        /

        it won\'t let me assign mailinglist_id.NextVal to id.

        \"Table,View Or Sequence reference \'MAILINGLIST_ID.NEXTVAL\' not allowed in this context\"

        Hmmmm... I\'m stuck as to how I\'m supposed to return NextVal. if I say \"RETURN mailinglist_id.NextVal\" that throws an error too...

        so if anyone knows how to do this, that would be great.

        thanks 🙂

          Hiya Adam, there are a couple of different ways to do what you want to do. Personally, when I insert rows into a table and I want to use a sequence to generate my ID's for me, I use a TRIGGER. This way nobody can insert bogus data into the table. Let's walk through a quick sample:

          /******************************************/
          SQL> create sequence id_seq
          2 start with 1
          3 increment by 1
          4 /

          Sequence created.

          SQL> create table name_values(
          2 id number,
          3 name varchar2(30),
          4 value varchar2(255)
          5 )
          6 /

          Table created.

          SQL> create trigger name_values_bi_trig
          2 before insert on name_values
          3 for each row
          4 begin
          5 select id_seq.nextval
          6 into :new.id
          7 from dual;
          8 end;
          9 /

          Trigger created.

          SQL> insert into name_values( name, value )
          2 values( 'Oracle', 'Database' );

          1 row created.

          SQL> insert into name_values( name, value )
          2 values( 'Java', 'Language' );

          1 row created.

          SQL> select * from name_values;

          ID NAME VALUE


          1 Oracle Database
          2 Java Language
          /******************************************/

          The reason you can't assign a seq.nextval to a variable in your function is because PL/SQL won't let you do that, only SQL understands how to do that. You can use a "singleton select" in PL/SQL to do what you're trying to do:

          /******************************************/
          SQL> create or replace function get_next_id
          2 return number
          3 is
          4 l_id number;
          5 begin
          6 select id_seq.nextval
          7 into l_id
          8 from dual;
          9 return l_id;
          10 end;
          11 /

          Function created.

          SQL> select get_next_id from dual;

          GET_NEXT_ID

          3
          /******************************************/

          Hope that helps!

          sean

            Thanks Sean, that helped a lot.

            It's been a bit a of a steep learning curve reading up on all this pl/sql stuff... but it's been good. (We don't actually cover pl/sql till later in my course... so it looks like I have a head start on everyone else now 😉 )

            But I managed to get it working, so thanks.

            bye 🙂

              Write a Reply...