Facing New Challenge in WITH Clause

Copper Contributor

Hi users,

 

I am facing the challenge to run the below query in 2014 MS Sql server.

 

With Hotels as (
select *
from dbo.['2018$']
union
select *
from dbo.['2019$']
union
select *
from dbo.['2020$'])

 

can you please detect the error and help me to get away from this issue.

 

I am grateful to you..

2 Replies

@praveshrawat , and which error message do you get?

First, don't use the asteriks * for all columns in a UNION statement. In a UNION all sub-queries must return the same count of column and the columns must have the same data type.

 

Is that the complete statement? If not, then you get an error because the final SELECT statement is misssing.

;WITH Hotels AS (...)
select *
from Hotels

 

hi, you probably need to execute this after closing ():

SELECT  * FROM Hotels.

@praveshrawat