insert temp table with multiple select statement - sql

I have a temporary table in my procedure; I tried to insert data from a select statement like this:
INSERT INTO #temptable
SELECT fee, expense, total FROM invoice
UNION
SELECT vat, holdingtax, total FROM uplifts
...but in my temptable, only the first select statement gets populated into the table, while the next select statement does not insert the data.

I assume the UNION is removing duplicated data.
I just made a test with union ALL:
insert into #temptable
select top 1 name from sys.tables
union all
select top 1 name from sys.tables a
and I got (2 row(s) affected)
try to replace UNION by UNION ALL

The syntax looks ok,
Its possible that there is duplication in the 2nd table.
You could test this using a Union All as opposed to Union.
I would just run the query without the insert to see if you return the results you expect.

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;

MS Access SQL for UNION from 3 similar tables INSERT INTO a single field in a 4th table?

Aim:
Select all distinct values from three different columns, each located in a different table: [QueryA].Att1, [QueryB].Att2, [QueryC].[Att C].
Return all selected values in a single column in a 4th table: [Table1].Field1
The 'INSERT INTO' line isn't working. The rest of it works (makes query) when 1st line is removed.
The rest of it works (makes query) when 1st line is removed.
INSERT INTO [Table1].Field1
SELECT DISTINCT [QueryA].Att1
FROM [QueryA]
UNION
SELECT DISTINCT [QueryB].Att2
FROM [QueryB]
UNION
SELECT DISTINCT [QueryC].[Att C]
FROM [QueryC];
Error message:
Syntax error in FROM clause.
In MS Access, you need to place UNION in a subquery or entirely separate query to be included in an append. Additionally, period qualifier between table and column should not be used in INSERT INTO:
INSERT INTO [Table1] (Field1)
SELECT Att1
FROM
(SELECT [QueryA].Att1
FROM [QueryA]
UNION
SELECT [QueryB].Att2
FROM [QueryB]
UNION
SELECT [QueryC].[Att C]
FROM [QueryC]
) AS sub
Alternatively, with a separate query:
INSERT INTO [Table1] (Field1)
SELECT Att1
FROM mySavedUnionQuery
could be your insert and select columns don't match (you have more than a col inf table1)
INSERT INTO [Table1] (Field1 )
SELECT [QueryA].Att1
FROM [QueryA]
UNION
SELECT [QueryB].Att2
FROM [QueryB]
UNION
SELECT [QueryC].[Att C]
FROM [QueryC];
and if you use UNION you don't need distinct

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)

SELECT values of an INSERT INTO statement

I need to select a few columns from a table1. I need to insert only one of these columns as well as some arbitrary hard coded data and insert it into table2 while also getting the original select statement back.
Basically I would like to get the results of my INSERT INTO statement instead of the "(1 row(s) affected)" that I get in SSMS.
Is there a way to do this?
Here is a SQLFiddle:
http://sqlfiddle.com/#!3/e9beb/3
Those records will insert just fine. However, I want the results of my SELECT statement to come back to me so that I can do it all at once without multiple reads or trips. Is this possible?
You can use the OUTPUT clause:
INSERT INTO Table2
OUTPUT inserted.*
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
FROM Table1
Use a batch statement and store the intermediate results in a table variable:
DECLARE #intermediate TABLE (
col1 type,
col2 type,
col3 type
);
INSERT INTO #intermediate VALUES ( ... );
INSERT INTO destinationTable SELECT * FROM #intermediate;
SELECT #intermediate;
If using this from code you can have all of this in a single command-text string.
Have you tried something like this:
INSERT INTO Table1 (
SELECT Phrase, Notes, GetDate, 1
FROM Table2
UNION
SELECT Phrase, 'This is an automatic note by the system', GETDATE(), 1
UNION
)

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.