SQL中WITH AS 语句可以放到select语句内部吗?下面代码如何修改

select * from (
with
t_tree as
(
select CountryRegionCode from person.CountryRegion
)
select * from person.StateProvince where CountryRegionCode in (select * from t_tree)
) as chaxuuu

很简单哦,你把WITH AS 这段放到最前面去就可以了,下面的语句可以直接调用的,当然中间不要有分号断开。

with
t_tree as
(
    select CountryRegionCode from person.CountryRegion 
)
select * from (select * from person.StateProvince where CountryRegionCode in (select * from t_tree)
) as chaxuuu

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-12-21
很报谦!你的这个需求是无法支持的。因此无法在将其做为子查询来处理
SQL 引擎不支持此种语法,会报错的,如:
关键字 'with' 附近有语法错误。如果此语句是公用表表达式、xmlnamespaces 子句或者更改跟踪上下文子句,那么前一个语句必须以分号结尾。
第2个回答  推荐于2016-08-14
with t_tree as (select CountryRegionCode from person.CountryRegion ) select * from person.StateProvince where CountryRegionCode in (select * from t_tree)
为什么不用表连接进行查询呢 用in 严重影响查询效率
select * from c.* from person.StateProvince s,person.CountryRegion c where s.CountryRegionCode=c.CountryRegionCode;追问

表连接查询比用in查询效率高?是吗?
请优化下面这个查询
with

t_tree1 as (select C from a ),
t_tree2 as (select Cfrom b )
select * from p
where C in (select C from t_tree1) or C in (select C from t_tree2)

追答

以上写法意义不大
优化结果如下:
采用exists

select p.* from a,b,p where a.c=p.c or c.c=p.c

本回答被提问者和网友采纳