How to insert multiple insert sql statement - sql

There is a table Person(id, name). I am inserting more than 1000 records into person table. Both id and name should be unique. I wrote something like this
INSERT ALL
INTO PERSON (1, 'MAYUR')
INTO PERSON (2, 'SALUNKE')
.....(1000 records)
SELECT * FROM DUAL;
I am getting unique constraint for name in this query. How do I know which record in particular is failing. All I see in logs is this
Error starting at line : 3 in command - ORA-00001: unique constraint
(UN_PERSON_NAME) violated.
This does not tell the exact record which is duplicate.

You are missing values keyword. Try this!
INSERT ALL
INTO PERSON values(1, 'MAYUR')
INTO PERSON values(2, 'SALUNKE')
.....(1000 records)
SELECT * FROM DUAL;

INSERT INTO table2 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
FROM table1

Unfortunately, Oracle doesn't support multiple inserts using a single VALUES() statement. I usually approach this as:
INSERT PERSON (id, name)
SELECT 1, 'MAYUR' FROM DUAL UNION ALL
SELECT 2, 'SALUNKE' FROM DUAL UNION ALL
.....;
One advantage of this approach is you can use a subquery and assign the id:
INSERT PERSON (id, name)
SELECT rownum, x.name
FROM (SELECT 'MAYUR' FROM DUAL UNION ALL
SELECT 'SALUNKE' FROM DUAL UNION ALL
.....
) x

Related

Insert new records with several ids from another table

I have a table, which has 9 records with id = 1 - 9 (for example, there can be more than 20 ids).
I have one varchar value = 'premium'.
I need to insert these values to another table, after this action I should have 9 records with id from the first table and 'premium' varchar in the second table:
1, 'premium';
2, 'premium';
etc.
How to write the function for SQL?
Are you looking for insert . . . select or create table as?
insert into table2 (id, value)
select id, 'premium'
from table1;
or:
create table table2 as
select id, 'premium' as value
from table1;
Do you want this?
demo:db<>fiddle
INSERT INTO second_table (id, text_value)
SELECT id, 'premium'
FROM first_table;

Removing true duplicates from greenplum table

I am trying to remove true duplicates from a table. I have removed dupes multiple times in past but I'm not able to figure what's wrong with my syntax with this one.
My code -
DELETE
FROM my_table_name
WHERE (
column1, column2, column3, column4, column5, column6, column7, column8, column9) IN
(
SELECT Row_number() OVER( partition BY column1, column2,column3, column4,column5,column6,column7,column8 ORDER BY column2 DESC, column3 ASC ) AS row_num,
column1,
column2,
column3,
column4,
column5,
column6,
column7,
column8,
column9
FROM my_table_name
WHERE column1='some_value') a
WHERE row_num=2;
Error
********** Error **********
ERROR: syntax error at or near ""a""
SQL state: 42601
Character: 1607
I can see that the error is on creating the alias a subquery. But I'm not able to pin point what's wrong here.
Any help is appreciated
Edit 1 -
If I remove a, I get the below error
********** Error **********
ERROR: syntax error at or near "where"
SQL state: 42601
Character: 1608
If you have duplicate rows, you can't just delete all but one of the records in a single command. You have to delete all duplicates and then insert just one version for each duplicate row or build new table (preferred) without duplicates.
Let's start with the preferred method which is to create a new table without the duplicates. This solution utilizes disk space in the most efficient way possible rather than having a fragmented table.
Example:
create table foo
(id int, fname text)
with (appendonly=true)
distributed by (id);
Insert some data with duplicates:
insert into foo values (1, 'jon');
insert into foo values (1, 'jon');
insert into foo values (2, 'bill');
insert into foo values (2, 'bill');
insert into foo values (3, 'sue');
insert into foo values (4, 'ted');
insert into foo values (4, 'ted');
insert into foo values (4, 'ted');
insert into foo values (4, 'ted');
Create a new version of the table without the duplicates:
create table foo_new with (appendonly=true) as
select id, fname
from (
select row_number() over (partition by id) as row_num, id, fname
from foo
) as sub
where sub.row_num = 1
distributed by (id);
And now rename the tables:
alter table foo rename to foo_old;
alter table foo_new rename to foo;
The second method is to use DELETE but you'll see that it needs more steps to complete.
First, create a temp table with the IDs you want to delete. You typically don't have primary keys enforced in Greenplum but you still have a logical PK. Columns like customer_id, product_id, etc are all in your data. So, find the dups first based on the PK.
drop table if exists foo_pk_delete;
create temporary table foo_pk_delete with (appendonly=true) as
select id
from foo
group by id
having count(*) > 1
distributed by (id);
Next, get the entire row for each duplicate but only one version of it.
drop table if exists foo_dedup;
create temporary table foo_dedup with (appendonly=true) as
select id, fname
from (
select row_number() over (partition by f.id) as row_num, f.id, f.fname
from foo f
join foo_pk_delete fd on f.id = fd.id
) as sub
where sub.row_num = 1
distributed by (id);
Now you can delete the duplicates:
delete
from foo f
using foo_pk_delete fk
where f.id = fk.id;
And then you can insert the deduplicated data back into the table.
insert into foo (id, fname)
select id, fname from foo_dedup;
You'll want to vacuum your table after this data manipulation.
vacuum foo;

