Insert multiple records in oracle [duplicate] - sql

This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 8 months ago.
I am using oracle sql developer to insert rows in my database.
While this request is working :
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1")
The second one (when I am trying to insert multiple rows)is not working:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1"),(2,"ok2")
I am getting this error :
Erreur SQL : ORA-00933: SQL command not properly ended
00933. 00000 - "SQL command not properly ended"

You could use INSERT ALL statement. For example:
INSERT ALL
INTO mytable (column1, column2, column3) VALUES ('val1.1', 'val1.2', 'val1.3')
INTO mytable (column1, column2, column3) VALUES ('val2.1', 'val2.2', 'val2.3')
INTO mytable (column1, column2, column3) VALUES ('val3.1', 'val3.2', 'val3.3')
SELECT * FROM dual;

Oracle does not support multi-row inserts. You need to write one insert per row:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,'ok1');
INSERT INTO TABLE ( USERID, USERNAME) VALUES (2,'ok2');
Additionally: string literals need to be enclosed in single quotes in SQL. Double quotes are for identifiers. "ok1" is a column name, 'ok1' is a string constant.

See this recent post for some other ways to enter test data as well:
Insert same data multiple times

If you are not worried about SQL injection then run the following :
BEGIN
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,'ok1');
INSERT INTO TABLE ( USERID, USERNAME) VALUES (2,'ok2');
END;

Related

Inserting multiple rows query failed [duplicate]

This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
Closed 3 years ago.
INSERT INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
VALUES (7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30)
, (7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30);
Tried to insert two rows at the same time. but failed saying "SQL command not properly ended". Can someone please correct the query?
Error:
Error at Command Line : 18 Column : 125 Error report - SQL Error: ORA-00933: SQL command not properly ended 00933. 00000 - "SQL command not properly ended" *Cause: *Action:
Based on your error message (ORA-00933:SQL command not properly ended), the DBMS is Oracle.
You can use the following query to INSERT INTO in Oracle.
INSERT ALL
INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES (7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30)
INTO EMP (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO) VALUES (7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30)
try like below
insert into emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
select 7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30 from dual
union all
select 7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30 from dual
To insert multiple records in ORACLE, you need to club your records into cte. Or use Insert All as mentioned by #Arulkumar.
INSERT INTO Emp (EMPNO,ENAME,JOB,MGR,HIREDATE,SAL,COMM,DEPTNO)
WITH names AS (
SELECT 7499,'ALLEN','SALESMAN',7698,'20-Feb-81',1600,300,30 UNION ALL
SELECT 7521,'WARD','SALESMAN',7698,'22-Feb-81',1250,500,30
)
SELECT * FROM names
You may find this link for more info on insert command in Oracle.Link
Your Syntax is for Microsoft SQL Server, but your Error Message is from an Oracle DBMS.
You could use the INSERT ALL query:
INSERT ALL
INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
INTO mytable (column1, column2, column_n) VALUES (expr1, expr2, expr_n)
SELECT * FROM dual;
See Official Oracle Documentation

Error msg SQL command not properly ended when I try to insert values to table [duplicate]

This question already has answers here:
Best way to do multi-row insert in Oracle?
(9 answers)
ORA-00933: SQL command not properly ended in insert command
(3 answers)
I am getting an error in Oracle about a command not properly ending
(3 answers)
Closed 4 years ago.
I'm trying to insert values to a table, but it kept giving me error msg saying the command not properly ended. I checked again and again, I don't see I miss any comma, semicoma, and the table name is correct (I also checked again and again), all column names are correct and in right order too (I checked it again and again too), and the spell of the command is also correct. So what's wrong with my code?
insert into fruits (fid,fname,quantities)
values (1,'apple',3),
(2,'orange',2),
(3,'banana',5);
As you tagged question as oracle, it don't support that kind of multiple insert query like SQL server and mysql does.
Alternatively you can use insert all as
INSERT ALL INTO mytable (column1, column2, column_n)
VALUES (expr1, expr2, expr_n) INTO mytable (column1, column2, column_n)
VALUES (expr1, expr2, expr_n) INTO mytable (column1, column2, column_n)
VALUES (expr1, expr2, expr_n) SELECT * FROM dual;
It is probably simplest to use insert . . . select:
insert into fruits (fid, fname, quantities)
select 1, 'apple', 3 from dual union all
select 2, 'orange', 2 from dual union all
select 3, 'banana', 5 from dual;
Or three separate insert statements.

How to insert multiple values in 11g?

How to insert multiple values in a table in oracle 11g using query?
I tried This
Use the following DML known as INSERT ALL statement :
insert all into departmentstrigger values(4,'Hello')
into departmentstrigger values(5,'HEy There')
into departmentstrigger values(6,'sup')
into departmentstrigger values(7,'Hii')
select * from dual;
SQL Fiddle Demo
For a detailed explanation you may look at : Insert All Statement

Insert rows based on insert statement (nested insert)

A common insert statement is this..
INSERT INTO tbl_name (ID) VALUES (1)
What I wanted to achieve is to Insert an ID using another insert statement from another table.. It would look like this
INSERT INTO tbl_name VALUES (INSERT INTO tbl_name2 (ID) VALUES (1))
I have tried it but it's giving me errors..
INSERT INTO tblReport_OPA (ID_Main) VALUES (INSERT INTO tblReport_OPF (ID_Main) VALUES (1))
I'm currently developing under vb.net 2010 and sql express 2005
You probably can use OUTPUT clause, like this:
INSERT INTO tblReport_OPF (ID_Main)
OUTPUT Inserted.Id_Main
INTO tblReport_OPA
SELECT 1 as Id_Main
Note you'll have to use SELECT instead of VALUES
Opyionally a merge can be used.
merge into #a T1
using (select -1 as ID)Q on Q.ID=T1.ID
WHEN NOT matched by target then
insert(id) values(1)
output
inserted.id
INTO #b;

Inserting unique value into database PGSQL/MSSQL

I'm trying to insert unique records in a MSSQL and Postgresql DB using insert into where not exists. But I am getting a incorrect syntax error as seen below. What am I doing wrong?
INSERT INTO settings (id, title, description)
VALUES (1, 'imageHeight', 'Image Height')
WHERE NOT EXISTS (Select * from settings where id = 1);
Error:
[Macromedia][SQLServer JDBC Driver][SQLServer]Incorrect syntax near the keyword 'WHERE'.
Try this:
INSERT INTO settings (id, title, description)
SELECT 1, 'imageHeight', 'Image Height'
WHERE NOT EXISTS (SELECT 1 FROM settings WHERE id = 1);
sql server sql fiddle
postgresql sql fiddle
WHERE is a filter for results which are not typically pertinent to INSERT operations.