HSQLDB -Query - insert first record if does not exist - hsqldb

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)

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;

Big Query - Only insert if column value does not exist

Does Big Query support operations like "REPLACE INSERT" or something related to that?
If I run a query like this twice:
INSERT INTO table(column1) VALUES(1)
It'll create a duplicated row, is it possible to insert a row only if a column with the same value does not exist?
Thanks!
Below should make it
#standardSQL
INSERT INTO yourTable(column1)
SELECT value FROM (SELECT 1 AS value)
LEFT JOIN yourTable
ON column1 = value
WHERE column1 IS NULL
Does this work for you?
INSERT INTO table(column1)
WITH s AS (SELECT 1 src)
SELECT src FROM s WHERE NOT EXISTS (
SELECT * FROM table t WHERE t.column1 = s.src
)

How many rows are inserted

I just want to know how many rows are inserted in a table by using SQL only.
For Example: consider 2 tables TT and TT1 and I want to insert into TT1 using TT table like this:
INSERT INTO TT1
SELECT *
FROM TT
WHERE 1=1;
after executing above statement I want to know how many rows are inserted? by using Oracle SQL only. Is there any way?
The PL/SQL expression SQL%ROWCOUNT may do what you want. You would need to phrase this as:
BEGIN
INSERT INTO TT1
SELECT *
FROM TT
WHERE 1=1;
DBMS_OUTPUT.PUT_LINE('Inserted ' || SQL%ROWCOUNT)
END;
In other words, it needs to go into a PL/SQL code block.
INSERT INTO TT1
SELECT *
FROM TT
WHERE 1=1;
The number of rows inserted are the number of rows returned by the SELECT statement. Which is nothing but the count of all the rows of table TT, since 1=1 is always TRUE, basically filtering nothing.
So, just do:
SELECT COUNT(*)
FROM TT;
Update Added an example
For example,
Let's say I have a table T which is empty:
SQL> create table t as select * from emp;
Table created.
I insert rows from EMP table to T:
SQL> insert into t select * from emp;
14 rows created.
I want to know how many rows were inserted, which is nothing but the rows returned by the SELECT statement.
SQL> select count(*) from emp;
COUNT(*)
----------
14
This is in pure SQL. i.e. you executed an insert, and now want to know how many rows actually inserted by the INSERT INTO..SELECT.
However, when you have a lot of insert statements happening programmatically i.e. in PL/SQL, then you will need SQL%ROWCOUNT for every INSERT.
If you are able to use PL/SQL you can do it like
declare
i number;
begin
INSERT INTO TT1 SELECT * FROM TT WHERE 1=1;
i := sql%rowcount;
end;
Otherwise as far as I know there is only way to do it is using SELECT COUNT, if there are records before this query just run SELECT COUNT twice, one before executing and one after query then just substract them.

duplicate rows using insert into

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.

SQL query to execute insert statement from the result of another sql query

Guys i have to do a insert in to a table when there is no record found. is it possible to do it with only sql query. i tried with case and if .
Just add the condition of your 'IF' to the WHERE of the INSERT for example
INSERT INTO People
(FirstName,LastName,Email)
SELECT #FirstName,#LastName,#Email WHERE #Email NOT IN (SELECT Email FROM People)
If the email address already exists in the table then the SELECT will return no rows and hence no insert will take place
How about:
insert into table1
(select * from table2 where id not in
(select id from table1))