inserting multiple rows with one insert command - sql

Is it possible to insert more than one row in a table with one insert statement?
I know this will happen if I do:
insert into table ( fields ) select values from another_table
But what if I want to insert:
row 1 - ( a1, b1, c1 )
row 2 - ( a2, b2, c2 )
...
row n - ( an, bn, cn )
with just one insert command?

Two solutions (source : http://appsfr.free.fr/spip.php?article21 ):
INSERT ALL
INTO table (column1, column2)
VALUES (value1, value2)
INTO table (column1, column2)
VALUES (value1, value2)
...etc...
SELECT * FROM DUAL ;
or
INSERT INTO table (column1, column2)
SELECT value1, value2 FROM DUAL UNION ALL
SELECT value1, value2 FROM DUAL UNION ALL
...etc...
SELECT value1, value2 FROM DUAL ;

Insert All
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;

INSERT INTO products (product_no, name, price) VALUES
(1, 'Cheese', 9.99),
(2, 'Bread', 1.99),
(3, 'Milk', 2.99);

INSERT INTO College (CustomerID, FirstName, MiddleName, LastName)
SELECT 7, N'Charles', N'Simmons', N'Burns'
UNION ALL
SELECT 9, N'Dominic', N'Fred', N'Einsten'
UNION ALL
SELECT 12, N'Dave', N'William, N'Bryan';
NOTE:
Letter N before each hard coded string value converts string to an NVARCHAR value to match the datatype of the column.

No, this is not possible. As you already stated yourself, it is only possible with a select clause providing the insert values and rows.

Related

postgresql conditionnal insert values

I need a SQL which do following work.
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
This insert will only happen when another condition is TRUE, the
condition is:
SELECT 1 FROM table_2 WHERE column_time = 'time_stamp'
Table_1 and table_2 dont have any relation. the INSERT values to table_1 should only happen if the query on Table_2 return TRUE(1)
So i try to write my SQL as following
INSERT INTO table_1 (column1, column2, column3) VALUES (:1,:2, :3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
However, this SQL not work as Postgresql looks dont like the INSERT combine with WHERE condition.
BTW: the values (:1, :2:, :3) are bind array values. the final SQL looks like this:
INSERT INTO table_1 (column1, column2, column3) VALUES ('ca_1','cb_1', 'cc_1') ('ca_2','cb_2', 'cc_2') ... ('ca_n','cb_n', 'cc_n') WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'my_time_stamp') ON CONFLICT DO NOTHING
Really need help thanks.
You need to select those values.
This would work in Postgres:
INSERT INTO table_1 (column1, column2, column3)
SELECT :1, :2, :3
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING
Alternatively:
INSERT INTO table_1 (column1, column2, column3)
SELECT x.*
FROM (VALUES (:1, :2, :3)) AS x(column1, column2, column3)
WHERE EXISTS (SELECT 1 FROM table_2 WHERE column_time = 'time_stamp')
ON CONFLICT DO NOTHING

SQL - Concat in where clause with IN

I'm trying use IN to query multiple columns. If I use "=", I return rows (see example) but I would like to query multiple.
need query table to return rows A12345 and B98765 but not C00000
column1 | column2
A 12345
B 98765
C 00000
This works
SELECT *
FROM TABLE
WHERE (column1,column2) = ('A',12345)
This does not work.
SELECT *
FROM TABLE
WHERE (column1,column2) IN (('A',12345),('B',98765))
Here is error:
Error: SQL0104N An unexpected token "," was found following ",". Expected tokens may include: "AT MICROSECONDS MICROSECOND SECONDS SECOND MINUTES MINUTE HOURS". SQLSTATE=42601
(State:42601, Native Code: FFFFFF98)
I've tried several variations of parenthesis, commas's, etc and can't get it to work. Is it possible to do this and if so can you provide the syntax.
thanks
DB2 9.7 onwards:
SELECT *
FROM TABLE
WHERE (column1, column2) IN (VALUES ('A', 12345), ('B', 98765))
One solution is to use a temp table:
create table #tmp
(
col1 varchar(2),
col2 int
);
insert into #tmp (col1, col2)
values ('A', 12345), ('B', 98765)
SELECT t.* FROM TABLE t
JOIN #tmp ON #tmp.col1 = t.column1 and #tmp.col2 = t.column2
You haven't stated your RDBMS, but have you tried:
SELECT * FROM TABLE
WHERE (column1, column2) IN ('A', 12345)
OR (column1, column2) IN ('B', 98765)
Depending on your data, this might be workable:
SELECT *
FROM TABLE T
JOIN (
SELECT 'A' COL1, 12345 COL2
UNION ALL
SELECT 'B', 98765
UNION ALL
SELECT 'C', 44365) AS Matches
ON T.column1 = Matches.COL1
AND T.column2 = Matches.COL2
This turned out to be easy and should have known this.
SELECT *
FROM TABLE
WHERE CONCAT(column1, column2) IN ('A12345','B98765')

Inserting data into Oracle table (SQL)

I already have a table built in oracle.
Im trying to insert some data like this:
INSERT INTO movies_actor('name','id')
VALUES ('Nuno','2'), ('Pedro','3'), ('Jose','1');
select * from movies_actor;
I always get this error
ORA-00928: missing SELECT keyword
What am I doing wrong?
I don't think you need the single quote around your field names.
You need to do:
INSERT INTO TableName(Column1, Column2)
VALUES('Nuno', '2');
In your example, it would be:
INSERT INTO movies_actor(name, id)
VALUES ('Nuno','2');
INSERT INTO movies_actor(name, id)
VALUES ('Pedro','3');
INSERT INTO movies_actor(name, id)
VALUES ('Jose','1');
select * from movies_actor;
Another way.
insert into table
(field1, field2)
select value1, value2
from dual
union
select value3, value4
from dual
etc
You cannot insert multiple records in one statement using VALUES. You can either use Tenzin's solution or use INSERT ALL :
INSERT ALL
INTO movies_actor(name, id) VALUES ('Nuno', '2')
INTO movies_actor(name, id) VALUES ('Pedro', '3')
INTO movies_actor(name, id) VALUES ('Jose', '1')
SELECT * FROM dual;

Oracle, Updating multiple rows in one query, using prepared statement

I know that you can insert multiple rows in a query by using
INSERT ALL
INTO mytable (column1, column2, column_n) VALUES (?,?,?)
INTO mytable (column1, column2, column_n) VALUES (?,?,?)
INTO mytable (column1, column2, column_n) VALUES (?,?,?)
SELECT * FROM dual;
Is there a way for update as well based on a value? So it would be something like
UPDATE ALL
SET mytable (column1, column2, column_n) VALUES (?,?,?)
SET mytable (column1, column2, column_n) VALUES (?,?,?)
SET mytable (column1, column2, column_n) VALUES (?,?,?)
WHERE ID= ?
SELECT * FROM dual;
For instance i have a reviewtable
reviewid bookid authorname authoremail
1 1 peter wdwdd
2 1 jane dwdwdw
3 1 mary dwdw
Is it possible to do a multiple update where bookid = "1" ?
You can use MERGE:
MERGE INTO reviewtable r
USING ( SELECT 1 AS reviewid,
1 AS bookid,
'peter smith' AS name,
'p.smith#email' AS email
FROM DUAL
UNION ALL
SELECT 2, 1, 'jane blogs', 'j.blogs#email' FROM DUAL
UNION ALL
SELECT 3, 1, 'mary adams', 'm.adams#email' FROM DUAL
) src
ON ( r.bookid = src.bookid AND r.reviewid = src.reviewid )
WHEN MATCHED THEN UPDATE
SET authorname = src.name,
authoremail = src.email;

INSERT INTO subtract 2 values

Is it possible for an INSERT query to subtract 2 values you have entered to create a 3rd value that can then be inserted into a table - if that makes sense...
e.g.
INSERT INTO table1 (column1, column2, column3)
VALUES ('50', '25', column1 - column2)
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', column1 - column2) from table1 where conditions)
This is a sample query! hope it helps!
Convoluted:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
(select 50 as column1,
25 as column2
) t
Since you can't reference other columns from the same SELECT clause, you have to do it as a subquery. I've also switched to using int literals rather than strings, because I can't make subtraction make sense in my head otherwise.
You could also do it using a Table Value Constructor:
INSERT INTO table1 (column1,column2,column3)
select column1,column2,column1-column2
from
( VALUES (50, 25)
) AS t (column1, column2);
As indicated in my comment though, if the relationship should always hold, I'd build table1 as:
CREATE TABLE table1 (
column1 int not null,
column2 int not null,
column3 as column1 - column2
--More columns
)
Because that way, the column3 value is always correct.
You can create function that subtracts values and use this function in insert. This is the right way to do such things:
INSERT INTO table1 (column1, column2, column3)
(select ('50', '25', your_function() ) from table1 where conditions)
/
Using the "INSERT INTO" would do this:
INSERT INTO Table1Name (column1, column2, column3,)
(select 'X', 'Y', X - Y as Z)
Here is a link to SQL Authority with more examples of INSERT INTO
Another method would to be add a trigger to the table, where on insert of data, the third column would be updated with the difference of the first two columns.