Insert multiple records in Oracle Database - sql

Example:
INSERT INTO TABLE ( USERID, USERNAME) VALUES (1,"ok1"),(2,"ok2");
As i understood ,Multiple rows insertion not allowed in Oracle Database.
Please confirm me if any other alternatives for inserting multiple records into oracle DB with above format.

Oracle only allows one row to be inserted at a time. So use two inserts:
INSERT INTO TABLE ( USERID, USERNAME)
VALUES (1, 'ok1');
INSERT INTO TABLE ( USERID, USERNAME)
VALUES (2, 'ok2');
Or use INSERT . . . SELECT:
INSERT INTO TABLE ( USERID, USERNAME)
SELECT 1 'ok1' FROM DUAL UNION ALL
SELECT 2, 'ok2' FROM DUAL;

Related

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;

How to insert table1 filed to another table2 with same table2.filed

MS SQL- 2014
I have table as temp (time, name) and Name (name)
In Name table we have data as (Ram, Shyam).
I want to insert data as Insert Into temp (time, name) Values (10:30, 'Ram') , (10:30, 'Shyam' );
But Want to automate to insert data on temp table as Insert should take time as current time and insert all the row from Name table.
Does it possible using SQL query?
You can insert values from Name table as below
INSERT INTO temp (time, name) SELECT GETDATE(), Name from Name

Stored Procedure Insert (Select and Values)

I'm looking to have a stored procedure that will:
run through Table A and retrieve all IDs.
insert into Table B all IDs (loop) but also static values which aren't found in Table A.
How do I approach this?
CREATE OR REPLACE PROCEDURE TEST AS
BEGIN
select ID from TABLE A;
INSERT INTO TABLE B
(
created_date,
created_by,
ID
)
VALUES ('sysdate', '1', 'RESULTS FROM SELECT QUERY');
END TEST;
Not sure how to merge static data ('sysdate' and '1') with results from a query.
No need for 2 separate queries. This should work with INSERT INTO SELECT:
INSERT INTO TABLEB
(
created_date,
created_by,
ID
)
SELECT 'sysdate', '1', id
FROM TABLEA

Insert data in 3 tables at a time using Postgres

