sql query multiple record insertion - sql

how can i insert multiple records using single sql statement

For SQL Server 2005 you could do the following:
INSERT INTO your_table (id, field_1, field_2)
SELECT 1, 'some-data-a', 'some-data-1'
UNION ALL
SELECT 2, 'some-data-b', 'some-data-2'
UNION ALL
SELECT 3, 'some-data-c', 'some-data-3'
UNION ALL
SELECT 4, 'some-data-d', 'some-data-4';
In most modern DBMSes, including SQL Server 2008 and MySQL 5, you could use a much neater syntax:
INSERT INTO your_table (id, field_1, field_2) VALUES
(1, 'some-data-a', 'some-data-1'),
(2, 'some-data-b', 'some-data-2'),
(3, 'some-data-c', 'some-data-3'),
(4, 'some-data-d', 'some-data-4');

Using MySQL:
INSERT INTO TABLE (col1, col2) VALUES
(val1a, val1b),
(val2a, val2b),
(valNa, valNb);

You should use table types and use bulk insert.
EDIT:
http://msdn.microsoft.com/en-us/library/bb510489.aspx
This can explain further on using either one. This works for SQL server, I am not so sure about other databases. Both are very useful for a lot of records to be inserted at one go.

You can insert multiple records from a subquery as well.
INSERT INTO TABLE (col1, col2) (select a,b from SUB)

Related

Vertica - Work around for using WITH with INSERT statement

I have a SQL query like
with subtable as (
................
)
select *
from subtable
I wanted to insert the records from the select statement into a table. looking for something like :
with subtable as (......)
insert into newtable
select *
from subtable
However, in Vertica, INSERT cannot be used with the WITH ( CTE) statements.
Is there any work around for this?
Thanks for the help
with is part of the select.
insert into newtable
with subtable as (......)
select *
from subtable
I can only agree to what woot said.
In Vertica, the common table expression is supported in the SELECT statement - not in any DML statement. That's also what the ANSI standard intended.
If your point is to have the data bit at the top of the script and not "hidden" by the INSERT INTO foo line before, then you can (but still with one line before the WITH clause):
CREATE LOCAL TEMPORARY TABLE foo(id,ts,name,exp) ON COMMIT PRESERVE ROWS AS
SELECT 1,'2016-12-13 10:11'::TIMESTAMP,'Moshe',1
UNION ALL SELECT 2,'2016-12-13 12:12'::TIMESTAMP,'Karl' ,2
UNION ALL SELECT 3,'2016-12-13 13:12'::TIMESTAMP,'Karl' ,2
UNION ALL SELECT 4,'2016-12-13 14:09'::TIMESTAMP,'Moshe',2
UNION ALL SELECT 5,'2016-12-13 18:07'::TIMESTAMP,'Karl' ,2
KSAFE 0;
Marco the Sane

Insert Multiple Rows SQL Teradata

I am creating a volatile table and trying to insert rows to the table. I can upload one row like below...
create volatile table Example
(
ProductID VARCHAR(15),
Price DECIMAL (15,2)
)
on commit preserve rows;
et;
INSERT INTO Example
Values
('Steve',4);
However, when I try to upload multiple I get the error:
"Syntax error: expected something between ')' and ','."
INSERT INTO Example
Values
('Steve',4),
('James',8);
As Gordon said, Teradata doesn't support VALUES with multiple rows (and the UNION ALL will fail because of the missing FROM.
You can utilize a Multi Statement Request (MSR) instead:
INSERT INTO Example Values('Steve',4)
;INSERT INTO Example Values('James',8)
;
If it's a BTEQ job the Inserts are submitted as one block after the final semicolon (when there's a new command starting on the same line it's part of the MSR). In SQL Assistant or Studio you must submit it using F9 instead of F5.
I don't think Teradata supports the multiple row values syntax. Just use select:
INSERT INTO Example(ProductId, Price)
WITH dual as (SELECT 1 as x)
SELECT 'Steve' as ProductId, 4 as Price FROM dual UNION ALL
SELECT 'James' as ProductId, 8 as Price FROM dual;
CTE syntax (working):
insert into target_table1 (col1, col2)
with cte as (select 1 col1)
select 'value1', 'value2' from cte
union all
select 'value1a', 'value2a' from cte
;
CTE Syntax not working in Teradata
(error: expected something between ")" and the "insert" keyword)
with cte as (select 1 col1)
insert into target_table1 (col1, col2)
select 'value1', 'value2' from cte
union all
select 'value1a', 'value2a' from cte
;
I found a solution for this via RECURSIVE. It goes like this:-
INSERT INTO table (col1, col2)
with recursive table (col1, col2) as
(select 'val1','val2' from table) -- 1
select 'val1','val2' from table -- 2
union all select 'val3','val4' from table
union all select 'val5','val6' from table;
Data of line 1 does not get inserted (but you need this line). Starting from line 2, the data you enter for val1, val2 etc. gets inserted into the respective columns. Use as many UNION ALLs' as many rows you want to insert. Hope this helps :)
At least in our version of Teradata, we are not able to use an insert statement with a CTE. Instead, find a real table (preferably small in size) and do a top 1.
Insert Into OtherRealTable(x, y)
Select top 1
'x' as x,
'y' as y
FROM RealTable
create table dummy as (select '1' col1) with data;
INSERT INTO Student
(Name, Maths, Science, English)
SELECT 'Tilak', 90, 40, 60 from dummy union
SELECT 'Raj', 30, 20, 10 from dummy
;
yes you can try this.
INSERT INTO Student
SELECT (Name, Maths, Science, English) FROM JSON_Table
(ON (SELECT 1 id,cast('{"DataSet" : [
{"s":"m", "Name":"Tilak", "Maths":"90","Science":"40", "English":"60" },
{"s":"m", "Name":"Raj", "Maths":"30","Science":"20", "English":"10" }
]
}' AS json ) jsonCol)
USING rowexpr('$.DataSet[*]')
colexpr('[{"jsonpath":"$.s","type":"CHAR(1)"},{"jsonpath":"$.Name","type":"VARCHAR(30)"}, {"jsonpath":"$.Maths","type":"INTEGER"}, {"jsonpath":"$.Science","type":"INTEGER"}, {"jsonpath":"$.English","type":"INTEGER"}]')
) AS JT(id,State,Name, Maths, Science, English)

