Careeroza
Procedures vs Functions
Differences
| Stored procedure | Function | |
|---|---|---|
| Returns | zero or more result sets, OUT parameters | exactly one value or one table |
| Called with | CALL / EXEC | inside SQL: SELECT fn(x) |
| Use in SELECT / WHERE | no | yes |
| Modify data (INSERT/UPDATE) | yes | usually not (SQL Server forbids it) |
| Transaction control | yes (COMMIT/ROLLBACK) | no |
| Typical use | business operations: place order, month-end close | calculations and reusable expressions |
Pros and Cons of Database Code
- Pros: fewer network round trips, shared logic, security through EXECUTE-only access.
- Cons: harder to version, test and debug than application code; ties you to one database vendor; can hide business logic from developers.
Tip: Many teams keep business logic in the application and use procedures only for heavy data operations close to the data (bulk updates, archiving, reports).