I want to insert data into 3 tables with a single query.
My tables looks like below:
CREATE TABLE sample (
id bigserial PRIMARY KEY,
lastname varchar(20),
firstname varchar(20)
);
CREATE TABLE sample1(
user_id bigserial PRIMARY KEY,
sample_id bigint REFERENCES sample,
adddetails varchar(20)
);
CREATE TABLE sample2(
id bigserial PRIMARY KEY,
user_id bigint REFERENCES sample1,
value varchar(10)
);
I will get a key in return for every insertion and I need to insert that key in the next table.
My query is:
insert into sample(firstname,lastname) values('fai55','shaggk') RETURNING id;
insert into sample1(sample_id, adddetails) values($id,'ss') RETURNING user_id;
insert into sample2(user_id, value) values($id,'ss') RETURNING id;
But if I run single queries they just return values to me and I cannot reuse them in the next query immediately.
How to achieve this?
Use data-modifying CTEs:
WITH ins1 AS (
INSERT INTO sample(firstname, lastname)
VALUES ('fai55', 'shaggk')
-- ON CONFLICT DO NOTHING -- optional addition in Postgres 9.5+
RETURNING id AS sample_id
)
, ins2 AS (
INSERT INTO sample1 (sample_id, adddetails)
SELECT sample_id, 'ss' FROM ins1
RETURNING user_id
)
INSERT INTO sample2 (user_id, value)
SELECT user_id, 'ss2' FROM ins2;
Each INSERT depends on the one before. SELECT instead of VALUES makes sure nothing is inserted in subsidiary tables if no row is returned from a previous INSERT. (Since Postgres 9.5+ you might add an ON CONFLICT.)
It's also a bit shorter and faster this way.
Typically, it's more convenient to provide complete data rows in one place:
WITH data(firstname, lastname, adddetails, value) AS (
VALUES -- provide data here
('fai55', 'shaggk', 'ss', 'ss2') -- see below
, ('fai56', 'XXaggk', 'xx', 'xx2') -- works for multiple input rows
-- more?
)
, ins1 AS (
INSERT INTO sample (firstname, lastname)
SELECT firstname, lastname -- DISTINCT? see below
FROM data
-- ON CONFLICT DO NOTHING -- UNIQUE constraint? see below
RETURNING firstname, lastname, id AS sample_id
)
, ins2 AS (
INSERT INTO sample1 (sample_id, adddetails)
SELECT ins1.sample_id, d.adddetails
FROM data d
JOIN ins1 USING (firstname, lastname)
RETURNING sample_id, user_id
)
INSERT INTO sample2 (user_id, value)
SELECT ins2.user_id, d.value
FROM data d
JOIN ins1 USING (firstname, lastname)
JOIN ins2 USING (sample_id);
db<>fiddle here
You may need explicit type casts in a stand-alone VALUES expression - as opposed to a VALUES expression attached to an INSERT where data types are derived from the target table. See:
Casting NULL type when updating multiple rows
If multiple rows can come with identical (firstname, lastname), you may need to fold duplicates for the first INSERT:
...
INSERT INTO sample (firstname, lastname)
SELECT DISTINCT firstname, lastname FROM data
...
You could use a (temporary) table as data source instead of the CTE data.
It would probably make sense to combine this with a UNIQUE constraint on (firstname, lastname) in the table and an ON CONFLICT clause in the query.
Related:
How to use RETURNING with ON CONFLICT in PostgreSQL?
Is SELECT or INSERT in a function prone to race conditions?
Something like this
with first_insert as (
insert into sample(firstname,lastname)
values('fai55','shaggk')
RETURNING id
),
second_insert as (
insert into sample1( id ,adddetails)
values
( (select id from first_insert), 'ss')
RETURNING user_id
)
insert into sample2 ( id ,adddetails)
values
( (select user_id from first_insert), 'ss');
As the generated id from the insert into sample2 is not needed, I removed the returning clause from the last insert.
Typically, you'd use a transaction to avoid writing complicated queries.
http://www.postgresql.org/docs/current/static/sql-begin.html
http://dev.mysql.com/doc/refman/5.7/en/commit.html
You could also use a CTE, assuming your Postgres tag is correct. For instance:
with sample_ids as (
insert into sample(firstname, lastname)
values('fai55','shaggk')
RETURNING id
), sample1_ids as (
insert into sample1(id, adddetails)
select id,'ss'
from sample_ids
RETURNING id, user_id
)
insert into sample2(id, user_id, value)
select id, user_id, 'val'
from sample1_ids
RETURNING id, user_id;
You could create an after insert trigger on the Sample table to insert into the other two tables.
The only issue i see with doing this is that you wont have a way of inserting adddetails it will always be empty or in this case ss. There is no way to insert a column into sample thats not actualy in the sample table so you cant send it along with the innital insert.
Another option would be to create a stored procedure to run your inserts.
You have the question taged mysql and postgressql which database are we talking about here?

Get identity column values upon INSERT

I am using SQL Server 2008 to create a procedure.
I am using the following sql statement to insert into a audit table
insert into Listuser
(
UserID,
ListID,
AuditCreated
)
select
UserID,
ListID,
GETDATE()
from ListUser where Surname='surname'
I am using scope_identity() to get the identity column from the listuser table and insert the identity column to another logs table
If the select statement contains more than 1 value, how to get the identity value of both columns and insert into logs table?
Thanjs
If you need to capture multiple identity values being inserted, I'd use the OUTPUT clause:
DECLARE #TableOfIdentities TABLE (IdentValue INT)
INSERT INTO dbo.Listuser(UserID, ListID, AuditCreated)
OUTPUT Inserted.ID INTO #TableOfIdentities(IdentValue)
SELECT
UserID, ListID, GETDATE()
FROM
dbo.ListUser
WHERE
Surname = 'surname'
This will insert all rows into your ListUser table, and it will output all identities generated by this INSERT into the #TableOfIdentities table variable
Read more about the OUTPUT clause on MSDN
These values are inserted into the table variable, and you can select them out from that table variable after the insert, and do whatever you need to do with them:
SELECT * FROM #TableOfIdentities
Update: the use of the table variable here is just as an example - of course you can also output the data into a "normal" table in SQL Server - and you can also output multiple values for each inserted row, if you need that - so you can have something like:
INSERT INTO dbo.Listuser(UserID, ListID, AuditCreated)
OUTPUT Inserted.ID, Inserted.UserID, Inserted.SurName, GETDATE()
INTO AuditTable(IdentValue, UserID, Surname, InsertDate)
SELECT
UserID, ListID, GETDATE()
FROM
dbo.ListUser
WHERE
Surname = 'surname'