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

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

Related

Should I Use a Temp Table To Optimize?

I have a somewhat complex query that I need to access for the IDs in order to delete from multiple tables, something along the lines of:
DELETE FROM Table1 WHERE ID IN ( -- Query here -- )
DELETE FROM Table2 WHERE ID IN ( -- Query here -- )
Would selecting the query into a temp table be more efficient than writing out the entire query twice, or is it just visually cleaner?
SELECT ( -- Query here -- ) INTO #Temp
DELETE FROM Table1 WHERE ID IN ( SELECT * FROM #Temp )
DELETE FROM Table2 WHERE ID IN ( SELECT * FROM #Temp )
Also, am open to other suggestions that I may have overlooked.
Thanks in advance
Completely agree with Jeroen Mostert. Try it and see. You could although try to use CTE for deletion or table variables and select distinct values of IDs there in case you have no distinct values of IDs in your query. And if you have a lot of data to delete, try to create additional indexes in temp table.

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;

hive "with tbl as" vs "create table tbl as"

Does < with tbl as > much faster than < create table tbl as > ?
with tbl as
(
select
id,name
from
a
)
select id from tbl;
create table tbl
as
select
id,name
from
a;
select id from tbl;
If I want use tbl in many querys, how to use < with tbl as >?
with tbl as
(
select
id,name
from
a
)
select id from tbl;
select name from tbl;
There's no obvious performance gap.
with tbl as is a common table expression, aka CTE, which is only accessible within a single query. So we CAN NOT use CTE across multiple SQL queries separated by ;.
Favor create temporary table table over create table. The former is visible within a single session, and will be gone when the session ends.

SQL: Insert into temp table on nested common table expression

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.

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