表值函数和 View 有什么区别?您是否可以用1做一些很难或不可能做的事?还是差异在于效率?

最佳答案

无参数内联TVF和未实现的 View 非常相似。以下是一些我想到的功能差异。

观看次数

Accepts Parameters               - No
Expanded out by Optimiser        - Yes
Can be Materialized in advance   - Yes (through indexed views)
Is Updatable                     - Yes
Can contain Multiple Statements  - No
Can have triggers                - Yes
Can use side-effecting operator  - Yes

嵌入式TVF
Accepts Parameters               - Yes
Expanded out by Optimiser        - Yes
Can be Materialized in advance   - No
Is Updatable                     - Yes
Can contain Multiple Statements  - No
Can have triggers                - No
Can use side-effecting operator  - No

多声明TVF
Accepts Parameters               - Yes
Expanded out by Optimiser        - No
Can be Materialized in advance   - No
Is Updatable                     - No
Can contain Multiple Statements  - Yes
Can have triggers                - No
Can use side-effecting operator  - No

在运行时,对View和Inline TVF都进行内联,并且与派生表或CTE相似。可能无法整体评估or may be evaluated multiple times in others(甚至在某些情况下甚至根本无法评估)。多语句TVF将始终被评估并存储在返回表类型(基本上是表变量)中

Occasionally the ability to parameterise inline TVFs directly can lead to a better execution plan than the equivalent parameterised query against a view.

10-08 20:27