Functions vs Stored Procedures

后端 未结 12 602
我寻月下人不归
我寻月下人不归 2020-12-02 06:26

Let\'s say I have to implement a piece of T-SQL code that must return a table as result. I can implement a table-valued function or else a stored procedure that returns a se

12条回答
  •  -上瘾入骨i
    2020-12-02 06:55

    From the docs:

    If a stored procedure meets the following criteria, it is a good candidate for being rewritten as a table-valued function:

    • The logic is expressible in a single SELECT statement but is a stored procedure, rather than a view, only because of the need for parameters.

    • The stored procedure does not perform update operations, except to table variables.

    • There is no need for dynamic EXECUTE statements.

    • The stored procedure returns one result set.

    • The primary purpose of the stored procedure is to build intermediate results that are to be loaded into a temporary table, which is then queried in a SELECT statement.

提交回复
热议问题