sql out apply

SQL Server supports table valued functions, what are functions that return data in the form of tables.

SQL Server支持表值函数,即以表形式返回数据的函数。

JOIN operations in SQL Server are used to join two or more tables. However, JOIN operations cannot be used to join a table with the output of a table valued function.

SQL Server中的JOIN操作用于联接两个或多个表。 但是,JOIN操作不能用于将表与表值函数的输出连接。

APPLY operators are used for this purpose.

APPLY运算符用于此目的。

There are two main types of APPLY operators. 1) CROSS APPLY and 2) OUTER APPLY.

有两种主要类型的APPLY运算符。 1)交叉申请和2)外部申请。

The CROSS APPLY operator is semantically similar to INNER JOIN operator. It retrieves those records from the table valued function and the table being joined, where it finds matching rows between the two.

CROSS APPLY运算符在语义上类似于INNER JOIN运算符。 它从表值函数和要连接的表中检索那些记录,并在两者之间找到匹配的行。

On the other hand, OUTER APPLY retrieves all the records from both the table valued function and the table, irrespective of the match.

另一方面,OUTER APPLY会从表值函数和表中检索所有记录,无论是否匹配。

In this article we will take a look at the CROSS APPLY and OUTER APPLY operators. We will see how they are implemented practically with the help of an example and will also discuss how they differ from each other.

在本文中,我们将研究CROSS APPLY和OUTER APPLY运算符。 我们将通过一个示例了解它们的实际实现方式,还将讨论它们之间的不同之处。

准备伪数据 (Preparing dummy data)

First, let’s create a dummy database with some dummy records in it. We will use this dummy database to perform different operations throughout this article. As ever if you are trying things out on a live database be sure to check that you are fully backed up.

首先,我们创建一个虚拟数据库,其中包含一些虚拟记录。 在本文中,我们将使用该虚拟数据库执行不同的操作。 与以往一样,如果您要在实时数据库上进行尝试,请确保检查是否已完全备份。

Execute the following script:

执行以下脚本:

CREATE DATABASE LibraryGO USE Library;CREATE TABLE Author
(id INT PRIMARY KEY,author_name VARCHAR(50) NOT NULL,)CREATE TABLE Book
(id INT PRIMARY KEY,book_name VARCHAR(50) NOT NULL,price INT NOT NULL,author_id INT NOT NULL)USE Library;INSERT INTO Author VALUES
(1, 'Author1'),
(2, 'Author2'),
(3, 'Author3'),
(4, 'Author4'),
(5, 'Author5'),
(6, 'Author6'),
(7, 'Author7')INSERT INTO Book VALUES
(1, 'Book1',500, 1),
(2, 'Book2', 300 ,2),
(3, 'Book3',700, 1),
(4, 'Book4',400, 3),
(5, 'Book5',650, 5),
(6, 'Book6',400, 3)

In the script above we created a database named Library. The database has two tables: Author and Book. Book has an author_id column which contains values from the id column of the Author table. This means that there is a one to many relationships between the Author and Book columns.

在上面的脚本中,我们创建了一个名为Library的数据库。 该数据库有两个表:Author和Book。 图书有一个author_id列,其中包含Author表的id列中的值。 这意味着在“作者”和“书”列之间存在一对多的关系。

使用JOIN运算符联接表 (Joining tables using JOIN operators)

Let’s first use the INNER JOIN operator to retrieve matching rows from both of the tables.

首先让我们使用INNER JOIN运算符从两个表中检索匹配的行。

Execute the following script:

执行以下脚本:

SELECT A.author_name, B.id, B.book_name, B.price
FROM Author A
INNER JOIN Book B
ON A.id = B.author_id

The following records will be selected.

将选择以下记录。

You can see that only those records have been selected from the Author table where there is a matching row in the Book table. To retrieve all the records from Author table, LEFT JOIN can be used.

您可以看到仅从“作者”表中选择了那些记录,在“书”表中有匹配的行。 要从Author表中检索所有记录,可以使用LEFT JOIN。

SELECT A.author_name, B.id, B.book_name, B.price
FROM Author A
LEFT JOIN Book B
ON A.id = B.author_id

The output of the above query looks like this:

以上查询的输出如下所示:

You can see that all the records are retrieved from the Author table, irrespective of there being any matching rows in the Book table.

您可以看到所有记录都是从“作者”表中检索的,无论“书”表中是否有任何匹配的行。

使用APPLY运算符将表值函数与表联接 (Joining table valued functions with tables using APPLY operators)

We saw how JOIN operators join the results from two tables. However, as mentioned above they cannot be used to join a table valued function with a table. A table valued function is a function that returns records in the form of a table.

我们看到了JOIN运算符如何将两个表的结果联接起来。 但是,如上所述,它们不能用于将表值函数与表连接。 表值函数是一种以表形式返回记录的函数。

