June 11, 2004 at 10:46 am
What is the difference between the two and when would you use which? Normally I've seen everything as SP.
June 13, 2004 at 5:49 am
Functions can return scalar data or tables and can be used in joins etc with a stored procedure. You'll need a stored procedure as your first level, but can encapsulate logic in functions that you call from your sprocs - eg if you had a function to split a csv string on the commas and it returned a table, you could then join on the returned table in a sp. another eg - you could have a function that applies complex logic to two columsn in a table and could then have a where clause like "where dbo.function(val1,val2) > 10"
June 13, 2004 at 12:26 pm
SQL Server Books Online gives the following reasons to sometimes convert stored procedures to UDFs; and I think they give a good beginning on how the two are different from each other.
This topic describes how to determine whether to rewrite existing stored procedure logic as user-defined functions. For example, if you want to invoke a stored procedure directly from a query, repackage the code as a user-defined function.
In general, if the stored procedure returns a (single) result set, define a table-valued function. If the stored procedure computes a scalar value, define a scalar function.
If a stored procedure meets the following criteria, it is a good candidate for being rewritten as a table-valued function:
INSERT #temp EXEC sp_getresultsSELECT ... FROM #temp, t1 WHERE ...
The sp_getresults stored procedure can be rewritten as a table-valued function, for example fn_results(), which means the preceding statements can be rewritten as:
SELECT ... FROM fn_results(), t1 WHERE ...
The two really are fundamentally different. One should only be used to perform functions,
thus the name. Stored Procedures on the other hand are encapsulated procedures that perform system or business logic through manipulation of data.
Derrick Leggett
Mean Old DBA
When life gives you a lemon, fire the DBA.
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply