SQL: Insert into temp table on nested common table expression - sql

I want to insert into temp table inside the nested CTE as on below code. I can select from first x inside second expression but cannot insert into temp table.
WITH x AS
(
SELECT * FROM MyTable
),
y AS
(
SELECT * INTO #temp FROM x
)
SELECT * FROM y
I have to do this using nested CTE as there is other logic to implement. I know i can insert into temp table outside of expression. Is there a way to achieve this? Please help.

SQL Server is quite explicit. A query cannot both return a result set and save the results to a query. You can easily do:
WITH x AS (
SELECT * FROM MyTable
)
SELECT x.*
INTO #temp
FROM x;
And then:
SELECT t.*
FROM #temp t;
The first query saves the result set into a temporary table. The second returns the values from the query.

Related

Postgres single query for function and temp table it generates

I wonder if in Postgres there is any way to replace below 2-queries call by single query:
SELECT my_func(8,CURRENT_DATE);
SELECT * FROM tmp_table_generated_by_my_func
my_func is generating temp table want then use for other queries
I tried below but doesn't work (compiler seems doesn't recognise tmp table)
WITH
x AS(
SELECT * FROM my_func(8,CURRENT_DATE)
),
y AS(
SELECT * FROM tmp_table_generated_by_my_func
)
SELECT * FROM y

Save a Select/Except Union into a Temp Table

This code does precisely what I want: finds the difference between two tables, including nulls, and returns them. Thanks to: sql query to return differences between two tables
(
SELECT * FROM table1
EXCEPT
SELECT * FROM table2
)
UNION ALL
(
SELECT * FROM table2
EXCEPT
SELECT * FROM table1
)
I am having trouble getting this to turn into a temporary table (or even a regular table) to store its results for later use. Is there a way that I can tack on INSERT INTO here or generate a temp table from this beautiful query?
Select from your existing query as a sub-query INTO the temp table of your choice.
SELECT *
INTO #temp1
FROM (
(
SELECT * FROM #table1
EXCEPT
SELECT * FROM #table2
)
UNION ALL
(
SELECT * FROM #table2
EXCEPT
SELECT * FROM #table1
)
) X

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;

With structure - using multiple select queries without repeating the "with"

I would like to use a with structure with multiple sql select queries. Ex:
;with temptable as ( ... )
select id from temptable
select name from temptable
However after the first select query is done, SQL Server 2008 doesnt allow me to do it and it pushes me to write the same with structure again above the second query. Ex:
;with temptable as ( ... )
select id from temptable
;with temptable as ( ... )
select name from temptable
I tried using comma after the first select query but didnt work. How can I use multiple select queries under one with structure in SQL Server 2008.
Common table expression works only for one statement.
Specifies a temporary named result set, known as a common table
expression (CTE). This is derived from a simple query and defined
within the execution scope of a single SELECT, INSERT, UPDATE, or
DELETE statement.
select id from temptable;
select name from temptable;
are two statements, so you cannot use it in second query.
The alternative is to use temp table:
SELECT .... INTO #temptable FROM ...; -- your query from CTE
SELECT id FROM #temptable;
SELECT name FROM #temptable;
CTE only exists for the "duration of the query" you can't use the same CTE in two different SELECT statements as each is a separate query.
you need to store the result set of CTE into temp table to and fire multiple select statements on temp table.
;with temptable as ( ... )
select id
into #tempResultSet
from temptable
select name from tempResultSet

Creating #temp before CTE SQL Server 2012

I need to create a #temp table before my list of CTE:s start so that I can use it in the end to perform calculations.
This is what I have written so far:
SELECT DISTINCT
SUM(X+Y) AS Total
INTO #Summary
FROM Table
WITH CTE_START AS
(
SELECT DISTINCT *
FROM TableX
)
....
I have even tried creating my #Summary as a CTE and then writing INTO before FROM. It does not work.
I have looked at similar questions on:CTE&Temp Table
I have not found anything helpful. How can I create a #temp table before my WITH CTE_START begins processing?
You need to terminate the statement before the CTE with a semicolon, otherwise SQL Server doesn't know the WITH isn't part of the previous statement, for example part of a table hint.
For example:
SELECT DISTINCT SUM(X+Y) AS Total INTO #Summary FROM Table;
WITH CTE_START AS ( SELECT DISTINCT * FROM TableX )
SELECT * FROM CTE_START