在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.
其他回答
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.
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 |
在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.
用户定义函数是sql server程序员可用的重要工具。您可以像这样在SQL语句中内联使用它
SELECT a, lookupValue(b), c FROM customers
其中lookupValue将是UDF。这种功能在使用存储过程时是不可能实现的。同时,您不能在UDF中做某些事情。这里要记住的基本事情是UDF的:
不能产生永久的变化 无法更改数据
存储过程可以做这些事情。
对我来说,UDF的内联使用是UDF最重要的使用。
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- 从现有模式生成表关系图(SQL Server)
- 我如何循环通过一组记录在SQL Server?
- 数据库和模式的区别
- 如何在SQL Server中一次更改多个列
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何选择每一行的列值不是独特的