Hoy Cheung
Hoy Cheung

Reputation: 1670

How to union a statement start with WITH?

I want to make a union to start with WITH, but SQL server won't allow me to do that.

What will be the correct way to do it?

with T1 as
(select * from Table where ID in (1, 2, 3))
select * from T1
UNION
with T2 as
(select * from Table where ID in (7, 8, 9))
select * from T2

Upvotes: 0

Views: 48

Answers (1)

Hoy Cheung
Hoy Cheung

Reputation: 1670

I found the syntax is

with T1 as (select * from Table where ID in (1, 2, 3)), 
T2 as (select * from Table where ID in (7, 8, 9))
select * from T1
UNION
select * from T2

Upvotes: 1

Related Questions