函数与存储过程 [英] Functions vs Stored Procedures

查看:136
本文介绍了函数与存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我必须实现一个必须返回一个表作为结果的T-SQL代码。我可以实现表值函数或返回一组行的存储过程。我应该使用什么?

Let's say I have to implement a piece of T-SQL code that must return a table as result. I can implement a table-valued function or else a stored procedure that returns a set of rows. What should I use?

总之,我想知道的是:

函数和存储过程之间的主要区别?使用一个或另一个时,需要考虑哪些因素?

Which are the main differences between functions and stored procedures? What considerations do I have to take into account for using one or the other?

推荐答案

如果您可能想将这段代码的结果与其他表结合,那么很明显,一个表值函数将允许您在一个SELECT语句中组合结果。

If you're likely to want to combine the result of this piece of code with other tables, then obviously a table-valued function will allow you to compose the results in a single SELECT statement.

通常,存在层次结构(View< TV Function< Stored Proc)。你可以在每一个中做更多,但是组合输出的能力,以及优化器真正涉及的能力随着功能的增加而减少。

Generally, there's a hierarchy (View < TV Function < Stored Proc). You can do more in each one, but the ability to compose the outputs, and for the optimizer to get really involved decreases as the functionality increases.

因此,使用最低限度允许您表达您想要的结果。

So use whichever one minimally allows you to express your desired result.

这篇关于函数与存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