Let’s first write a simple table valued function that accepts author id as parameter and returns all the books written by that author.

让我们首先编写一个简单的表值函数,该函数接受作者ID作为参数并返回该作者所写的所有书籍。

Execute the following script:

执行以下脚本:

CREATE FUNCTION fnGetBooksByAuthorId(@AuthorId int)
RETURNS TABLE
AS
RETURN
(
SELECT * FROM Book
WHERE author_id = @AuthorId
)

Let’s test the above function. We will pass 3 as the author id to the fnGetBooksByAuthorId function. It should return book4 and book6, since these are the two books written by the author with the id of three.

让我们测试上述功能。 我们将3作为作者ID传递给fnGetBooksByAuthorId函数。 它应该返回book4和book6,因为这是作者写的两本书,id为3。

Execute the following script:

执行以下脚本:

SELECT * FROM fnGetBooksByAuthorId(3)

The output of the above script looks like this:

上面脚本的输出如下所示:

Let’s try to use an INNER JOIN operator to join the Author table with the table valued function fnGetBooksByAuthorId.

让我们尝试使用INNER JOIN运算符将Author表与表值函数fnGetBooksByAuthorId联接。

Take a look at the following script:

看一下以下脚本:

SELECT A.author_name, B.id, B.book_name, B.price
FROM Author A
INNER JOIN fnGetBooksByAuthorId(A.Id) B
ON A.id = B.author_id

Here we are using the INNER JOIN operator to join a physical table (Author) with a table valued function fnGetBooksByAuthorId. All the ids from the Author table are passed to the function. However, the script above throws an error which looks like this:

在这里,我们使用INNER JOIN运算符将物理表(作者)与表值函数fnGetBooksByAuthorId连接起来。 Author表中的所有ID都传递给函数。 但是,上面的脚本会引发如下错误:

使用CROSS APPLY连接表和表值函数 (Joining table and table valued function using CROSS APPLY)

Now, let’s use the CROSS APPLY operator to join the Author table with the table valued function fnGetBooksByAuthorId. The CROSS APPLY operator is semantically similar to INNER JOIN. It retrieves all the records from the table where there are corresponding matching rows in the output returned by the table valued function.

现在,让我们使用CROSS APPLY运算符将Author表与表值函数fnGetBooksByAuthorId联接起来。 CROSS APPLY运算符在语义上类似于INNER JOIN。 它从表中检索所有记录,其中表值函数返回的输出中有对应的匹配行。

Take a look at the following script:

看一下以下脚本:


SELECT A.author_name, B.id, B.book_name, B.price
FROM Author A
CROSS APPLY fnGetBooksByAuthorId(A.Id) B

In the script above, all the ids from the Author table are being passed to fnGetBooksByAuthorId function. For each id in the Author table, the function returns corresponding records from the Book table. The result from this table valued function is being joined with the table Author.

在上面的脚本中,Author表中的所有ID都将传递给fnGetBooksByAuthorId函数。 对于Author表中的每个id,该函数从Book表中返回相应的记录。 该表值函数的结果与表Author结合在一起。

The output of the above script looks like this:

上面脚本的输出如下所示:

This is similar to the INNER JOIN operation performed on the Author and Book tables. CROSS APPLY returns only those records from a physical table where there are matching rows in the output of the table valued function.

这类似于对Author和Book表执行的INNER JOIN操作。 CROSS APPLY仅从物理表中返回那些表值函数的输出中有匹配行的记录。

使用OUTER APPLY连接表和表值函数 (Joining table and table valued functions using OUTER APPLY)

To retrieve all the rows from both the physical table and the output of the table valued function, OUTER APPLY is used. OUTER APPLY is semantically similar to the OUTER JOIN operation.

要从物理表和表值函数的输出中检索所有行,请使用OUTER APPLY。 外层申请 在语义上类似于OUTER JOIN操作。

Take a look at the following script to see OUTER APPLY in action.

看一下以下脚本,以查看OUTER APPLY的实际应用。

SELECT A.author_name, B.id, B.book_name, B.price
FROM Author A
OUTER APPLY fnGetBooksByAuthorId(A.Id) B

The output of the above function looks like this:

上面函数的输出如下所示:

You can see that all the records from the Author table have been retrieved irrespective of the matching rows in the output from the table valued function fnGetBookByAuthorId.

您可以看到,与表值函数fnGetBookByAuthorId的输出中的匹配行无关,已检索了Author表中的所有记录。

结论 (Conclusion)

In this article, we studied what the CROSS APPLY and OUTER APPLY functions are, and how they can be used to perform join operations between a physical table and table valued function.

