在SQL中什么时候应该使用函数而不是存储过程,反之亦然?每一个的目的是什么?
当前回答
在SQL Server中,函数和存储过程是两种不同类型的实体。
函数:在SQL Server数据库中,函数用于执行一些动作,动作立即返回结果。 函数有两种类型:
系统定义的 用户定义的
存储过程:在SQL Server中,存储过程存储在服务器中,它可以返回零,单个和多个值。 存储过程有两种类型:
系统存储过程 用户自定义过程
其他回答
下面是一个总结差异的表格:
Stored Procedure | Function | |
---|---|---|
Returns | Zero or more values | A single value (which may be a scalar or a table) |
Can use transaction? | Yes | No |
Can output to parameters? | Yes | No |
Can call each other? | Can call a function | Cannot call a stored procedure |
Usable in SELECT, WHERE and HAVING statements? | No | Yes |
Supports exception handling (via try/catch)? | Yes | No |
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.
存储过程用作脚本。它们为您运行一系列命令,您可以安排它们在特定时间运行。通常运行多个DML语句,如INSERT, UPDATE, DELETE等,甚至是SELECT。
函数用作方法。你给它传递一些东西,它会返回一个结果。应该是小而快速的-在飞行中。通常在SELECT语句中使用。
当你想要计算并返回一个值以供其他SQL语句使用时,编写一个用户定义函数;当您想要编写存储过程时,您可以将一组可能很复杂的SQL语句分组。毕竟,这是两个非常不同的用例!
存储过程:
就像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.
推荐文章
- 选项(RECOMPILE)总是更快;为什么?
- 设置数据库从单用户模式到多用户
- oracle中的RANK()和DENSE_RANK()函数有什么区别?
- 我如何转义一个百分比符号在T-SQL?
- SQL Server恢复错误-拒绝访问
- 的类型不能用作索引中的键列
- SQL逻辑运算符优先级:And和Or
- 如何检查一个表是否存在于给定的模式中
- 添加一个复合主键
- 如何在SQL Server Management Studio中查看查询历史
- SQL Server索引命名约定
- 可以为公共表表达式创建嵌套WITH子句吗?
- 什么时候我需要在Oracle SQL中使用分号vs斜杠?
- SQL Server的NOW()?
- 在SQL中,count(列)和count(*)之间的区别是什么?