Condition before insert SQL

In DB2 SQL, I want to write something like
insert into employees
(id, name) values (1, "emp1")
where (select count(*) from employee_registry) <= 10
Can't figure out the correct syntax though.
post edit.
i wanted to insert records only if some condition is met.
You need to have a SELECT statement somewhere in order to use the WHERE clause, so something like this might work:
insert into employees (id, name)
select 1, 'emp1' from sysibm.sysdummy1
where (select count(*) from employee_registry) <= 10
sysibm.sysdummy1 is a special system table that always has only one row.
Other method, group by Nothing + having :
insert into employees (id, name)
select '1', 'emp1' from employee_registry
group by 1 having count(*)<=10

Combining INSERT SELECT with constants

I'm trying to insert records into a table where one of the columns comes from another table.
The other two columns are the same for each record.
All three columns are keys.
I'm trying this embedded INSERT SELECT which I see is not allowed?
INSERT INTO TABLE (COLUMN_A, COLUMN_B, COLUMN_C)
VALUES (1,(SELECT COLUMN_NAME FROM TABLE) ,2)
Your syntax is off - this is the correct syntax for this:
Insert Into Table
(Column_A, Column_B, Column_C)
Select 1, Column_Name, 2
From OtherTable
I would post a comment but I don't have enough 'status' yet.
The insert below works in PostgreSQL. What database are you using?
insert into people_experiences (qty, created_at, updated_at, person_id, experience_id) values (1, now(), now(), (select id from people where first_name='Joseph'), (select id from experiences where experience='MS'));
INSERT 0 1
We can even use this
Insert Into Table
(Select "value-1" as column, "value-2" as column, ot.* from otherTable ot)
for two column i want constant values and rest should be use as it is.
Note: Insert select will not allow virtual column.

How to do one where BEFORE multiple inserts in SQL Server

I'm doing a multiple insert in SQL Server using UNION ALL between the inserts. In the last part of the query I have a WHERE clause. Now it seems that that the WHERE clause is executed before every statement, but I only want the WHERE to be executed one time. If the WHERE clause has a result then none of the inserts should be executed.
For illustration, insert some persons into a table, if any records exists with one of the defined ages none of the inserts should be executed.
INSERT INTO mytable
select 1, 33,john UNION ALL
select 2, 28,james UNION ALL
select 3, 20,Harry UNION ALL
WHERE NOT EXISTS (SELECT 1 FROM mytable where age in(22,28,30))
How should I do this?
Try this instead:
INSERT INTO mytable
(id, age, name)
SELECT * FROM
(
SELECT 1 AS id, 33 AS age, 'john' AS name
UNION ALL
SELECT 2, 28, 'james'
UNION ALL
SELECT 3, 20, 'Harry'
) T1
WHERE NOT EXISTS (SELECT 1 FROM mytable WHERE age IN (22, 28, 30))