What is the SQL syntax to add multiple rows of data in one time?

Here is my code and obviously there is something wrong with it.
INSERT INTO qdel(delno,delqty)
VALUES
(192,3422);
(1,2);
(203,20);
How to insert multiple rows of data in one statement?
You trying to insert mutiple row in same table.
But your process is not valid in oracle
Try like this
INSERT ALL
INTO qdel(delno,delqty) VALUES (192,3422)
INTO qdel(delno,delqty) VALUES (1,2)
INTO qdel(delno,delqty) VALUES (203,20)
select * from dual
You can use insert . . . select:
INSERT INTO qdel(delno, delqty)
select 192, 3422 from dual union all
select 1, 2 from dual union all
select 203, 20 from dual;

Multiple INSERT queries turned into one single query [duplicate]

This question already has answers here:
Inserting multiple rows in a single SQL query? [duplicate]
(4 answers)
Closed 9 years ago.
I've searched through Stackoverflow and nothing answers my question properly.
My question is how do you turn multiple INSERT queries into 1 single insert query.
More specific; https://gist.github.com/gregariousjb/e73fdf1489acbbb63651 this one. That's the query I need to understand how to make into a single one.
Sample;
INSERT INTO `creature` (`guid`, `id`, ...)
VALUES (1, 2843, ...);
INSERT INTO `creature` (`guid`, `id`, ...)
VALUES (2, 7853, ...);
There's 1000 of these, that needs to be turned into a single one. I sincerely appreciate any help I can get on this.
If you are using Sql Server try the following
Insert into table (columns..)
Values(values1,value2,...),
(values1,value2,...),
(values1,value2,...),
(values1,value2,...)
In Mysql, do this (most popular databases have a similar syntax):
INSERT INTO mytable (col1, col2, col3, ...) VALUES
(1, 2843, 0, ...),
(2, 7853, 0, ...);
In most databases, you can do this:
INSERT INTO mytable (col1, col2, col3, ...)
SELECT 1, 2843, 0, ...
UNION ALL
SELECT 2, 7853, 0, ...;
In backward, behind-the-times databases, like Oracle, you must code this second option using the artificial single-rowed table DUAL:
INSERT INTO mytable (col1, col2, col3, ...)
SELECT 1, 2843, 0, ...
FRIM DUAL
UNION ALL
SELECT 2, 7853, 0, ...
FROM DUAL;
The closest is the shorthand version that requires the field list only once:
INSERT INTO `creature` ( <field list> ) VALUES
( <value list> ),
( <value list> ),
( <value list> )

MySQL INSERT with multiple nested SELECTs

Is a query like this possible? MySQL gives me an Syntax error. Multiple insert-values with nested selects...
INSERT INTO pv_indices_fields (index_id, veld_id)
VALUES
('1', SELECT id FROM pv_fields WHERE col1='76' AND col2='val1'),
('1', SELECT id FROM pv_fields WHERE col1='76' AND col2='val2')
I've just tested the following (which works):
insert into test (id1, id2) values (1, (select max(id) from test2)), (2, (select max(id) from test2));
I imagine the problem is that you haven't got ()s around your selects as this query would not work without it.
When you have a subquery like that, it has to return one column and one row only. If your subqueries do return one row only, then you need parenthesis around them, as #Thor84no noticed.
If they return (or could return) more than row, try this instead:
INSERT INTO pv_indices_fields (index_id, veld_id)
SELECT '1', id
FROM pv_fields
WHERE col1='76'
AND col2 IN ('val1', 'val2')
or if your conditions are very different:
INSERT INTO pv_indices_fields (index_id, veld_id)
( SELECT '1', id FROM pv_fields WHERE col1='76' AND col2='val1' )
UNION ALL
( SELECT '1', id FROM pv_fields WHERE col1='76' AND col2='val2' )