cte公⽤表表达式_SQLServer公⽤表表达式(CTE)
cte公⽤表表达式香港歌手
什么是通⽤表表达式 (What is a Common Table Expression )
A Common Table Expression, also called as CTE in short form, is a temporary named result t that you can reference within a SELECT, INSERT, UPDATE, or DELETE statement. The CTE can also be ud in a View.
公⽤表表达式,也简称为CTE,是⼀个临时的命名结果集,您可以在SELECT,INSERT,UPDATE或DELETE语句中引⽤该结果集。 CTE 也可以在视图中使⽤。
In this article, we will e in detail about how to create and u CTEs from our SQL Server.
在本⽂中,我们将详细介绍如何从SQL Server创建和使⽤CTE。
常⽤表表达式的语法和⽰例 (Syntax and Examples for Common Table Expressions)
The CTE query starts with a “With” and is followed by the Expression Name. We will be using this expr
ession name in our lect query to display the result of our CTE Query and be writing our CTE query definition.
CTE查询以“ With”开头,后跟“表达式名称”。 我们将在选择查询中使⽤此表达式名称来显⽰CTE查询的结果并编写CTE查询定义。
WITH expression_name [ ( column_name [,...n] ) ]
AS
( CTE_query_definition )
To view the CTE result we u a Select query with the CTE expression name.
要查看CTE结果,我们使⽤带有CTE表达式名称的Select查询。
Select [Column1,Column2,Column3 …..] from expression_name
Or
要么
Select * from expression_name
公⽤表表达式(CTE)类型 (Common Table Expression (CTE) Types)
There are two types of CTEs: Recursive and Non-Recursive
CTE有两种类型:递归和⾮递归
Non-Recursive CTEs
⾮递归CTE
Non-Recursive CTEs are simple where the CTE doesn’t u any recursion, or repeated processing in of a sub-routine. We will create a simple Non-Recursive CTE to display the row number from 1 to 10.
⾮递归CTE很简单,其中CTE不使⽤任何递归或⼦例程中的重复处理。 我们将创建⼀个简单的⾮递归CTE,以显⽰从1到10的⾏号。
As per the CTE Syntax each CTE query will start with a “With” followed by the CTE Expression name with column list.
根据CTE语法,每个CTE查询都将以“ With”开头,然后是带有列列表的CTE表达式名称。
Here we have been using only one column as ROWNO. Next is the Query part, here we write our lect query to be execute for our CTE. After creating our CTE query to run the CTE u the lect statement with CTE Expression name.
绿茶作用
在这⾥,我们仅使⽤⼀列作为ROWNO。 接下来是查询部分,在这⾥我们编写要对CTE执⾏的选择查询。 创建我们的CTE查询以运⾏CTE 后,请使⽤带有CTE表达式名称的lect语句。
;with ROWCTE(ROWNO) as
(
SELECT
ROW_NUMBER() OVER(ORDER BY name ASC) AS ROWNO
FROM sys.databas
WHERE databa_id <= 10
)
SELECT * FROM ROWCTE
Output: When we run the query, we can e the below output.
输出:运⾏查询时,我们可以看到以下输出。
Recursive CTE
递归CTE
Recursive CTEs are u repeated procedural loops aka recursion. The recursive query call themlves until the query satisfied the condition. In a recursive CTE we should provide a where con
dition to terminate the recursion.:
递归CTE使⽤重复的过程循环(也称为递归)。 递归查询将⾃⾏调⽤,直到查询满⾜条件为⽌。 在递归CTE中,我们应该提供⼀个where 条件来终⽌递归。 :
生活压力大
We will e how to create a simple Recursive query to display the Row Number from 1 to 10 using a CTE.
我们将看到如何创建⼀个简单的递归查询以使⽤CTE显⽰从1到10的⾏号。
Firstly we declare the Integer variable as “RowNo” and t the default value as 1 and we have created our first CTE query as an expression name, “ROWCTE”. In our CTE we’ll first display the default row number and next we’ll u a Union
ALL to increment and display the row number 1 by one until the Row No reaches the incremented value to 10. To view the result, we will u a lect query to display our CTE result.
⾸先,我们将Integer变量声明为“ RowNo”,并将默认值设置为1,并创建了第⼀个CTE查询作为表达式名称“ ROWCTE”。 在CTE 中,我们将⾸先显⽰默认⾏号,然后我们将使⽤Union ALL来递增并显⽰⾏号1,直到⾏号达到递增值到10。要查看结果,我们将使⽤选择查询以显⽰我们的CTE结果。
Declare @RowNo int =1;
;with ROWCTE as
(
SELECT @RowNo as ROWNO
UNION ALL
SELECT ROWNO+1
FROM ROWCTE
WHERE RowNo < 10
)
SELECT * FROM ROWCTE
Output: When we run the query, we can e the below output.
输出:运⾏查询时,我们可以看到以下输出。
CTE Query to display Date Range:
CTE查询以显⽰⽇期范围:
Let’s consider as there is a scenario to display the date from start date to end date all one by one as each row with details. In order to display the recursive data, we will be using the CTE Query.
让我们考虑⼀下,因为存在⼀种⽅案,将开始⽇期到结束⽇期的⽇期⼀⼀显⽰在每⼀⾏中,并带有详细信息。 为了显⽰递归数据,我们将使⽤CTE查询。
Here we will write a CTE query to display the dates range with week number and day. For this we t the start and end date in parameter. Here in this example we have ud the getdate() to t the start date as Todays date, and for end date we add 16 days from today.
在这⾥,我们将编写⼀个CTE查询以显⽰带有星期数和⽇期的⽇期范围。 为此,我们在参数中设置开始和结束⽇期。 在此⽰例中,我们已使⽤getdate()将开始⽇期设置为“今天”⽇期,对于结束⽇期,我们要添加从今天开始的16天。
CTE without Union All
没有Union All的CTE
Here we can e we have create a simple CTE query to display the RowNo, start date and week number. When we run this we will get only one result with RowNo as “1” ,StartDate as current date and week number along with week day.
在这⾥,我们可以看到我们已经创建了⼀个简单的CTE查询,以显⽰RowNo,开始⽇期和星期数。 当我们运⾏此命令时,将仅获得⼀个结果,其中RowNo为“ 1”,StartDate为当前⽇期和星期数以及星期⼏。
declare @startDate datetime,
@endDate datetime;
lect @startDate = getdate(),
@endDate = getdate()+16;
-- lect @sDate StartDate,@eDate EndDate
;with myCTE as
(
lect 1 as ROWNO,@startDate StartDate,'W - '+convert(varchar(2),
冈村宁次回忆录
DATEPART( wk, @startDate))+' / D ('+convert(varchar(2),@startDate,106)+')' as 'WeekNumber'
)
lect ROWNO,Convert(varchar(10),StartDate,105) as StartDate ,WeekNumber from myCTE ;
Output: When we run the query, we can e the below output.
输出:运⾏查询时,我们可以看到以下输出。
CTE with Union All
CTE与Union All
In order to display the result from start date to end date one by one as recursive, we u a Union All to increment RowNo, to add the day one by one till the condition satisfied the date range, in order to stop the recursion we need t some condition. In this example, we repeat the recursion to display our records until the date is less than or equal to the end date.
为了将开始⽇期到结束⽇期的结果⼀⼀显⽰为递归,我们使⽤Union All来增加RowNo,⼀⼀添加⼀天直到条件满⾜⽇期范围,以停⽌递归设置⼀些条件。 在此⽰例中,我们重复递归以显⽰我们的记录,直到⽇期⼩于或等于结束⽇期为⽌。
declare @startDate datetime,
@endDate datetime;
lect @startDate = getdate(),
@endDate = getdate()+16;
鸡豆粉-- lect @sDate StartDate,@eDate EndDate
;with myCTE as
(
lect 1 as ROWNO,@startDate StartDate,'W - '+convert(varchar(2),
DATEPART( wk, @startDate))+' / D ('+convert(varchar(2),@startDate,106)+')' as 'WeekNumber'
union all
lect ROWNO+1 ,dateadd(DAY, 1, StartDate) ,
'W - '+convert(varchar(2),DATEPART( wk, StartDate))+' / D ('+convert(varchar(2),
王天旭
dateadd(DAY, 1, StartDate),106)+')' as 'WeekNumber'
FROM myCTE
WHERE dateadd(DAY, 1, StartDate)<= @endDate
)
lect ROWNO,Convert(varchar(10),StartDate,105) as StartDate ,WeekNumber from myCTE
Output: When we run the query, we can e the below output.
输出:运⾏查询时,我们可以看到以下输出。
多个CTE (Multiple CTE)
In some scenarios, we need to create more than one CTE query and join them to display our result. In this ca, we can u the Multiple CTEs. We can create a multiple CTE query and combine them into one single query by using the comma. Multiple CTE need to be parate by “,” comma fallowed by CTE name.
病毒定义在某些情况下,我们需要创建多个CTE查询并将其联接以显⽰我们的结果。 在这种情况下,我们可以使⽤多个CTE。 我们可以创建多个CTE查询,并使⽤逗号将它们组合为⼀个查询。 多个CTE必须以CTE名称分隔的逗号分隔。
We will be using above same date range example to u more than one CTE query, here we can e as we have created two CTE query as CTE1 and CTE 2 to display date range result for both CTE1 and for CTE2.
发家致富好项目
我们将使⽤上⾯相同的⽇期范围⽰例来使⽤多个CTE查询,在这⾥我们可以看到,因为我们创建了两个CTE查询,分别为CTE1和CTE 2,以显⽰CTE1和CTE2的⽇期范围结果。
Example :
范例: