Home » SQL & PL/SQL » SQL & PL/SQL » Putting a sequence value into a variable
Putting a sequence value into a variable [message #156051] Fri, 20 January 2006 08:26 Go to next message
fakelvis
Messages: 3
Registered: January 2006
Junior Member
Hi all...

I'm writing a number of scripts that insert rows into tables.
To better explain my point, I'll paste the scripts here as they are easier to understand...

Script 1:
INSERT INTO WORKGROUP_DEPARTMENTS(ID, DEPT_NAME, DEPT_TYPE)
VALUES (SEQ_WGDPT.NEXTVAL, 'Staff', 3);

Script 2:
INSERT INTO WORKGROUP_SECTIONS(ID, SECT_NAME, WGDPT_ID)
VALUES (SEQ_WGSEC.NEXTVAL, 'Staff 1',
        (SELECT ID FROM WORKGROUP_DEPARTMENTS
         WHERE DEPT_NAME = 'Staff'
         AND DEPT_TYPE = '3')
);

Script 3:
INSERT INTO WORKGROUPS(NAME, UNIT_TYPE, WGSEC_ID)
VALUES ('Staff Room', 3,
        (SELECT ID FROM WORKGROUP_SECTIONS
         WHERE SECT_NAME = 'Staff 1')
);

As you can see, in scripts 1 and 2 a new value is produced from a sequence (SEQ_WGDPT & SEQ_WGSEC respectively). Then, in scripts 2 & 3 (respectively) these values are inserted into another table.

What I'm trying to figure out is: can I do the same thing here without having to include the horrible SELECT statements in scripts 2 & 3 in order to find the ID, which was produced by the sequence in the previous script?

Unfortunately they do have to be separate scripts, and I'm stuck!

I tried to insert the sequence value into a variable a la PL/SQL so that I could call this variable from the next script, but failed miserably as I'm rubbish!

Any help would be great. Thanks,

fakelvis
Re: Putting a sequence value into a variable [message #156053 is a reply to message #156051] Fri, 20 January 2006 08:31 Go to previous messageGo to next message
JSI2001
Messages: 1016
Registered: March 2005
Location: Scotland
Senior Member
look at sequence_name.currval

HTH
Jim
Re: Putting a sequence value into a variable [message #156060 is a reply to message #156053] Fri, 20 January 2006 09:06 Go to previous messageGo to next message
fakelvis
Messages: 3
Registered: January 2006
Junior Member
I have tried doing this in PL/SQL...

SELECT SEQ_WGDPT.CURRVAL INTO SeqVal FROM DUAL


Yet this fails for a number of various reasons. I can get the variable to be created with currval in it, but then when i run another script and try and call this variable to insert, it fails!

Is this what you would suggest, or am I barking up the wrong tree with this?
Re: Putting a sequence value into a variable [message #156064 is a reply to message #156060] Fri, 20 January 2006 09:35 Go to previous messageGo to next message
fakelvis
Messages: 3
Registered: January 2006
Junior Member
Also, I did consider simply using the sequence.currval in the insert statement that wants the value, but this might not work in some situations...

The statements are in different .SQL files so that they can be run at different times (in the same session). Although, any number of users could, between me running, say, the 2nd and 3rd script, use the sequence and alter the value, and as such the ID values would be incorrect when the later script is run and 'sequence.currval' is used in the insert statement.
Re: Putting a sequence value into a variable [message #156081 is a reply to message #156051] Fri, 20 January 2006 11:59 Go to previous messageGo to next message
scottwmackey
Messages: 515
Registered: March 2005
Senior Member
Given that you posted this on the Newbies forum, I going to have to push you a bit on one of the comments you made. A common problem with newbies is that they quite often ask the wrong question. They assume that they have to do something one way, often a quite difficult way, and it turns out there is a simpler way. So, can you please explain why they have to be in separate scripts. Because if they "have to be" in separate scripts you are indeed screwed. [Edit: Oops. Just re-read the "same session" comment. I take all that back.]

[Updated on: Fri, 20 January 2006 12:02]

Report message to a moderator

Re: Putting a sequence value into a variable [message #156085 is a reply to message #156081] Fri, 20 January 2006 12:25 Go to previous messageGo to next message
scottwmackey
Messages: 515
Registered: March 2005
Senior Member
All of this assumes that you are, indeed, in one session and that no other work occurs in that session between the times that the scripts are run.

SQL> create table t1(id number);

Table created.

SQL> create table t2(id number, fk number);

Table created.

SQL> create table t3(id number, fk number);

Table created.

SQL> create sequence t1_s;

Sequence created.

SQL> create sequence t2_s start with 100; -- I used a different number just to verify that we are getting the right values in the right places

Sequence created.

----------------------- Start of first script -----------------------------

SQL> var x1 number;

SQL> insert into t1 values(t1_s.nextval) returning id into :x1;

1 row created.

----------------------- End of first script -------------------------------

---------------------- Start of second script -----------------------------

SQL> var x2 number;

SQL> insert into t2 values (t2_s.nextval, :x1) returning id into :x2;

1 row created.

---------------------- End of second script -------------------------------


---------------------- Start of third script ------------------------------

SQL> insert into t3 values (1, :x2);

1 row created.

---------------------- End of third script --------------------------------

-- Validation that the process worked as expected

SQL> select *
  2  from   t1;

        ID
----------
         1

SQL> select * from t2;

        ID         FK
---------- ----------
       100          1

SQL> select * from t3;

        ID         FK
---------- ----------
         1        100

SQL>

Re: Putting a sequence value into a variable [message #156098 is a reply to message #156051] Fri, 20 January 2006 13:21 Go to previous messageGo to next message
d.fowler
Messages: 21
Registered: January 2006
Location: Missouri
Junior Member


Currval can only be referenced in the session when .nextval done first. No need to use a variable.

insert into t1 (t1_id...
select seq1.nextval,.... from ;

insert into t2(t2_id,....t1_fk)
select seq2.nextval ... seq1.currval from ;

hth

Re: Putting a sequence value into a variable [message #156121 is a reply to message #156098] Sun, 22 January 2006 13:14 Go to previous messageGo to next message
William Robertson
Messages: 1643
Registered: August 2003
Location: London, UK
Senior Member
Is it not possible to construct something more robust, perhaps using PL/SQL, where it is very easy to declare and populate variables?

Even in a scripting environment though, you can use SQL*Plus variables:

SQL> CREATE TABLE testit (id INT);

Table created.

SQL> CREATE SEQUENCE testit_seq;

Sequence created.

SQL> var testit_id NUMBER
SQL> 
SQL> INSERT INTO testit VALUES (testit_seq.NEXTVAL)
  2  RETURNING id INTO :testit_id;

1 row created.

SQL> print :testit_id

 TESTIT_ID
----------
         1

The requirement to allow the scripts to be called in an unpredictable order is a tough one, though.
Re: Putting a sequence value into a variable [message #156143 is a reply to message #156051] Sun, 22 January 2006 23:47 Go to previous message
Sneh
Messages: 5
Registered: January 2006
Location: Tampa
Junior Member
i am new to PL/SQL , but following might work

though i dont know this is a best solution .

may be there r other expert can help u.

DECLARE
CURSOR c1 IS select SEQ_WGDPT.NEXTVAL,AS "WGDPT_NEXTVAL",
'staff' AS "DEPT_NAME",3 AS "DEPT_TYPE",
SEQ_WGSEC.NEXTVAL AS "WGSEC_NEXTVAL",
'STAFF 1' AS "SECTION_NAME",'STAFF ROOM'"NAME",3 "UNIT_TYPE"
FROM DUAL;

v_ID_V_SEQ_WGDPT_NEXTVAL NUMBER;
V_DEPT_NAME VARCHAR2(10);
V_DEPT_TYPE NUMBER;
V_SEQ_WGSEC_NEXTVAL NUMBER;
V_SEC_NAME VARCHAR2(10);
V_NAME VARCHAR2(10);
V_UNIT_TYPE NUMBER;
BEGIN
FOR c1_r IN c1 LOOP
v_SEQ_WGDPT_NEXTVAL := c1_r.wgdpt_nextval;
V_DEPT_NAME := c1_r.dept_name;
V_DEPT_TYPE := c1_r.dept_type;
V_SEQ_WGSEC_NEXTVAL := c1_r.WGSEC_NEXTVAL;
V_SEC_NAME :=c1_r.SECTION_NAME;
V_NAME :=c1_r.name;
v_unit_type :=c1_r.unit_type;

INSERT INTO WORKGROUP_DEPARTMENTS(ID, DEPT_NAME, DEPT_TYPE)
VALUES (c1_r.v_SEQ_WGDPT_NEXTVAL,cl_r.V_DEPT_NAME,c1_r.v_V_DEPT_TYPE);

INSERT INTO WORKGROUP_SECTIONS(ID, SECT_NAME, WGDPT_ID)
VALUES (c1_r.V_SEQ_WGSEC_NEXTVAL,cl_r.V_SEC_NAME,c1_r.v_SEQ_WGDPT_NEXTVAL);

INSERT INTO WORKGROUPS(NAME, UNIT_TYPE, WGSEC_ID)
VALUES (c1_r.V_NAME,c1_r.v_unit_type,c1_r.V_SEQ_WGSEC_NEXTVAL);


end loop;

commit;

end;
/

Previous Topic: help with trigger
Next Topic: 'DAY' isn't recognized by PL/SQL
Goto Forum:
  


Current Time: Sat Sep 06 11:38:27 CDT 2025