Functions (select data only)
|
SPs (can modify data)
| |
Purpose
|
For returning a computation value used in other T-SQL statements
|
For performing business logic with a set of complex T-SQL statements, or interacting with outside world.
|
Which T-SQL elements can be used inside?
|
SELECT only (cannot modify the data or objects)
|
DML, DDL, Transaction, Error Handling
|
How to be used?
|
SELECT, WHERE, HAVING
EXEC (for deterministic functions) |
EXEC
|
Deferred Name Resolution?
|
No
|
Yes
|
Need input parameters?
|
Optional (0 ~ 1023)
|
Optional (0 ~ 21000)
|
Which SQL Server data types can be used as input parameters?
|
All including table variable but not text, ntext, image, timestamps
|
Any SQL Server data types including table variable (companying with READONLY).
|
Have to return a data?
|
Yes (a single value or a table)
|
Optional (0 ~ Many) – using OUTPUT or OUT
|
Which SQL Server data types can be used as output parameters?
|
All including table variable but not text, ntext, image, timestamps
|
Any SQL Server data types, but not a table variable. Cursor is used as output only accompanied by the VARYING keyword.
|
Return a status code?
|
No
|
Yes
|
Can a temp table be used inside?
|
No (but table variable is OK)
|
Yes (table variable OK too)
|
Can call another function
|
Yes
|
Yes
|
Can call another SP?
|
No (but can call an extended stored procedure.)
|
Yes
|
Can be used for a dynamic SQL?
|
No
|
Yes
|
Can a non-deterministic function be used?
|
No
|
Yes
|
Typical use cases
|
|
SQL Codes in the applications vs. Stored Procedure in SQL Server?
The benefits of using stored procedures in SQL Server rather than application code stored locally on client computers include:
When to use SPs? – if the codes need to be reused
When to place SQL directly in your application? – If the codes are dynamic
|
Caveat
|
|
|
SQL Server Administration, Development and B.I. Development related