在SQL中什么时候应该使用函数而不是存储过程,反之亦然?每一个的目的是什么?
当前回答
这里有一个更喜欢函数而不是存储过程的实际原因。如果有一个存储过程需要另一个存储过程的结果,则必须使用insert-exec语句。这意味着您必须创建一个临时表并使用exec语句将存储过程的结果插入到临时表中。它是混乱的。这样做的一个问题是insert-execs不能嵌套。
如果您使用调用其他存储过程的存储过程,可能会遇到这种情况。如果嵌套存储过程只是返回一个数据集,则可以将其替换为表值函数,这样就不会再出现此错误。
(这是我们应该将业务逻辑排除在数据库之外的另一个原因)
其他回答
存储过程用作脚本。它们为您运行一系列命令,您可以安排它们在特定时间运行。通常运行多个DML语句,如INSERT, UPDATE, DELETE等,甚至是SELECT。
函数用作方法。你给它传递一些东西,它会返回一个结果。应该是小而快速的-在飞行中。通常在SELECT语句中使用。
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.
在SQL Server中,函数和存储过程是两种不同类型的实体。
函数:在SQL Server数据库中,函数用于执行一些动作,动作立即返回结果。 函数有两种类型:
系统定义的 用户定义的
存储过程:在SQL Server中,存储过程存储在服务器中,它可以返回零,单个和多个值。 存储过程有两种类型:
系统存储过程 用户自定义过程
当你想要计算并返回一个值以供其他SQL语句使用时,编写一个用户定义函数;当您想要编写存储过程时,您可以将一组可能很复杂的SQL语句分组。毕竟,这是两个非常不同的用例!
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 |
推荐文章
- 选项(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(*)之间的区别是什么?