hi guys can u tell me the difference between funtion and a stored procedure??
difference
Collapse
X
-
by function I presume you mean user defined function (UDF)
Its hard to define exactly so I will try in terms of how they are used
Stored procedures
may or may not take input parameters
perform any number of separate operations via SQL
that may or may not return a result to the caller.
UDF
always take input parameters
perform any number of separate operations via SQL
that may or may not return a result to the caller.
UDF's are generally used to perform a calcutation using the input parameters and return a result but they can also return a table variable that holds the recordset of a query. When they return a table variable you can think of them as parameterised views -
i think (read: am not sure) function's parameter are optional. functions may or may have parameter. in sql server, the difference between functions and stored-proc are almost blurred. but there are still major differences...a mong them,
as of many other software and programming language, a function can not be called as a single statement call. a EXEC myFunction() is wrong. but this is valid in STORED PROC. to call a function, it has to be embedded in a statement set @myvar = myfunction()... .or select myfunction(), field1...from mytable...or could be if @myfunction() = somevalue.....
it may also return a table which could act as a view. a view in difference with function, cannot accept parameter.
most developer/programmer create stored proc to create module that will be executed in a regular basis. functions are usually created to be called within stored proc...these are "generally" speaking, this is not specific and not written in stone..
that's it for now...i guess, it would depend on your requirement. your requirement will dictate whether you're to use function or stored proc.
-- CKComment
-
Comment