()AS()SELECT*FROM示例一(基本用法):withMyCTE(ID,Name)as(selectEmployeeIDasI" />

欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

CTE 通用表表達式

系統 1738 0
CTE 通用表表達式

概念:Common Table Expression,簡稱CTE,中文可以叫做,通用表表達式.

用處:處理以前版本中SQL不好現實,不好理解,復雜的查詢問題.比如:分頁,遞歸查詢...

基本用法:

WITH < name of your CTE > ( < column names > )
AS

(
< actual query >
)

SELECT * FROM < name of your CTE >

示例一(基本用法):

with MyCTE(ID, Name)
as

(
select EmployeeID as ID, FirstName + ' ' + LastName as Name
from
HumanResources.vEmployee
)
select * from MyCTE

示例二(分頁):

with MyCTE(ID, Name, RowID)
as

(
select EmployeeID as ID, FirstName + ' ' + LastName as Name,
Row_Number()
over ( order by EmployeeID) as
RowID
from
HumanResources.vEmployee
)
select * from MyCTE where RowID between 1 and 10

示例三(關聯CTE):

with OrderCountCTE(SalesPersonID, OrderCount)
as
(
select
SalesPersonID, count (
1)
from Sales.SalesOrderHeader
where SalesPersonID is not
null
group
by SalesPersonID
)
select sp.SalesPersonID, sp.SalesYTD, cte.OrderCount
from OrderCountCTE cte inner
join Sales.SalesPerson sp
on cte.SalesPersonID = sp.SalesPersonID order by 3


示例四(使用CTE的刪除):

CREATE TABLE Products (
Product_ID
int NOT NULL
,
Product_Name
varchar ( 25
),
Price
money NULL
,
CONSTRAINT PK_Products PRIMARY KEY NONCLUSTERED
(Product_ID)
)
GO

INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 1 , ' Widgets ' , 25 )
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 2 , ' Gadgets ' , 50
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 3 , ' Thingies ' , 75
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 4 , ' Whoozits ' , 90
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 5 , ' Whatzits ' , 5
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 6 , ' Gizmos ' , 15
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 7 , ' Widgets ' , 24
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 8 , ' Gizmos ' , 36
)
INSERT INTO Products (Product_ID, Product_Name, Price) VALUES ( 9 , ' Gizmos ' , 36
)
GO


-- ==================Delete duplicate products=============================
with DuplicateProdCTE
as

(
select Min (Product_ID) as Product_ID, Product_Name
from
Products
group by
Product_Name
having count ( 1 ) > 1

)
delete Products from Products p join DuplicateProdCTE cte
on cte.Product_Name = p.Product_Name and p.Product_ID > cte.Product_ID

示例五(遞歸查詢):

CREATE TABLE Employee_Tree (Employee_NM nvarchar ( 50 ), Employee_ID int PRIMARY KEY , ReportsTo int )
-- insert some data, build a reporting tree

INSERT INTO Employee_Tree VALUES ( ' Richard ' , 1 , NULL )
INSERT INTO Employee_Tree VALUES ( ' Stephen ' , 2 , 1
)
INSERT INTO Employee_Tree VALUES ( ' Clemens ' , 3 , 2
)
INSERT INTO Employee_Tree VALUES ( ' Malek ' , 4 , 2
)
INSERT INTO Employee_Tree VALUES ( ' Goksin ' , 5 , 4
)
INSERT INTO Employee_Tree VALUES ( ' Kimberly ' , 6 , 1
)
INSERT INTO Employee_Tree VALUES ( ' Ramesh ' , 7 , 5
)
--
with
MyCTE
as

(
select Employee_ID, Employee_NM, - 1 as ReportsTo, 0 as SubLevel
from Employee_Tree where ReportsTo is null -- root node

union all
select e.Employee_ID, e.Employee_NM, e.ReportsTo, SubLevel + 1
from Employee_Tree e, MyCTE where e.ReportsTo = MyCTE.Employee_ID

)
-- select * from MyCTE

select MyCTE.Employee_NM as emp , MyCTE.SubLevel, e.Employee_NM as boss
from MyCTE left join Employee_Tree e on MyCTE.ReportsTo =
e.Employee_ID
-- OPTION(MAXRECURSION 3) --error

--
OPTION(MAXRECURSION 4) --ok
where SubLevel < 4

注意: OPTION(MAXRECURSION 4) 用來設置遞歸時查找的層數,默認是100,如果超過默認或指定的,則會報錯.通常我們用一個層數列來過濾指定的層.

CTE 通用表表達式


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 播五月婷婷 | 日本爽爽爽爽爽爽免费 | 久久草资源费视频在线观看 | 成人一级| 国产精品成人av | 91高清免费 | 欧美中文字幕 | 97精品超碰一区二区三区 | 欧美zozozo人禽交免费观看 | 国精品午夜dy8888狼人 | 狠狠狠色丁香婷婷综合激情 | 精品欧美成人高清视频在线观看 | 午夜成人在线视频 | 久久精品国产清自在天天线 | 91精品国产综合久久精品 | 免费看国产片在线观看 | 夜夜夜操操操 | 欧美精品一二区 | 日韩国产欧美一区二区三区 | 欧美黑人玩白人巨大极品 | 亚洲精品久久久久一区二区 | 国产小视频免费在线观看 | 91视频青娱乐 | 狠狠干天天干 | 久久精品a | 欧美精品一区二区三区蜜桃视频 | 激情视频免费 | 天天爱天天做天天干 | 三级精品 | 久久久久国产成人精品亚洲午夜 | 五月天黄色网址 | 天天操天天摸天天爽 | 国产目拍亚洲精品区一区 | 日韩精品一区二区在线观看 | 日韩欧美不卡 | 国产精品第一区 | 亚洲日本va中文字幕线 | xxxx亚洲 | 国产乱码精品1区2区3区 | 国产 欧美 日韩 一区 | 国产成人一区二区三区久久久 |