Both a STORED PROCEDURE and a USER DEFINED FUNCTION encapsulate SQL statements and have parameters that can be passed to within the SPROC or UDF.
In effect that makes them very similar.
But philosophically, they are very different. A UDF is supposed to work on the parameters passed and return out a value. They are not intended to do TABLE I/O - but of course this rule can be broken.
A UDF can be part of a SELECT statement.
The function DBO.GETLASTNAME will take the STUNAME column from STUDENTTABLE and extract the LAST NAME of the student, for example.Code:SELECT STUID,DBO.GETLASTNAME(STUNAME) FROM STUDENTTABLE
A STORED PROCEDURE cannot be part of a select statement
SELECT STUID,GETLASTNAME_P(STUNAME) <-- this does not work...
STORED PROCEDURES usually return a recordset or perform some larger database operation. We calculate payroll in stored procedures - we adjudicate medical claims in stored procedures - we schedule students into high school classes in stored procedures.
With UDF's we put dashes in social security numbers, build address lines.




Reply With Quote