Click here to Skip to main content
16,017,986 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
When i Use Store Procedure and when Sql Function??
Posted

main difference

store procedure's can not used as inline Query

while function is used for inline query

eg.
SQL
select (exec sp_abc 'a1','a2') as a,* from tbl_abc --will give error because sp can't be used as inline Query

SQL
select fn_concat(a1, a2) as a,* from tbl_abc--functions can be used as inline query


another important things are,
* Function have limited error handling then Store-procedures
* Function can not use temporary tables
* Function can not call SP, but in sp you can call function using select statement

Happy Coding!
:)
 
Share this answer
 
Use a user-defined function when you want to compute and return a value for use in other SQL statements;
Refer more: here[^]
Use a stored procedure when you want instead is to group a possibly-complex set of SQL statements.
Refer more:
Link 1[^]
Link 2[^]
Link 3[^]
These are two pretty different use cases, after all!

Refer more detailed answer:
Function vs. Stored Procedure in SQL Server[^]
 
Share this answer
 
v2
Comments
Mehdi Gholam 27-Jul-12 7:47am    
5'ed
Prasad_Kulkarni 27-Jul-12 7:53am    
Thank you Mehdi!
_Amy 27-Jul-12 7:48am    
Nice Answer. +5!
Prasad_Kulkarni 27-Jul-12 7:53am    
Thank you Amit!

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