Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

sql - 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.

(在我看来,它们看起来一样,也许是因为我对此有些新手。)

Can some one tell me why?

(有人可以告诉我为什么吗?)

  ask by Tarik translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Functions are computed values and cannot perform permanent environmental changes to SQL Server (ie no INSERT or UPDATE statements allowed).

(函数是计算值,不能对SQL Server进行永久的环境更改(即,不允许INSERT或UPDATE语句)。)

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.

(如果函数返回标量值,则可以在SQL语句中内联使用;如果返回结果集,则可以将其加入。)

A point worth noting from comments, which summarize the answer.

(注释总结了答案,这一点值得一提。)

Thanks to @Sean K Anderson:

(感谢@Sean K Anderson:)

Functions follow the computer-sciency 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.

(存储的proc不必具有参数,可以更改数据库对象,也不必返回值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...