在本文中,我们研究了CROSS APPLY和OUTER APPLY函数是什么,以及如何将它们用于执行物理表和表值函数之间的联接操作。

We first used JOIN operators to join two physical tables. We then explained how JOIN operators can be replaced by APPLY operators in order to achieve the same results by joining a physical table with output of a table valued function.

我们首先使用JOIN运算符来联接两个物理表。 然后,我们解释了如何通过将物理表与表值函数的输出连接起来,用APPLY运算符替换JOIN运算符,以实现相同的结果。

翻译自: https://www.sqlshack.com/the-difference-between-cross-apply-and-outer-apply-in-sql-server/

sql out apply

sql out apply_在SQL Server中CROSS APPLY和OUTER APPLY之间的区别相关推荐

  1. SQL Server中唯一索引和唯一约束之间的区别

    This article gives you an overview of Unique Constraints in SQL and also the Unique SQL Server index ...

  2. SQL Server 2008的cross apply 和 outer apply

    我们知道有个 SQL Server 2000 中有个 cross join 是用于交叉联接的.实际上增加 cross apply 和 outer apply 是用于交叉联接表值函数(返回表结果集的函数 ...

  3. 浅析 SQL Server 的 CROSS APPLY 和 OUTER APPLY 查询 - 第一部分

    第一部分:APPLY 与 JOIN 你可能知道,SQL Server 中的 JOIN 操作用于联接两个或多个表.但是,在 SQL Server 中,JOIN 操作不能用于将表与表值函数的输出联接起来. ...

  4. SQL 关于apply的两种形式cross apply 和 outer apply

    SQL 关于apply的两种形式cross apply 和 outer apply 阅读目录 SQL 关于apply的两种形式cross apply 和 outer apply Sql学习第四天--S ...

  5. SQL 关于apply的两种形式cross apply 和 outer apply(转)

    转载链接:http://www.cnblogs.com/shuangnet/archive/2013/04/02/2995798.html apply有两种形式: cross apply 和 oute ...

  6. 【转载】SQL 关于apply的两种形式cross apply 和 outer apply

    apply有两种形式: cross apply 和 outer apply 先看看语法: <left_table_expression>  {cross|outer} apply < ...

  7. SQL中inner join、left join、right join、outer join之间的区别

    SQL中inner join.left join.right join.outer join之间的区别 举个例子你就能知道了! A表(a1,b1,c1)      B表(a2,b2) a1   b1  ...

  8. mysql cross apply_SQL Server CROSS APPLY和OUTER APPLY的应用详解

    SQL Server数据库操作中,在2005以上的版本新增加了一个APPLY表运算符的功能 SQL Server数据库操作中,在2005以上的版本新增加了一个APPLY表运算符的功能.新增的APPLY ...

  9. sqlserver中cross apply与outer apply用法

    apply有两种形式: cross apply 和 outer apply 先看看语法: <left_table_expression>  {cross|outer} apply < ...

最新文章

  1. 为什么需要消息队列?
  2. 服务器如何管理?分享九款服务器管理工具
  3. 从linux和ucos的比较中来看进程这个概念
  4. ElementUI中el-form实现表单重置以及将方法抽出为全局方法
  5. shell字段拼接日期_shell 脚本字符串拼接
  6. Linux——grep文本搜索命令
  7. Codeforces Gym101473 F.Triangles-前缀和 (2013-2014 ACM-ICPC Brazil Subregional Programming Contest)...
  8. 在VS2005中打造自己的Starter Kits
  9. 联合主键三种实现方式
  10. Python撰写mail
  11. python from import 和 import 区别_python import和from import的区别
  12. live2d模型二次开发
  13. 一阶惯性环节如何实现跟踪性能与滤波性能共存(三)
  14. GO、Rust这些新一代高并发编程语言为何都极其讨厌共享内存?
  15. javaMail发送电子邮件
  16. RFC 文档中文目录
  17. 全球十大程序化交易系统 ( 有源码 )
  18. 大数据分析工具,主要存在哪些最常见的难题?
  19. 做好PMC管理三大工作,轻松搞定生产计划与物料控制
  20. Android11.0下应用管控实现解决方案(家长管理)(一)

热门文章

  1. 10本最值得推荐的区块链书
  2. 如何下载HLS视频到本地(m3u8)
  3. python小括号( )与中括号 [ ]
  4. java中== 和 .equals()的区别
  5. 牛腩新闻发布系统——触发器使用
  6. LeetCode(476)——数字的补数(JavaScript)
  7. 句句真研—每日长难句打卡Day6
  8. 睡眠多少分钟一个循环_关于科学睡眠丨90分钟一个睡眠周期,每晚循环3到5次...
  9. PowerPoint什么意思
  10. 听说社保不能补缴了,像我们这种没缴社保的打工者,该怎么办?