duplicate rows using insert into - sql

I have a table that contains data and I want to use insert into statement
here is my query
INSERT INTO [table]
SELECT Distinct * FROM [DataSource]
how to to ignore a row from being inserted if it's already found in the table?

Assuming your target table and your data source table have identical structure, you could do something like this:
INSERT INTO [table]
SELECT Distinct *
FROM [DataSource]
EXCEPT
SELECT *
FROM [table]
Alternatively, you could use MERGE statement or SELECT ... WHERE NOT EXISTS.

Related

How to add union data in table?

Thanks in advance !!
I want to get below data in separate table with column how can we achieved this.
From my reading of your question, you would like the results of that SELECT statement put into a new table?
Firstly, I'm assuming your original SQL works as a SELECT statement - e.g., all those tables have the same structure. Note that you can simplify the unions, but I haven't done so here, to keep the key part of the answer (saving the data) as the main focus.
To save the data into another table, you can either create a table first and make that into an insert, or just use 'SELECT INTO' within the main SELECT.
If you are happy with the columns being automatically created, the 'SELECT INTO' version will create columns (e.g., you do not need to specify the columns in a CREATE TABLE statement). However, when you run the SELECT INTO, it does create the table. Therefore if you want to insert further values, you need to specify the column list (or have matching column lists).
SELECT INTO version
select *
INTO #Temp -- Added This row
from
( select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
) A
except
select * from
( select * from #ONEYRCON
union all
select * from #OTHERYRCON
) B
INSERT INTO version
CREATE TABLE #Temp (<your fields here to match the SELECT statement>)
INSERT INTO #Temp
select * from
( select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
) A
except
select * from
( select * from #ONEYRCON
union all
select * from #OTHERYRCON
) B
Set operators are evaluated from top to bottom so there only needs to be 1 subquery. Something like this
select ab.* into #Temp
from (select * from #OneyearExpiry
union all
select * from #OtherYearExpiry
except
select * from #ONEYRCON
except
select * from #OTHERYRCON) ab;

Insert into table using select plus value?

I created a table modeled on an existing table:
CREATE TABLE new_table AS
(SELECT * FROM old_table WHERE record_status = 'xyz' ) WITH DATA;
I then added a column:
ALTER TABLE new_table ADD new_column VARCHAR(100);
I now want to select more rows from the old table into the new table, but add a fixed value (eg. 'blabla') to the new column for each row selected. I tried a bunch of things, such as:
INSERT INTO new_table
SELECT *, 'blabla'
FROM old_table)
WHERE is_blabla = 'Y'
Tried every permutation I could think of but keep getting syntax errors. Any suggestions? I'm using Teradata.
Dave
INSERT INTO new_table
SELECT old_table.*,'your_fixed_value'
FROM old_table
WHERE is_blabla='Y'
You just need to alias your table
INSERT INTO new_table
SELECT a.*,'your_fixed_value'
FROM old_table a
WHERE is_blabla='Y'

HSQLDB -Query - insert first record if does not exist

Note: This question is specifically for HSQLDB and Informix.
I want to insert new entry if not exists in the table and I know we can use exists query to insert based on existing entry.
Eg:
INSERT INTO test(column1)
(SELECT DISTINCT 3
FROM test
WHERE NOT EXISTS (SELECT * FROM test WHERE column1 =3));
Problem is: The EXISTS condition is used in combination with a subquery and is considered to be met, if the subquery returns at least one row. Then only we can use in a SELECT, INSERT, UPDATE, or DELETE statement.
This is the CREATE table statement:
CREATE TABLE test(column1 int)
This is the INSERT statement for HSQLDB. It uses the SQL Standard syntax and should work with Informix as well:
INSERT INTO test(column1)
SELECT * FROM (VALUES (3))
WHERE NOT EXISTS (SELECT * FROM test WHERE column1 =3)
This is the SELECT statement to show the rows in the table
SELECT * FROM test
This is the result of the SELECT
COLUMN1
-------
3
1 row(s) in 0 ms
As Informix does not support the VALUES table constructor, you need to create a separate table with only one row, similar to Oracle's DUAL table. You then use this table inside the SELECT.
INSERT INTO test(column1)
SELECT 3 FROM single_row_table
WHERE NOT EXISTS (SELECT * FROM test WHERE column1 =3)

insert into value in select statement mutliple values

I try to insert into table with 1 column is (select from table).
I should copy all the id to table1 with 1 column is (select from table )
This not working:
insert into table1 (id,resoucrce,rate) values ((select id from table2),0,0)
I want to do something like that insert all the id from table to another table with default values.
Use insert . . . select:
insert into table1 (id, resource, rate)
select id, 0, 0
from table2;
For copy the content of one table to another table within the same database use this :-
INSERT INTO TARGET_TABLE (`col1`,`col2`) SELECT `col1`,`col2` FROM SOURCE_TABLE;
or your query like that :-
Insert into table1 (id, resource, rate) select id, 0, 0 from table2;
You should focus "values" keyword;
values ((select id from table2),0,0)
When you use values(, , ,) you have to specify value columns. But you're trying to pass both resultset and single values together. That's why you get an error. You should only pass single values...
If its clear for you; you can easily find the correct sql syntax.

Copy rows from one table to another in MySQL

I have two tables with the same structure. I need to select data from one table and have to store them into the other.
How can I do that?
insert into tablea(id,name) select id,name from tableb;
Since they are the same structure then you can just do
insert into table1 select colum1, column2,... from table2
insert into blacklist
select *
from newblacklist
for uploading any single number
insert into blacklist
select *
from newblacklist
where numbers ='123456'
leave out the values keyword
insert into tbl1
select * from tbl2