復制代碼 代碼如下:
select * from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')
復制代碼 代碼如下:
declare @t table(CountryRegionCode nvarchar(3))
insert into @t(CountryRegionCode) (select CountryRegionCode from person.CountryRegion where Name like 'C%')
select * from person.StateProvince where CountryRegionCode
in (select * from @t)
復制代碼 代碼如下:
[ WITH <common_table_expression> [ ,n ] ]
<common_table_expression>::=
expression_name [ ( column_name [ ,n ] ) ]
AS
( CTE_query_definition )
復制代碼 代碼如下:
with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.StateProvince where CountryRegionCode in (select * from cr)
復制代碼 代碼如下:
with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select * from person.CountryRegion -- 應將這條SQL語句去掉
-- 使用CTE的SQL語句應緊跟在相關的CTE后面 --
select * from person.StateProvince where CountryRegionCode in (select * from cr)
復制代碼 代碼如下:
with
cte1 as
(
select * from table1 where name like 'abc%'
),
cte2 as
(
select * from table2 where id > 20
),
cte3 as
(
select * from table3 where price < 100
)
select a.* from cte1 a, cte2 b, cte3 c where a.id = b.id and a.id = c.id
復制代碼 代碼如下:
-- table1是一個實際存在的表
with
table1 as
(
select * from persons where age < 30
)
select * from table1 -- 使用了名為table1的公共表表達式
select * from table1 -- 使用了名為table1的數據表
復制代碼 代碼如下:
新聞熱點
疑難解答