with as 用法

原语句:select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')

修改后:

with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)

select * from person.StateProvince where CountryRegionCode in (select * from cr)

with相当于一个临时表,优点是同一sql中多次调用的话不需要多次查询,感觉相当于定义了一个变量并赋值

猜你喜欢

转载自ydlmlh.iteye.com/blog/1489374