【Microsoft SQL Server 2008 技术内幕:T-SQL语言基础】三、表表达式

sql2008 t-sql

[b][size=large]Sql Server四种表表达式[/size][/b]
[list]
[*]派生表:derived table. from子句中的嵌套子查询
[*]视图:view
[*]通用表表达式:CTE, common table expression
[*]内联表值函数:intline TVF, inline table-valued function
[/list]
[b][size=large]通用表表达式CTE[/size][/b]

[b]CTE (Sql2005中引入)[/b]
[list]
[*]使用WITH子句定义,WITH后跟CTE名称(别名)
[*]外部查询完成时,生命期结束
[*]由于WITH子句有不同意义,容易引入歧义,所以建议CTE语句中明确地使用分号
[*]因为没有物化的存在,通常对性能没有任何影响
[/list]
-- CTE, definition
with C1(orderyear, custid) as (
select year(orderdate), CustomerID
from SalesLT.SalesOrderHeader
),
C2 as (
select orderyear, count(DISTINCT custid) as numcusts
from C1
group by orderyear
)
select orderyear, numcusts
from C2
where numcusts > 30;


[b]CTE的多引用[/b]
[list]
[*]相对于派生表,因为CTE先于from子句定义,因此可以引用同一CTE的多个实例。
[*]避免了像派生表那样需要维护多个子查询的副本
[/list]
-- CTE, multi-reference
with YearlyCount as (
select YEAR(orderdate) as orderyear
, COUNT(distinct customerID) as numcusts
from SalesLT.SalesOrderHeader
group by YEAR(OrderDate)
)
select Cur.orderyear
, Cur.numcusts as CurNumCusts
, Prv.numcusts as PrvNumCusts
, Cur.numcusts - Prv.numcusts as Growth
from YearlyCount as Cur
left join YearlyCount as Prv
on Cur.orderyear = Prv.orderyear + 1

[b]CTE的递归[/b]
[list]
[*]基本语法同CTE的一般定义。WITH内定义的查询将作为定位成员(anchor member),它只会被调用一次,返回“第一个”前一个结果集
[*]WITH定义内部须再添加UNION ALL关键字和一个递归查询成员(recursive member)。递归成员被调用多次,直到递归结束
[*]递归成员通过引用CTE名称达到递归的目的
[*]两个查询成员必须保持列个数和数据类型的兼容性
[*]外部查询中对CTE名称的引用表示对该递归查询结果集的引用
[*]为避免递归出现死循环,Sql server设置了最大递归次数为100,超过100时会自动终止。
[*]通过在外部查询的最后指定“OPTION MAXRECURSION n”改变该限制(n=0时取消限制)
[/list]
-- CTE, Recursive
WITH CategoryCTE([ParentProductCategoryID], [ProductCategoryID], [Name]) AS (
-- anchor member, executed only in the beginning
SELECT [ParentProductCategoryID], [ProductCategoryID], [Name]
FROM SalesLT.ProductCategory
WHERE ParentProductCategoryID IS NULL
UNION ALL -- to union all recursive query results
-- recursive member
SELECT C.[ParentProductCategoryID], C.[ProductCategoryID], C.[Name]
FROM SalesLT.ProductCategory AS C
INNER JOIN CategoryCTE AS BC
ON BC.ProductCategoryID = C.ParentProductCategoryID
)
SELECT PC.[Name] AS [ParentProductCategoryName]
, CCTE.[Name] as [ProductCategoryName]
, CCTE.[ProductCategoryID]
FROM CategoryCTE AS CCTE
JOIN SalesLT.ProductCategory AS PC
ON PC.[ProductCategoryID] = CCTE.[ParentProductCategoryID]

[b][size=large]视图属性和选项[/size][/b]
[list]
[*][b]ENCRYPTION[/b]属性:对视图、存储过程、触发器,用户定义函数(UDF)的定义进行加密,从而用sp_helptext和OBJECT_DEFINITION无法获取元数据
[*][b]SCHEMABIDING[/b]属性:指定改属性后,视图所引用的对象无法被删除,被引用的列也不可删除或修改
[*][b]CHECK OPTION[/b]选项:检查以避免通过视图进行的数据修改与当前视图中设置的过滤条件相冲突。
[/list]
ALTER VIEW [SalesLT].[vEnProduct] 
WITH SCHEMABINDING, encryption -- view attributes
AS
SELECT p.[ProductID]
,p.[Name]
,pmx.[Culture]
,pd.[Description]
FROM [SalesLT].[Product] p
INNER JOIN [SalesLT].[ProductModelProductDescription] pmx
ON p.[ProductModelID] = pmx.[ProductModelID]
INNER JOIN [SalesLT].[ProductDescription] pd
ON pmx.[ProductDescriptionID] = pd.[ProductDescriptionID]
WHERE pmx.Culture = 'en' -- this view only contains products for 'en'
WITH CHECK OPTION;
GO

-- Error sample: conflict with CHECK OPTION

-- Can't be changed to other culture other than 'en'
update P
set P.Culture = 'zh'
from SalesLT.vEnProduct as P

-- You can't insert a record without 'en' Culture tagged

[b][size=large]内联表值函数(参数化视图)[/size][/b]
除了支持输入参数以外,其他方面与视图类似,下边是一个例子:
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[ufnGetCustomerInformation]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[ufnGetCustomerInformation]
GO
CREATE FUNCTION [dbo].[ufnGetCustomerInformation]
(@CustomerID int) RETURNS TABLE
AS
RETURN (
SELECT
CustomerID,
FirstName,
LastName
FROM [SalesLT].[Customer]
WHERE [CustomerID] = @CustomerID


[b][size=large]APPLY运算符(APPLY opeator)[/size][/b]

[list]
[*]Sql2005引入的非标准运算符
[*]在FROM子句中使用
[*]包括CROSS APPLY和OUTER APPLY两种形式,概念上相似于INNER JOIN和OUTER JOIN。
[/list]
[b]基本原理如下:[/b]
[list]
[*]APPLY运算符以两个输入表为左右参数,其中右表(第二个表),可以是表表达式,通常是派生表或内联表值函数。
[*]CROSS APPLY:把右表应用到左表的每一行,再把结果集组合起来,输出统一的表结果。如果右表为空,则对应的左表行不会输出。
[*]OUTER APPLY:比CROSS APPLY多了一个逻辑处理步骤,标示出让右表为空的左表数据行,输出该行是,右表的列将置为NULL。
[*]出于封装的目的,推荐使用内联表值函数取代派生表作为右表
[/list]
-- Get the first 3 orders of each customer
select C.CustomerID
, A.SalesOrderID
, A.OrderDate
from SalesLT.Customer as C
outer apply (
select top(3) SalesOrderID, OrderDate
from SalesLT.SalesOrderHeader as O
where O.CustomerID = C.CustomerID
order by OrderDate desc, SalesOrderID desc
) as A
order by C.CustomerID
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值