Skip to content
Advertisement

SQL: How to write multiple interactive scripts in one query (insert rows)

Simple question- I just want to learn how to write a query that will insert 2 rows into my table by writing one query, instead of 2 separate ones. My queries work completely fine, I just want to know a simpler way, if that’s possible. Below are my 2 queries that I’d like to combine into 1.

INSERT INTO dept (deptno, dname)
VALUES (dept_id_seq.NEXTVAL, 'EDUCATION');

INSERT INTO dept (deptno, dname)
VALUES (dept_id_seq.NEXTVAL, 'ADMINISTRATION');

As I stated above, my code works as expected. Here is my output with NO errors-

DE DNAME                          LOC
-- ------------------------------ ------------------------------
10 ACCOUNTING                     NEW YORK
20 RESEARCH                       DALLAS
30 SALES                          CHICAGO
40 OPERATIONS                     BOSTON
60 EDUCATION
70 ADMINISTRATION

Thank you for anyone’s help!

Advertisement

Answer

Its not totally clear what you’re asking for, but you can make your inserts 1 script execution with:

BEGIN
INSERT INTO dept (deptno, dname) VALUES (dept_id_seq.NEXTVAL, 'EDUCATION');
INSERT INTO dept (deptno, dname) VALUES (dept_id_seq.NEXTVAL, 'ADMINISTRATION');
END;

However it looks like you may want to use a cursor to loop through the list of values and insert them somewhere else.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement