Forum Discussion

praveshrawat's avatar
praveshrawat
Copper Contributor
Nov 27, 2022

Facing New Challenge in WITH Clause

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..

  • olafhelper's avatar
    olafhelper
    Bronze Contributor

    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

     

Resources