SQL Server 中的函数与存储过程 [英] Function vs. Stored Procedure in SQL Server
问题描述
我学习函数和存储过程已经有一段时间了,但我不知道为什么以及何时应该使用函数或存储过程.它们对我来说看起来一样,也许是因为我在这方面有点新手.
I've been learning Functions and Stored Procedure for quite a while but I don't know why and when I should use a function or a stored procedure. They look same to me, maybe because I am kinda newbie about that.
谁能告诉我为什么?
推荐答案
函数是计算值,不能对 SQL Server
执行永久性环境更改(即,没有 INSERT
或允许 UPDATE
语句).
Functions are computed values and cannot perform permanent environmental changes to SQL Server
(i.e., no INSERT
or UPDATE
statements allowed).
如果函数返回标量值,则可以在 SQL
语句中内联使用,如果返回结果集,则可以连接.
A function can be used inline in SQL
statements if it returns a scalar value or can be joined upon if it returns a result set.
评论中值得注意的一点,总结了答案.感谢@Sean K Anderson:
函数遵循计算机科学定义,因为它们必须返回一个值并且不能改变它们作为参数接收的数据(论据).函数不允许改变任何东西,必须至少有一个参数,并且它们必须返回一个值.存储procs 不必有参数,可以改变数据库对象,并且不必返回值.
Functions follow the computer-science definition in that they MUST return a value and cannot alter the data they receive as parameters (the arguments). Functions are not allowed to change anything, must have at least one parameter, and they must return a value. Stored procs do not have to have a parameter, can change database objects, and do not have to return a value.
这篇关于SQL Server 中的函数与存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!