Click here to Skip to main content
15,916,180 members
Please Sign up or sign in to vote.
1.24/5 (3 votes)
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
Updated 2-Aug-17 20:14pm
v2

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
:)
 
Share this answer
 
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.
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900