在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.

其他回答

存储过程用作脚本。它们为您运行一系列命令,您可以安排它们在特定时间运行。通常运行多个DML语句,如INSERT, UPDATE, DELETE等,甚至是SELECT。

函数用作方法。你给它传递一些东西,它会返回一个结果。应该是小而快速的-在飞行中。通常在SELECT语句中使用。

函数和存储过程有不同的用途。虽然这不是最好的类比,但函数可以从字面上视为任何编程语言中使用的任何其他函数,但存储的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程序员可用的重要工具。您可以像这样在SQL语句中内联使用它

SELECT a, lookupValue(b), c FROM customers 

其中lookupValue将是UDF。这种功能在使用存储过程时是不可能实现的。同时,您不能在UDF中做某些事情。这里要记住的基本事情是UDF的:

不能产生永久的变化 无法更改数据

存储过程可以做这些事情。

对我来说,UDF的内联使用是UDF最重要的使用。

存储过程:

就像SQL Server中的一个微型程序。 可以像选择语句一样简单,也可以像长语句一样复杂 从多个服务器中添加、删除、更新和/或读取数据的脚本 数据库中的表。 (可以实现循环和游标,两者都允许您使用 较小的结果或对数据的逐行操作。) 应该使用EXEC或EXECUTE语句调用。 返回表变量,但不能使用OUT参数。 支持事务。

功能:

Can not be used to update, delete, or add records to the database. Simply returns a single value or a table value. Can only be used to select records. However, it can be called very easily from within standard SQL, such as: SELECT dbo.functionname('Parameter1') or SELECT Name, dbo.Functionname('Parameter1') FROM sysObjects For simple reusable select operations, functions can simplify code. Just be wary of using JOIN clauses in your functions. If your function has a JOIN clause and you call it from another select statement that returns multiple results, that function call will JOIN those tables together for each line returned in the result set. So though they can be helpful in simplifying some logic, they can also be a performance bottleneck if they're not used properly. Returns the values using OUT parameter. Does not support transactions.

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

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.