You can have multiple CTEs in one query, as well as reuse a CTE:
WITH cte1 AS
(
SELECT 1 AS id
),
cte2 AS
(
SELECT 2 AS id
)
SELECT *
FROM cte1
UNION ALL
SELECT *
FROM cte2
UNION ALL
SELECT *
FROM cte1
Note, however, that SQL Server may reevaluate the CTE each time it is accessed, so if you are using values like RAND(), NEWID() etc., they may change between the CTE calls.
Related Posts:
- Inserting multiple rows in a single SQL query? [duplicate]
- T-SQL split string
- How do I perform an IF…THEN in an SQL SELECT?
- Each GROUP BY expression must contain at least one column that is not an outer reference
- SQL Server : Arithmetic overflow error converting expression to data type int
- How to calculate percentage with a SQL statement
- What is the meaning of the prefix N in T-SQL statements and when should I use it?
- Query error with ambiguous column name in SQL
- Why use a READ UNCOMMITTED isolation level?
- How to group by month from Date field using sql
- SQL – The conversion of a varchar data type to a datetime data type resulted in an out-of-range value
- How can I get column names from a table in SQL Server?
- Is there a combination of “LIKE” and “IN” in SQL?
- How to get a date in YYYY-MM-DD format from a TSQL datetime field?
- MS SQL compare dates?
- MS SQL compare dates?
- Get size of all tables in database
- Using RegEx in SQL Server
- How to format a numeric column as phone number in SQL
- How do I format a number with commas in T-SQL?
- SQL: IF clause within WHERE clause
- To add server using sp_addlinkedserver
- Comma separated results in SQL
- Selecting COUNT(*) with DISTINCT
- What is a stored procedure?
- LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
- What do Clustered and Non-Clustered index actually mean?
- LEFT JOIN vs. LEFT OUTER JOIN in SQL Server
- How to drop a table if it exists?
- SQL Server: Difference between PARTITION BY and GROUP BY
- Self Join to get employee manager name
- How do I UPDATE from a SELECT in SQL Server?
- SQL query to select dates between two dates
- When should I use CROSS APPLY over INNER JOIN?
- SQL query to get the employee name and their manager name from the same table
- How to create Temp table with SELECT * INTO tempTable FROM CTE Query
- How can I do an UPDATE statement with JOIN in SQL Server?
- Function to Calculate Median in SQL Server
- The SQL OVER() clause – when and why is it useful?
- Update multiple columns in SQL
- SQL Query with NOT LIKE IN
- Function to Calculate Median in SQL Server
- How do I escape a single quote in SQL Server?
- SQL Server IF EXISTS THEN 1 ELSE 2
- T-SQL split string based on delimiter
- What is the use of GO in SQL Server Management Studio & Transact SQL?
- SQL Query with SUM with Group By
- How do I escape a single quote in SQL Server?
- IDENTITY_INSERT is set to OFF – How to turn it ON?
- SQL update from one Table to another based on a ID match
- An attempt was made to access a socket in a way forbidden by its access permissions
- Conversion of a varchar data type to a datetime data type resulted in an out-of-range value in SQL query
- How to drop all tables from a database with one SQL query?
- Optimistic vs. Pessimistic locking
- Column name or number of supplied values does not match table definition
- SQL Server FOR EACH Loop
- The target principal name is incorrect. Cannot generate SSPI context
- Query comparing dates in SQL
- The EXECUTE permission was denied on the object ‘xxxxxxx’, database ‘zzzzzzz’, schema ‘dbo’
- BCP error “Unable to open BCP host data-file”
- How can I clone an SQL Server database on the same server in SQL Server 2008 Express?
- Each GROUP BY expression must contain at least one column that is not an outer reference
- SQL Server : error converting data type varchar to numeric
- How to drop all tables from a database with one SQL query?
- Sql Server string to date conversion
- How to find sum of multiple columns in a table in SQL Server 2005?
- Check if table exists in SQL Server
- Conversion failed when converting from a character string to uniqueidentifier – Two GUIDs
- Operand type clash: int is incompatible with date + The INSERT statement conflicted with the FOREIGN KEY constraint
- How can I delete using INNER JOIN with SQL Server?
- Find all tables containing column with specified name – MS SQL Server
- DateTime2 vs DateTime in SQL Server
- Conversion of a varchar data type to a datetime data type resulted in an out-of-range value in SQL query
- CREATE TABLE IF NOT EXISTS equivalent in SQL Server
- How can I import an Excel file into SQL Server?
- sql server invalid object name – but tables are listed in SSMS tables list
- SQL Server String or binary data would be truncated
- How to parse XML data in SQL server table
- Search text in stored procedure in SQL Server
- How to convert SQL Server’s timestamp column to datetime format
- What is the equivalent of ‘describe table’ in SQL Server?
- Arithmetic overflow error converting expression to data type datetime. (while displaying date time..)
- Error converting data type varchar to float
- What is the Oracle equivalent of SQL Server’s IsNull() function?
- ClassNotFoundException – com.microsoft.jdbc.sqlserver.SQLServerDriver
- I want to use CASE statement to update some records in sql server 2005
- What represents a double in sql server?
- How do I fix ‘Invalid character value for cast specification’ on a date column in flat file?
- The SELECT permission was denied on the object ‘Users’, database ‘XXX’, schema ‘dbo’
- How do you open an SDF file (SQL Server Compact Edition)?
- What is the maximum characters for the NVARCHAR(MAX)?
- Conversion failed when converting the varchar value ‘simple, ‘ to data type int
- Must declare the scalar variable
- SELECT DISTINCT on one column
- Exclude a column using SELECT * [except columnA] FROM tableA?
- Bulk load data conversion error (type mismatch or invalid character for the specified codepage) for row 1, column 4 (Year)
- TSQL PIVOT MULTIPLE COLUMNS
- Trouble Connecting to sql server Login failed. “The login is from an untrusted domain and cannot be used with Windows authentication”
- Difference between database and schema
- Why do table names in SQL Server start with “dbo”?