Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to name my select statements as one table under 'hotels'.
What am I doing wrong

What I have tried:

with hotels
as (
select * FROM dbo.hotel_2018
union
select * FROM dbo.hotel_2019
union
select * FROM dbo.hotel_2020)
Posted
Updated 12-Jan-22 15:16pm

1 solution

Your SQL statement is incomplete, you still have to do something with those hotels.
Example:

T-SQL
with hotels
as (
select * FROM dbo.hotel_2018
union
select * FROM dbo.hotel_2019
union
select * FROM dbo.hotel_2020 ) select * FROM hotels

doesn't give me any syntax errors.

:)

PS: it isn't considered good practice to split data over several tables all having the same structure. Databases don't mind large tables, you probably should have put all records in a single table right from the start.
 
Share this answer
 
v4
Comments
Richard Deeming 13-Jan-22 4:05am    
Especially when SQL Server already supports Partitioned Tables[^]. :)
Rahab Lawshea 13-Jan-22 10:30am    
This is my current SQL statement.

with hotels as
(
select * FROM dbo.hotel_2018
union
select * FROM dbo.hotel_2019
union
select * FROM dbo.hotel_2020
)
select *
from hotels

When I run the statement above I get no syntax errors. However, when I run the statement below I get "Invalid object name 'hotels'.

select * from hotels

I will probably end up combining the tables as you mentioned. I am new to SQL so I am just trying different things.
Herman<T>.Instance 13-Jan-22 10:47am    
;WITH hotels AS ()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900