Stored Procedures are pre-compile objects which are compiled for first time and its compiled format is saved which executes (compiled code) whenever it is called. But Function is compiled and executed every time when it is called. For more about stored procedure and function refer the articles Different types of Stored Procedure andDifferent types of Function.

Basic Difference

  1. Function must return a value but in Stored Procedure it is optional( Procedure can return zero or n values).

  2. Functions can have only input parameters for it whereas Procedures can have input/output parameters .

  3. Functions can be called from Procedure whereas Procedures cannot be called from Function.

Advance Difference

  1. Procedure allows SELECT as well as DML(INSERT/UPDATE/DELETE) statement in it whereas Function allows only SELECT statement in it.

  2. Procedures can not be utilized in a SELECT statement whereas Function can be embedded in a SELECT statement.

  3. Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.

  4. The most important feature of stored procedures over function is to retention and reuse the execution plan while in case of function it will be compiled every time.

  5. Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.

  6. Inline Function can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

  7. Exception can be handled by try-catch block in a Procedure whereas try-catch block cannot be used in a Function.

  8. We can go for Transaction Management in Procedure whereas we can't go in Function.

Summary

In this article I try to explain the difference between Stored Procedure and Function. I hope after reading this article you will be able to understand BASIC as well as ADVANCE difference between Stored Procedure and Functions (UDF). Comments, criticism and suggestions are always welcome.

Difference between Stored Procedure and Function in SQL Server的更多相关文章

  1. SQL Stored Procedure and Function

    Anything can be programmable with defined syntax and common lib. )) -- Add the parameters for the st ...

  2. sql: table,view,function, procedure created MS_Description in sql server

    --添加描述 geovindu --https://msdn.microsoft.com/en-us/library/ms180047.aspx --https://msdn.microsoft.co ...

  3. [EF] - Entity Framework 6处理User Defined Function(UDF SQL Server)

    随着EF5的发布,新增了对数据库(SQL Server) UDF的支持,具体可以看以下的连接:https://msdn.microsoft.com/en-us/data/hh859577.aspx,新 ...

  4. Part 8 Coalesce function in sql server

  5. SQL Server ->> EXECUTE AS LOGIN/USER和Revert表达式

    EXECUTE AS LOGIN/USER和Revert表达式都是从SQL Server 2005就有.Revert的作用是用于切换当前过程的执行上下文返回上一个EXECUTE AS 语句发生之前的安 ...

  6. Northwind数据库不能在SQL Server 2012上创建

    附加mdf文件不出意料会提示数据库格式无法兼容,试着通过脚本来创建,会有以下错误提示: Could not find stored procedure 'sp_dboption'. 原因是SQL Se ...

  7. Microsoft SQL Server Trace Flags

    Complete list of Microsoft SQL Server trace flags (585 trace flags) REMEMBER: Be extremely careful w ...

  8. Sql Server问题集锦

    Sql数据库默认实例和命名实例的区别: 1.服务中服务名称的区别: 默认实例:MSSQLSERVER; 命名实例:实列名为:benet,在服务中的名称是:MSSQL$BENET;   注:如果你有多个 ...

  9. Microsoft SQL Server Version List [sqlserver 7.0-------sql server 2016]

    http://sqlserverbuilds.blogspot.jp/   What version of SQL Server do I have? This unofficial build ch ...

随机推荐

  1. bzoj1006 [HNOI2008]神奇的国度

    1006: [HNOI2008]神奇的国度 Time Limit: 20 Sec  Memory Limit: 162 MBSubmit: 2304  Solved: 1043 Description ...

  2. BZOJ4644 : 经典傻逼题

    设每个点的权值为和它相连的所有边的权值的异或和,那么等价于选若干个点,使得点权异或和最大,这显然只需要维护一组线性基,然后从高位到低位贪心选取即可. 对于本题,因为有修改操作,所以考虑按时间分治,并用 ...

  3. 移动端H5页面之iphone6的适配(转)

    iphone6 及 iphone 6 plus 已经出来一段时间了.很多移动端网站,以前写死body 为320px的,现在估计也忙着做适配了. 大屏幕手机其实一直有,只是以前大家没怎么重视,移动端的H ...

  4. Android & iOS 第三方 Crash ANR 捕捉上传

    1. Bugly 地址:http://bugly.qq.com/ 提供:专业的Crash(崩溃).Android ANR(application not response).iOS卡顿监控和解决方案. ...

  5. ACM: 限时训练题解-Rock-Paper-Scissors-前缀和

    Rock-Paper-Scissors   Rock-Paper-Scissors is a two-player game, where each player chooses one of Roc ...

  6. iOS9 升级XCode7遇到的问题收集

    开发环境运行      各位可能会觉得,笔者在此还要讲开发环境的运行,是不是多此一举.其实并非如此,综合笔者这几年iOS开发经验的总结,运行新版本,特别是测试版本的Xcode是一个需要格外小心的事情, ...

  7. java代码过滤emoji表情

    可以新建一个过滤器的类,在类中书写如下代码: public static String filterEmoji(String source) {           if(source != null ...

  8. 使用 Eclipse C/C++ Development Toolkit 开发应用程序

    使用 Eclipse C/C++ Development Toolkit 开发应用程序 (转) 来自http://blog.csdn.net/favory/article/details/189080 ...

  9. js变量

    由于undefined和null两个值的比较是相等的,所以,未初始化的变量和赋值为null的变量会相等.这时,可以采用typeof变量的类型进行比较.但,建议还是养成编码的规范,不要忘记初始化变量. ...

  10. mysql修改数据表名

    在使用mysql时,经常遇到表名不符合规范或标准,但是表里已经有大量的数据了,如何保留数据,只更改表名呢? 可以通过建一个相同的表结构的表,把原来的数据导入到新表中,但是这样视乎很麻烦. 能否简单使用 ...