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


当前回答

在SQL Server中,函数和存储过程是两种不同类型的实体。

函数:在SQL Server数据库中,函数用于执行一些动作,动作立即返回结果。 函数有两种类型:

系统定义的 用户定义的

存储过程:在SQL Server中,存储过程存储在服务器中,它可以返回零,单个和多个值。 存储过程有两种类型:

系统存储过程 用户自定义过程

其他回答

存储过程:

就像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.

It is mandatory for Function to return a value while it is not for stored procedure. Select statements only accepted in UDF while DML statements not required. Stored procedure accepts any statements as well as DML statements. UDF only allows inputs and not outputs. Stored procedure allows for both inputs and outputs. Catch blocks cannot be used in UDF but can be used in stored procedure. No transactions allowed in functions in UDF but in stored procedure they are allowed. Only table variables can be used in UDF and not temporary tables. Stored procedure allows for both table variables and temporary tables. UDF does not allow stored procedures to be called from functions while stored procedures allow calling of functions. UDF is used in join clause while stored procedures cannot be used in join clause. Stored procedure will always allow for return to zero. UDF, on the contrary, has values that must come - back to a predetermined point.

基本区别

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

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

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

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

之前的区别

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

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

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

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

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

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

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

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

函数可以在选择语句中使用,而过程则不能。 存储过程同时接受输入和输出参数,而函数只接受输入参数。 函数不能返回text, ntext, image和时间戳类型的值,而过程可以。 函数可以在创建表中作为用户定义的数据类型使用,但过程不能。

***例如:-create table <tablename>(name varchar(10),salary getsal(name))

这里getsal是一个用户定义的函数,它返回一个工资类型,当创建表时,没有存储空间分配给工资类型,getsal函数也不执行,但当我们从这个表中获取一些值时,getsal函数get被执行,返回 类型作为结果集返回。