Database consultancy: CROSS APPLY

Forrards
March 31, 2023

This article is dedicated to the operator APPLY, and more specifically its type CROSS APPLY. This statement was first introduced in SQL Server 2005, but unfortunately many have not learned how to use it, maybe it's because of the fact that the BOL (SQL ServerBooksOnline) this operator is not well and has a very "dry" examples of its use. In this article, I'll show some interesting demonstrations, where the operator can come in handy.

The main feature of the operator is that APPLY allows you to invoke a table function for each row returned by the outer table expression query. It is this example is in BOL.

CROSS APPLY operator returns only the rows from the outer table that creates a result set from a table-valued function. OUTER APPLY operator and returns the lines that form the result set, and the lines that do not, with NULL values ??in the columns created a table-valued function.

To demonstrate some of fitch operator APPLY, create a test database and a couple of tables.

The result of the function call is as follows:

Now, using the APPLY operator with a list of cities for each country from the table Countries:

So, very convenient, but a similar example is described in BOL. Next you`ll see how can still use the operator CROSS APPLY. For example, how to withdraw from 3 cities in each country, sorted alphabetically? Using APPLY operator is relatively easy:

Now try to further complicate our request. We derive the first letter of each of the 3 cities in each country and the total number of characters of any city in the current country:

As you can see the operator CROSS APPLY very useful

Forrards
March 31, 2023
Share
Recent articles:
Database consultancy: SaaS database considerations
March 31, 2023

Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.

More
Database consultancy: tree-like structure
March 31, 2023

Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:

More
Database consultancy: Hierarchy in MS SQL Server
March 31, 2023

In our time of greatest prevalence of databases were relational databases, which are the main targets of the tables and the relationships between them. Tables can solve most problems for data storage and manipulation. But in the real world entity requiring storage is not always presented in a tabular form. One of these very common types of data structures other than the table is a tree structure, where each data element is the parent and the offspring. An example of such a structure may be the structure of state enterprises, which is headed by the director (the root of the tree), his deputies, heads of departments from which are subject to certain deputies, employees of departments, which are subject to the rulers.

More

Subscribe to our blog

    

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
Database consultancy: SaaS database considerations

Utilizing given approach each client get his own database. In such case every client`s data stays isolated from others. It simplifies backing up the data, installing new clients but causes higher cost of maintenance.

March 31, 2023
Database consultancy: tree-like structure

Subject recursion is well covered in the literature, but, nevertheless, the problem of output “tree” does not mean the client and SQL Server many baffled. So, put the problem: there is a table with the name and record id field indicating the parent identifier. Immediately fill in this table, some sort of test data:

March 31, 2023

Discuss your project with us

    

Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.