snippetModerate
In Oracle how do I save a sequence.nextval in a variable to be reused in multiple inserts?
Viewed 0 times
insertsnextvalsequencesavemultiplehowreusedoraclevariable
Problem
I'm writing a script to populate some tables with data for testing.
I would like to write something like the following but I don't know how to do it (I'm Oracle 11g)
I know I could rearrange the queries and use the
Maybe I should just wrap the script in a
Addition 27 May 2011 15:31
It seems that in any case I have to declare the variables in a
but I get the following error
That points to the variable declaration.
I'm using java to load the script from a file and running it using the Oracle JDBC driver (ojdbc14-10.2.0.4.0.jar) on a Oracle 11g server.
The table TEST_USER has been created with
I would like to write something like the following but I don't know how to do it (I'm Oracle 11g)
SET ENABLED_USER_ID = SEQ.NEXTVAL; // PSEUDOCODE
SET DISABLED_USER_ID = SEQ.NEXTVAL; // PSEUDOCODE
INSERT INTO USERS
(ID, USR_NAME)
VALUES (:ENABLED_USER_ID, 'ANDREW');
INSERT INTO CAR
(CAR_ID, CAR_NAME, USR_ID)
VALUES (CARSEQ.NEXTVAL, 'FORD', :ENABLED_USER_ID);
INSERT INTO USERS
(ID, USR_NAME)
VALUES (:DISABLED_USER_ID, 'ANDREW');
INSERT INTO CAR
(CAR_ID, CAR_NAME, USR_ID)
VALUES (CARSEQ.NEXTVAL, 'FORD', :DISABLED_USER_ID);I know I could rearrange the queries and use the
sequence.currval reference, but I'd prefer to have the id saved in properly named variables.Maybe I should just wrap the script in a
DECLARE ... BEGIN ... END; but I'm hoping there is a more concise way to do it.Addition 27 May 2011 15:31
It seems that in any case I have to declare the variables in a
DECLARE block. So I'm trying with DECLARE
USER_ID NUMBER(10,0) := 1;
BEGIN
insert into TEST_USER
values (user_id, 'andrew', sysdate);
END;but I get the following error
Caused by: java.sql.SQLException: ORA-06550: **line 2, column 27:**
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
* & = - + ; at in is mod remainder not rem
<> or != or ~= >= and or like like2
like4 likec between || multiset member submultisetThat points to the variable declaration.
I'm using java to load the script from a file and running it using the Oracle JDBC driver (ojdbc14-10.2.0.4.0.jar) on a Oracle 11g server.
The table TEST_USER has been created with
create table TEST_USERS (
id number(10, 0) not null,
name varchar2(100),
date_ins date default sysdate,
primary key (id)
);Solution
I think it goes like this
DECLARE
ENABLED_USER_ID PLS_INTEGER;
DISABLED_USER_ID PLS_INTEGER;
BEGIN
ENABLED_USER_ID := SEQ.NEXTVAL;
DISABLED_USER_ID := SEQ.NEXTVAL;
INSERT INTO USERS (ID, USR_NAME)
VALUES (ENABLED_USER_ID, 'ANDREW');
INSERT INTO CAR (CAR_ID, CAR_NAME, USR_ID)
VALUES (CARSEQ.NEXTVAL, 'FORD', ENABLED_USER_ID);
INSERT INTO USERS (ID, USR_NAME)
VALUES (DISABLED_USER_ID, 'ANDREW');
INSERT INTO CAR (CAR_ID, CAR_NAME, USR_ID)
VALUES (CARSEQ.NEXTVAL, 'FORD', DISABLED_USER_ID);
END;
/Code Snippets
DECLARE
ENABLED_USER_ID PLS_INTEGER;
DISABLED_USER_ID PLS_INTEGER;
BEGIN
ENABLED_USER_ID := SEQ.NEXTVAL;
DISABLED_USER_ID := SEQ.NEXTVAL;
INSERT INTO USERS (ID, USR_NAME)
VALUES (ENABLED_USER_ID, 'ANDREW');
INSERT INTO CAR (CAR_ID, CAR_NAME, USR_ID)
VALUES (CARSEQ.NEXTVAL, 'FORD', ENABLED_USER_ID);
INSERT INTO USERS (ID, USR_NAME)
VALUES (DISABLED_USER_ID, 'ANDREW');
INSERT INTO CAR (CAR_ID, CAR_NAME, USR_ID)
VALUES (CARSEQ.NEXTVAL, 'FORD', DISABLED_USER_ID);
END;
/Context
StackExchange Database Administrators Q#2978, answer score: 11
Revisions (0)
No revisions yet.