Label:CTE (Common table expression) common table expressions, which are temporary result sets that are defined within the execution scope of a single statement and are valid only for the duration of the query. It can be self-referencing, and can be referenced multiple times in the same query, enabling the reuse of code snippets. The biggest benefit of the CTE is the readability of the T-SQL code, which enab
Label:CREATE
FUNCTION [dbo].[ GETXXXXXXXXX]
(
@productCategoryId INT, @SplitChar varchar
)
RETURNS NVARCHAR (MAX)
as
BEGIN
DECLARE @returnProductCategory NVARCHAR ( MAX) =n '
/*--Note that the CTE expression must be preceded by a semicolon ";" To separate the code, it is suggested to add a semicolon ";" in front of the--*/
;With CTE as (
SELECT
ProductCategoryI
Temporary tables, table variables, and CTE are commonly used in SQL Server to store temporary table data, and here is a brief introduction to the different and different scenarios in which they are used.CteA CTE is often called a "generic expression" and is created in memory.Purpose: Typically used to replace subqueries that require recursion.Valid range:
universal table Expression (CTEs) is a new feature of SQL Server 2005. They are similar to the alias (as in the Select t1.* from MyTable T1), but are more powerful. In essence, a CTE is a temporary result set that exists only in the statement in which it occurs. You can create a CTE in the Select, INSERT, DELETE, update, or cteate view statements. A
date intervalsDECLARE @begin_dateDate= '2014-12-31',@end_dateDate= '2016-12-31'; withGeneratehelpdata as( Select 1 asID,CAST( Case when Right(@begin_date,5)='12-30' Then DATEADD( Day,1,@begin_date) ELSE @begin_date END asDATE) asEndingDateUNION All SELECTId+1 asID, Case when Right(DATEADD(QQ,1, EndingDate),5)='12-30' Then DATEADD( Day,1,DATEADD(QQ,1, EndingDate)) ELSE DATEADD(QQ,1, EndingDate)END a
)This option causes SQL server2005 to produce an error when the CTE has a specified limit. By default, the limit is 100 (that is, when this option is omitted). To specify an option, you must set Maxrecursion to 0.Code Listing:With Simplerecursive (EMPLOYEE_NM, employee_id, ReportsTo)As (SELECT employee_nm,EMPLOYEE_ID,ReportsToFrom Employee_treeWHERE employee_id = 2UNION AllSELECT p.employee_nm,P.EMPLOYEE_ID
column must be unique in order to delete the other duplicates with one statement. For identical rows, a temporary table is created to transition the operation. The following uses the "with clause and the row_number () function" to implement the deletion of the other completely repeated rows, partition group randomly select a column to group sorting; With TAB as (select Row_number () over (partition by object_id order by (select 0)) Idfrom [dbo].[ TabName]) DELETE from TAB WHERE id>1------------
Today, a recursive query for SQL is used. A recursive query is a CTE statement with XX as (...). ) is implemented.If the table category data is as follows.We want to find the machine gun. This subcategory is extremely hierarchical (querying all levels of nodes through child nodes). The following is a query statementWith TT as (SELECT categoryid,name,parent,0 level from dbo. Category WHERE categoryid=15 --
Atitit. mysql oracle with as mode temporary table mode CTE statement, reduce the subquery structure mssql SQL server..., atitit. mysqlcte
Atitit. Use of the CTE statement in the temporary table mode of mysql oracle with as mode to reduce the structural mssql SQL server of subqueries ..
1. with... as (...) in mysql how
Atitit.mysql the use of the Oracle with AS Mode temp table pattern CTE statement to reduce the structural MSSQL SQL for subqueries Server:1. With ... as (...) in the MySQL what should be said in 12. Sub-query and query nesting is mainly easy to improve readability, 23. the solution in MYSQL :: temp table. 24. Seo blackheads keyword Replacement link statistics sample 24.1.//---------number of com sites que
' union select ' 7 ', ' member BB ', ' 3 ' Unio N Select ' 8 ', ' User aaa ', ' 4 ' union select ' 9 ', ' User bba ', ' 7 ' --Creates a composite clustered index create clustered index Clu_role_cte_indexon Role_ CTE (Id,parentid) with ( Pad_index=on, fillfactor=50, drop_existing=off, Statistics_ Norecompute=on) SELECT * FROM Role_cteFinds all descendant nodes of the specified node:Implemented using plain
How does SQL Server use a CTE to sort by sub-query?Requirements: Identify recently changed customer information (sorted by last change time, from Systemlog table Logdatetime field)DescriptionCustomer: Client Information FormSystemlog: System Log table, record all table information increment, delete, changeThe development of their own industry software, not only their own companies are in use, other companie
Omer ' and Rid=customerid order by ID DESC] as Ed, Row_number () Over (ORDER by (SELECT TOP 1 logdatetime from Systemlog WH ERE logtype= ' change ' and logtable= ' Customer ' and Rid=customerid ORDER by ID DESC] desc) as Rownumberfrom customerwhere Custom ERSTATUS=18) SELECT * from _temp WHERE RowNumber between 0 and ten ORDER by Ed DESC System log for querying customer informationSELECT TOP TenS.rid asCustomerId, CompanyName, S.logdatetime fromSystemlog S Left JOINCustomer C onS.rid=CustomerI
Label:Demand Recently, in an MRP project, the raw materials and quantities needed for the finished product are broken down from the original boring table according to the plan of production release. Reference Http://www.cnblogs.com/xqhppt/archive/2011/02/15/1955366.html Http://www.cnblogs.com/guoysh1987/archive/2011/12/23/2299379.html Code implementation SQL data table structure CREATE TABLE [dbo].[cproduction](
[ID] [int] IDENTITY(1,1) not NULL,
Label:digblogimgrominsert from recursive unionlogs DECLARE @DiGui TABLE (id INT, parentid int)
INSERT into @DiGui
(ID,
parentid
)
SELECT 1, C7/>2
Union
Select 2,
3
Union
Select 3,
4
Union
select 5,
Ten
UNION
Select 1,
ten
SELECT *
from @DiGui; with temp ([id], [parentid]) as
(SELECT ID,
Tags: null div creat CTE not from union ble nbsp --recursive query
IF object_id('Digui','U') is not NULL
DROP TABLEDiguiCREATE TABLEDigui (IdVARCHAR( -), ParentIDVARCHAR( -)
)
INSERT intodbo. Digui (Id, ParentID)Select 'Third Floor','First Floor'
Union Select 'Second Floor','First Floor'
Union Select 'Fourth Floor','First Floor'
Union Select 'Tenth Floor','Second Floor'
Union Select 'Nineth Floor','Second Floor'
Union Select 'Eighth Floor','Fif
Problem many times, we want table variables to execute in dynamic SQL, but the reality is very bony. such as this example:DECLARE @sql_command NVARCHAR(MAX);DECLARE @parameter_list NVARCHAR(MAX);DECLARE @last_names TABLE(last_nameNVARCHAR( -) );SELECT @sql_command = 'Select Distinctfirstnamefrom person.personwhere LastName in (select Last_Name from @last_names)'EXECsp_executesql@sql_command;View Code
Some people see here, perhaps the first in
Table structureTable DataSQL as followsWith Lmenu as (select name,id,1 as lv,cast (name as nvarchar (max)) as name1, cast (Row_number () Up (order by GETDATE ()) as N varchar () as OrderID from Table2 where table2.parentid=0 Union all Select A.name, a.ID, (b.lv+1) as LV, Cast (REPLICATE (" , b.lv*2) + a.name as nvarchar (max)) as name1, cast ((b.orderid+ '. ') + CAST (Row_number () over (Order by GETDATE ()) as nvarchar (5))) as nvarchar (ten)) as OrderID from Table2 as a inner j Oin Lm
complex, it can make SQL statements very difficult to read if too many layers are nested.Read and maintain. Therefore, you can also use table variables to solve this problem.The SQL statements are as follows:Insert into @t (Countryregioncode) Although the above SQL statement is more complex than the first, it puts the subquery in the table
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.