在SQL中什么时候应该使用函数而不是存储过程,反之亦然?每一个的目的是什么?


当前回答

存储过程与用户自定义函数的区别:

Stored procedures cannot be used in Select statements. Stored procedures support Deferred Name Resolution. Stored procedures are generally used for performing business logic. Stored procedures can return any datatype. Stored procedures can accept greater numbers of input parameter than user defined functions. Stored procedures can have up to 21,000 input parameters. Stored procedures can execute Dynamic SQL. Stored procedures support error handling. Non-deterministic functions can be used in stored procedures.


User-defined functions can be used in Select statements. User-defined functions do not support Deferred Name Resolution. User-defined functions are generally used for computations. User-defined functions should return a value. User-defined functions cannot return Images. User-defined functions accept smaller numbers of input parameters than stored procedures. UDFs can have up to 1,023 input parameters. Temporary tables cannot be used in user-defined functions. User-defined functions cannot execute Dynamic SQL. User-defined functions do not support error handling. RAISEERROR OR @@ERROR are not allowed in UDFs. Non-deterministic functions cannot be used in UDFs. For example, GETDATE() cannot be used in UDFs.

其他回答

函数和存储过程有不同的用途。虽然这不是最好的类比,但函数可以从字面上视为任何编程语言中使用的任何其他函数,但存储的proc更像是单个程序或批处理脚本。

函数通常有一个输出和可选的输入。输出可以作为另一个函数的输入(SQL Server内置的,如DATEDIFF, LEN等)或作为SQL查询的谓词-例如,SELECT a, b, dbo.MyFunction(c) FROM表或SELECT a, b, c FROM表WHERE a = dbo.MyFunc(c)。

Stored proc用于将SQL查询绑定到事务中,并与外部世界进行交互。框架,如ADO。NET等不能直接调用函数,但可以直接调用存储过程。

函数确实有一个隐患:它们可能被误用并导致相当严重的性能问题:考虑以下查询:

SELECT * FROM dbo.MyTable WHERE col1 = dbo.MyFunction(col2)

其中MyFunction声明为:

CREATE FUNCTION MyFunction (@someValue INTEGER) RETURNS INTEGER
AS
BEGIN
   DECLARE @retval INTEGER

   SELECT localValue 
      FROM dbo.localToNationalMapTable
      WHERE nationalValue = @someValue

   RETURN @retval
END

这里发生的事情是,MyFunction函数被MyTable表中的每一行调用。如果MyTable有1000行,那么就有另外1000个针对数据库的特别查询。类似地,如果函数在列规范中指定时被调用,则该函数将对SELECT返回的每一行被调用。

所以写函数的时候一定要小心。如果从函数中的表执行SELECT操作,则需要问问自己,在父进程中使用JOIN或其他SQL构造(如CASE…当……其他的……结束)。

SQL Server函数,比如游标,是你最后的武器!它们确实存在性能问题,因此应该尽可能避免使用表值函数。谈论性能就是谈论一个有超过1,000,000条记录的表托管在一个中产阶级硬件的服务器上;否则,您不需要担心函数对性能的影响。

永远不要使用函数将结果集返回给外部代码(如ADO.Net) 尽可能使用视图/存储procs组合。您可以使用DTA(数据库调优顾问)给您的建议(比如索引视图和统计数据)来解决未来的增长性能问题——有时!

如需进一步参考,请参阅:http://databases.aspfaq.com/database/should-i-use-a-view-a-stored-procedure-or-a-user-defined-function.html

一般来说,使用存储过程的性能更好。 例如,在以前版本的SQL Server中,如果你将函数置于JOIN条件下,基数估计为1 (SQL 2012之前)和100 (SQL 2012之后和SQL 2017之前),引擎可能会生成一个糟糕的执行计划。

此外,如果你把它放在WHERE子句中,SQL引擎可能会生成一个糟糕的执行计划。

在SQL 2017中,微软引入了称为交错执行的功能,以产生更准确的估计,但存储过程仍然是最佳解决方案。

要了解更多细节,请参阅以下Joe Sack的文章 https://techcommunity.microsoft.com/t5/sql-server/introducing-interleaved-execution-for-multi-statement-table/ba-p/385417

基本区别

函数必须返回一个值,但在存储过程中它是可选的(过程可以返回零或n个值)。

函数只能有输入参数,而过程可以有输入/输出参数。

函数需要一个输入参数,这是必须的,但存储过程可能需要o到n个输入参数。

函数可以从过程中调用,而过程不能从函数中调用。

之前的区别

Procedure允许在其中使用SELECT和DML(INSERT/UPDATE/DELETE)语句,而Function只允许在其中使用SELECT语句。

过程不能在SELECT语句中使用,而Function可以嵌入到SELECT语句中。

存储过程不能在WHERE/HAVING/SELECT部分的SQL语句中使用,而函数可以。

返回表的函数可以被视为另一个行集。这可以在与其他表的join中使用。

内联函数可以被认为是接受参数的视图,可以在join和其他行集操作中使用。

异常可以用try-catch块在过程中处理,而try-catch块不能在函数中使用。

我们可以在过程中使用事务管理,而不能在功能中使用。

STORE PROCEDURE FUNCTION (USER DEFINED FUNCTION)
Procedure can return 0, single or multiple values Function can return only single value
Procedure can have input, output parameters Function can have only input parameters
Procedure cannot be called from a function Functions can be called from procedure
Procedure allows select as well as DML statement in it Function allows only select statement in it
Exception can be handled by try-catch block in a procedure Try-catch block cannot be used in a function
We can go for transaction management in procedure We can not go for transaction management in function
Procedure cannot be utilized in a select statement Function can be embedded in a select statement
Procedure can affect the state of database means it can perform CRUD operation on database Function can not affect the state of database means it can not perform CRUD operation on database
Procedure can use temporary tables Function can not use temporary tables
Procedure can alter the server environment parameters Function can not alter the environment parameters
Procedure can use when we want instead is to group a possibly- complex set of SQL statements Function can use when we want to compute and return a value for use in other SQL statements