sql with as用法详解

一.WITH AS的含义
WITH AS短语,也叫做子查询部分(subquery factoring),可以让你做很多事情,定义一个sql片断,该sql片断会被整个sql语句所用到。有的时候,是为了让sql语句的可读性更高些,也有可能是在UNION ALL的不同部分,作为提供数据的部分。
特别对于UNION ALL比较有用。因为UNION ALL的每个部分可能相同,但是如果每个部分都去执行一遍的话,则成本太高,所以可以使用WITH AS短语,则只要执行一遍即可。如果WITH AS短语所定义的表名被调用两次以上,则优化器会自动将WITH AS短语所获取的数据放入一个TEMP表里,如果只是被调用一次,则不会。而提示materialize则是强制将WITH AS短语里的数据放入一个全局临时表里。很多查询通过这种方法都可以提高速度。
二.使用方法
先看下面一个嵌套的查询语句:
<div class="codetitle"><a style="CURSOR: pointer" data="81844" class="copybut" id="copybut81844" onclick="doCopy('code81844')"> 代码如下:

<div class="codebody" id="code81844">
select from person.StateProvince where CountryRegionCode in
(select CountryRegionCode from person.CountryRegion where Name like 'C%')

上面的查询语句使用了一个查询。虽然这条sql语句并不复杂,但如果嵌套的层次过多,会使sql语句非常难以阅读和维护。因此,也可以使用表变量的方式来解决这个问题,sql语句如下:
<div class="codetitle"><a style="CURSOR: pointer" data="73558" class="copybut" id="copybut73558" onclick="doCopy('code73558')"> 代码如下:
<div class="codebody" id="code73558">
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)

虽然上面的sql语句要比第一种方式更复杂,但却将子查询放在了表变量@t中,这样做将使sql语句更容易维护,但又会带来另一个问题,就是性能的损失。由于表变量实际上使用了临时表,从而增加了额外的I/O开销,因此,表变量的方式并不太适合数据量大且频繁查询的情况。为此,在sql Server 2005中提供了另外一种解决方案,这就是公用表表达式(CTE),使用CTE,可以使sql语句的可维护性,同时,CTE要比表变量的效率高得多。
下面是CTE的语法:
<div class="codetitle"><a style="CURSOR: pointer" data="51532" class="copybut" id="copybut51532" onclick="doCopy('code51532')"> 代码如下:
<div class="codebody" id="code51532">
[ WITH [,n ] ]
::=
expression_name [ ( column_name [,n ] ) ]
AS
( CTE_query_deFinition )

现在使用CTE来解决上面的问题,sql语句如下:
<div class="codetitle"><a style="CURSOR: pointer" data="91053" class="copybut" id="copybut91053" onclick="doCopy('code91053')"> 代码如下:<div class="codebody" id="code91053">
with
cr as
(
select CountryRegionCode from person.CountryRegion where Name like 'C%'
)
select
from person.StateProvince where CountryRegionCode in (select from cr)

其中cr是一个公用表表达式,该表达式在使用上与表变量类似,只是sql Server 2005在处理公用表表达式的方式上有所不同。
在使用CTE时应注意如下几点:
1. CTE后面必须直接跟使用CTE的sql语句(如select、insert、update等),否则,CTE将失效。如下面的sql语句将无法正常使用CTE:
<div class="codetitle"><a style="CURSOR: pointer" data="72490" class="copybut" id="copybut72490" onclick="doCopy('code72490')"> 代码如下:<div class="codebody" id="code72490">
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)

2. CTE后面也可以跟其他的CTE,但只能使用一个with,多个CTE中间用逗号(,)分隔,如下面的sql语句所示:
<div class="codetitle"><a style="CURSOR: pointer" data="54736" class="copybut" id="copybut54736" onclick="doCopy('code54736')"> 代码如下:<div class="codebody" id="code54736">
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

3. 如果CTE的表达式名称与某个数据表或视图重名,则紧跟在该CTE后面的sql语句使用的仍然是CTE,当然,后面的sql语句使用的就是数据表或视图了,如下面的sql语句所示:
<div class="codetitle"><a style="CURSOR: pointer" data="81440" class="copybut" id="copybut81440" onclick="doCopy('code81440')"> 代码如下:<div class="codebody" id="code81440">
-- table1是一个实际存在的表
with
table1 as
(
select from persons where age < 30
)
select
from table1 -- 使用了名为table1的公共表表达式
select from table1 -- 使用了名为table1的数据表

4. CTE 可以引用自身,也可以引用在同一 WITH 子句中预先定义的 CTE。不允许前向引用。
5. 不能在 CTE_query_deFinition 中使用以下子句:
(1)COmpuTE 或 COmpuTE BY
(2)ORDER BY(除非指定了 TOP 子句)
(3)INTO
(4)带有查询提示的 OPTION 子句
(5)FOR XML
(6)FOR broWSE
6. 如果将 CTE 用在属于批处理的一部分的语句中,那么在它之前的语句必须以分号结尾,如下面的sql所示:
<div class="codetitle"><a style="CURSOR: pointer" data="1330" class="copybut" id="copybut1330" onclick="doCopy('code1330')"> 代码如下:<div class="codebody" id="code1330">
declare @s nvarchar(3)
set @s = 'C%'
; -- 必须加分号
with
t_tree as
(
select CountryRegionCode from person.CountryRegion where Name like @s
)
select
from person.StateProvince where CountryRegionCode in (select * from t_tree)

assqlsqlwith

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...