Click here to Skip to main content
Click here to Skip to main content

Differences between Stored Procedures and Functions

By , 19 Nov 2011
 
  • Procedure can return zero or n values whereas function can return one value which is mandatory.
  • Procedures can have input/output parameters for it whereas functions can have only input parameters.
  • Procedure allows select as well as DML statement in it whereas function allows only select statement in it.
  • Functions can be called from procedure whereas procedures cannot be called from function.
  • Exception can be handled by try-catch block in a procedure whereas try-catch block cannot be used in a function.
  • We can go for transaction management in procedure whereas we can't go in function.
  • Procedures can not be utilized in a select statement whereas function can be embedded in a select statement.
  • UDF can be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section where as Stored procedures cannot be.
  • UDFs that return tables can be treated as another rowset. This can be used in JOINs with other tables.
  • Inline UDF's can be though of as views that take parameters and can be used in JOINs and other Rowset operations.

In depth

Stored Procedure

A Stored Procedure is a program (or procedure) which is physically stored within a database. They are usually written in a proprietary database language like PL/SQL for Oracle database or PL/PgSQL for PostgreSQL. The advantage of a stored procedure is that when it is run, in response to a user request, it is run directly by the database engine, which usually runs on a separate database server. As such, it has direct access to the data it needs to manipulate and only needs to send its results back to the user, doing away with the overhead of communicating large amounts of data back and forth.

User-defined Function

A user-defined function is a routine that encapsulates useful logic for use in other queries. While views are limited to a single SELECT statement, user-defined functions can have multiple SELECT statements and provide more powerful logic than is possible with views.

User defined functions have three main categories:

  1. Scalar-valued function - returns a scalar value such as an integer or a timestamp. Can be used as column name in queries.
  2. Inline function - can contain a single SELECT statement.
  3. Table-valued function - can contain any number of statements that populate the table variable to be returned. They become handy when you need to return a set of rows, but you can't enclose the logic for getting this rowset in a single SELECT statement.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Sangunni
Architect
India India
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
General20 main differences between Stored procedures and Functions in Sql ServermemberLalit24rocks15 May '13 - 0:09 
20 main differences between Stored procedures and Functions in Sql Server
http://www.webcodeexpert.com/2013/04/difference-between-stored-procedures.html
GeneralMy vote of 1memberPramod k patel26 Apr '13 - 1:42 
point wise difference given as wrong
GeneralMy vote of 5memberAzziet25 Apr '13 - 21:48 
Perfect Article
Questionnice article on comparision of Stored procedure and functionmemberisrarali24 Apr '13 - 0:11 
it is the best article on comparision of stored procedure and UDF.
i wish you very best of luck for your future.
GeneralwelldonememberVinod Kumar Prajapati2 Jan '13 - 1:27 
really a vital article..
thank u..
GeneralMy vote of 5memberadityawalia9@gmail.com28 Nov '12 - 1:45 
Covers In Depth and is quite explanatory
GeneralMy vote of 5memberAshish Rathod28 Nov '12 - 0:34 
good article.
GeneralMy vote of 5memberIbrahim Hebish27 Nov '12 - 0:23 
very accurate and simple
QuestionFeedbackmemberchandan _20may20 Sep '12 - 21:54 
good Article
SuggestionDifferences between Stored Procedures and FunctionsmemberSangramsingh Pawar13 Sep '12 - 19:23 
Good article but need with some examples
GeneralMy vote of 3memberMember 87600687 Sep '12 - 3:47 
not clear
GeneralMy vote of 5membersrsakthi@yahoo.co.in15 Jul '12 - 22:58 
good
SuggestionThere's really only TWO categories (or four, depending on how you look at it)memberkmote001 Jun '12 - 6:43 
Helpful article, but I would suggest you edit your final paragraph about function categories.
There are really only TWO categories: (1) Scalar functions, and (2) Table-Valued Functions.
Each of these categories can be further categorized as either
(a) inline (i.e., no function body; the table is the result set of a single SELECT statement), or
(b) multistatement (with a function body defined in a BEGIN...END block.)
 
(Another way to look at this is there are 4 categories: scalar-inline, scalar-multi, table-inline, and table-multi.)
 
MSDN details here.
Generalnice:)membergkannankannan27 Dec '11 - 23:12 
niceSmile | :)
GeneralReason for my vote of 5 Nice ArticlememberSunil Mane 00722 Nov '11 - 23:44 
Reason for my vote of 5
Nice Article
GeneralReason for my vote of 4 nice.membermuhammadhussain21 Nov '11 - 19:11 
Reason for my vote of 4
nice.
Generalnice.membermuhammadhussain21 Nov '11 - 19:10 
nice.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 19 Nov 2011
Article Copyright 2011 by Sangunni
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid