Inserting multiple rows query failed [duplicate] - sql

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

Related

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 multiple records in oracle [duplicate]

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;

Subquery cannot appear in an Insert Values statement

I have the following query in SQL Server CE which gives me an error during the execution time:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
VALUES('p', 104, (select MAX(expence_id) from c_expence))
The error is this:
Subquery cannot appear in an Insert Values statement.
What is wrong with this query?
Try this one:
INSERT INTO trans_rel
SELECT 'p', '102', MAX(expence_id)
FROM c_expence
This is exactly what your are looking for:
INSERT INTO trans_rel(trans, sale_purch_id, inc_exp_id)
SELECT 'p' as 'trans', '104' as 'sale_purch_id', MAX(expence_id) AS inc_exp_id
FROM c_expence;

Insert statement in SQL Server 2005

I have strange problem related to SQL Server 2005
When I try to insert into table
insert into IDName
VALUES (101 , 'AA'),
(301 , 'BB')
I receive this error
Msg 102, Level 15, State 1, Line 3
Incorrect syntax near ','.
There is no problem, if I insert records one by one.
EDIT:
Thanks for reply guys.... but this script works in other installation of sql server 2005... I think it is some setting issue but i do not where... if you could help
This syntax was introduced in SQL Server 2008. So upgrade, or use the more verbose:
INSERT dbo.IDName(column1, column2)
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB';
Some additional changes:
Always use the schema prefix when referencing objects
Always specify the column list for an INSERT
Always use semi-colons to terminate statements
SQL Server 2005 does not support that insert syntax; you would either need
insert into IDName
SELECT 101 , 'AA'
UNION ALL SELECT 301 , 'BB'
or
insert into IDName VALUES (101 , 'AA');
insert into IDName VALUES (301 , 'BB');