Click here to Skip to main content
13,593,025 members
Rate this:
 
Please Sign up or sign in to vote.
Hi.

i wanted to know what is difference between Stored procedure & function in SqlServer ?
and where each one is used in ?

for example i have just written a stored procedure and i have finished it with return @@Identity so can we say is this a fucntion because this will return a value ?
these sort of problems has made me distracted.
Posted 2-Oct-12 20:34pm
Updated 2-Aug-17 20:14pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Functions
----------
1) can be used with Select statement
2) Not returning output parameter but returns Table variables
3) You can join UDF
4) Cannot be used to change server configuration
5) Cannot be used with XML FOR clause
6) Cannot have transaction within function

Stored Procedure
-----------------
1) have to use EXEC or EXECUTE
2) return output parameter
3) can create table but won't return Table Variables
4) you can not join SP
5) can be used to change server configuration
6) can be used with XML FOR Clause
7) can have transaction within SP

Regard
Sham
:)
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Thanks for the question,I too have this doubt my pal.
I googled our doubt and came here with an answer.

These are the difference between Store procedure and Function.

Store Procedure Vs. Function in sql server
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 Rowsetoperations.
I hope this clears our doubt my pal.
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180618.1 | Last Updated 3 Aug 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100