In this article: Cross join Usage in T-SQL (semi-translation) is also available for Microsoft Certified 70-461:querying Microsoft SQL Server 2012 Exam learning.
---------------------------------------------------------------------the following translations----------------------------------------------- ------------------------
Original source: http://www.sqlservercentral.com/articles/Stairway+Series/121318/
Starting with SQL Server 2005, Microsoft added a new operator to associate a result set with a function and apply the function to each qualifying row in the table/view. This operator is apply. Technically, the underlying logic of this operation is not the actual "join", but because its usage is more like a join, it is usually associated with an associated operation. The apply operation is divided into two formats: cross apply or outer apply. Each of these formats is shown in this article.
Introduction of APPLY:
Have you ever written a SELECT statement to invoke a table-valued function or to match each record of a table-valued expression to a table? If you have experienced this, the Apply operator will help you. Apply is divided into cross apply and outer apply.
The first form is called Cross APPLY. It extracts the required column values for each row of a table or DataSet from the side of the crossapply, and then transmits it to the table-valued function or expression on the other side as the input value. The Union ALL operation is then used to match all associated values. If the table-valued function does not meet the calling criteria for the input data, the function will not return the output, and the table or result set in this section will not appear in the final result because it cannot be associated with the data of the table-valued function.
The second form is called outer APPLY. Its behavior is similar to crossapply, but returns the value of a table-valued function/expression that cannot be called.
To better understand both of these formats, here's a demonstration:
Test data and functions:
Use tempdb; GOif object_id (' dbo. Product ') is not a NULL DROP TABLE dbo. Product;if object_id (' dbo. SearchString ') is not a NULL DROP TABLE dbo. Searchstring;if object_id (' dbo. Findproductlike ') is not a NULL DROP FUNCTION dbo. Findproductlike; CREATE TABLE dbo. Product ( ID INT IDENTITY, Productnamevarchar, price money ); INSERT into dbo. Productvalues (' Red santasuit ', 199.99), (' Candy canes ', 1.99), (' Fake Snow ', 2.99), (' Red bells ', 49.99), (' LED Lights ', 6.99); CREATE TABLE dbo. SearchString ( ID INT IDENTITY, String VARCHAR) ; INSERT into dbo. Searchstringvalues (' Red '), (' Lights '), (' Star '); GO CREATE FUNCTION dbo. Findproductlike ( @FindStringVARCHAR) RETURNS tableasreturn (SELECT ProductName, Price From dbo. Product WHERE productnamelike '% ' + @FindString + '% ')
Creating table and table-valued functions
The above script creates a table called product that contains 5 different products. It also creates a table called SearchString, which contains 3 different strings. Finally, a table-valued function called Findproductlike is created. The function receives a @findstring parameter and finds all ProductName that contain @findstring in the product table.
Use the Cross APPLY operator:
Cross apply applies the function to each associated row
Use tempdb; Goselect *from dbo. SearchString as S cross applydbo.findproductlike (s.string);
The results are as follows:
Looking back at the code, you can see that the result set and Findproductlike table-valued functions in the Crossapply Association searchstring table are used in the code. Cross apply gets the string value from SearchString and then calls the function Findproductlike. If the function returns data, it is associated with the SearchString row.
The first two rows of data come from the string "red", and when "red" is transferred to the function, return the ProductName and price containing the value, and then associate with the SearchString, returning the product containing the "red" value to the result set. The third row of data is consistent with the first two lines, but is generated by "Lights". The string "Star" does not return a result because it does not have a matching value in product.
Use the outer APPLY operator:
The only difference between this operator and cross apply is that all data is returned, including values that do not match:
Use tempdb; Goselect *from dbo. SearchString as S OUTER applydbo.findproductlike (s.string);
As can be seen from the results, for the string "Star", OUTER apply also returns the result, just returns NULL.
Use table-Valued Expressions:
Here's an example of using table-valued expressions with apply.
Use tempdb; Goselect * FROM dbo. SearchString as S cross APPLY (SELECT ProductName, price from dbo. Product WHERE ProductName like '% ' + s.string + '% ') as X
In terms of results, it is no different from cross apply, only the table-valued function is replaced with a table-valued expression.
Summarize:
The apply operation can associate data in a dataset with a table-valued function or table-valued expression, using apply to make a collection-based query against a table-valued function or an expression's dataset. In this case, you might consider using the Apply operator.
---------------------------------------------------------------------End of translation------------------------------------------------ -----------------------
---------------------------------------------------------------------Personal Summary------------------------------------------------ -----------------------
I don't think much about this topic because I haven't really developed it for many years. But it provides me with a way of thinking that, with the permission, the new technology is not a bad idea. Also in the 70-461 example, one uses a window function, which is dozens of times times faster than the old one. It really shook me. The determination to use the new technology was also strengthened.
In the near future, I intend to upgrade the database version of the company's logistics system, which can be considered using the following technologies:
1. Columnstore index (Improve query efficiency for static data)
2. T-SQL new syntax, such as offset (for paging), etc.
3. AlwaysOn for read-write separation and disaster recovery.
4.2014 In-memory technology may be used.
5. Other pending.
My sentiment to this article is: Do not stand still, learn more new technology, especially with your existing technology completely different areas, such as now you are learning a relational database, referred to as oldsql, then may learn to learn NoSQL even newsql.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Apply usage in T-SQL (semi-translation)