Skip to content

Commit 2e8dd6e

Browse files
committed
added new answer
1 parent f8dcd14 commit 2e8dd6e

1 file changed

Lines changed: 91 additions & 0 deletions

File tree

โ€ŽREADME.mdโ€Ž

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,97 @@ DROP PROCEDURE IF EXISTS total_duration;
16081608
```
16091609
16101610
#### Q. What is the difference between Stored Procedure and User Defined Function?
1611+
1612+
Stored Procedures and User Defined Functions (UDFs) are both database objects used in SQL to encapsulate a set of SQL statements for reuse. However, they serve different purposes and have several fundamental differences. Let's explore them in detail:
1613+
1614+
**๐Ÿ”น 1. Definition & Purpose**
1615+
1616+
| Feature | **Stored Procedure** | **User Defined Function (UDF)** |
1617+
| ------- | ---------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
1618+
| Purpose | Used to perform actions like modifying data (INSERT, UPDATE, DELETE), calling other procedures, managing transactions. | Used to return a value or result set based on input parameters โ€” often used for calculations or data retrieval. |
1619+
| Type | Procedural programming object. | Deterministic programming object (usually returns value(s) only). |
1620+
1621+
**๐Ÿ”น 2. Return Type**
1622+
1623+
| Feature | **Stored Procedure** | **User Defined Function** |
1624+
| ------------ | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
1625+
| Return Value | Can return **0 or more result sets**, **output parameters**, and **status codes**. | Must **return a single value** (scalar function) or a **table** (table-valued function). Cannot return multiple result sets. |
1626+
1627+
**๐Ÿ”น 3. Usage in SQL Statements**
1628+
1629+
| Feature | **Stored Procedure** | **User Defined Function** |
1630+
| ------------------- | -------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
1631+
| Use in SELECT/WHERE | **Cannot** be used directly in `SELECT`, `WHERE`, or `JOIN` clauses. | **Can** be used directly inside SQL statements like `SELECT`, `WHERE`, or `JOIN`. |
1632+
| Example | `EXEC GetCustomerDetails` | `SELECT dbo.GetDiscount(100)` |
1633+
1634+
**๐Ÿ”น 4. Side Effects (Data Modification)**
1635+
1636+
| Feature | **Stored Procedure** | **User Defined Function** |
1637+
| ----------------------------------------- | -------------------- | ---------------------------------------------------------------------------------------- |
1638+
| Can modify data (INSERT, UPDATE, DELETE)? | โœ… Yes | โŒ No (only for scalar functions; table-valued UDFs can **read** but not **modify** data) |
1639+
1640+
1641+
**๐Ÿ”น 5. Transactions**
1642+
1643+
| Feature | **Stored Procedure** | **User Defined Function** |
1644+
| -------------------- | ----------------------------------------------------------------------------------- | ----------------------------------------------------------- |
1645+
| Transactions Support | Can handle and manage transactions using `BEGIN TRANSACTION`, `COMMIT`, `ROLLBACK`. | Cannot handle transactions. No transaction control allowed. |
1646+
1647+
**๐Ÿ”น 6. Error Handling**
1648+
1649+
| Feature | **Stored Procedure** | **User Defined Function** |
1650+
| -------------- | ------------------------------------------------------- | ----------------------------------------------------- |
1651+
| Error Handling | Supports `TRY...CATCH` blocks and can use `RAISEERROR`. | Limited or **no** advanced error handling mechanisms. |
1652+
1653+
1654+
**๐Ÿ”น 7. Performance**
1655+
1656+
| Feature | **Stored Procedure** | **User Defined Function** |
1657+
| ----------- | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
1658+
| Performance | More flexible and efficient for batch processing and complex logic. | Suitable for quick computations and small logic. Can have performance issues if misused. |
1659+
1660+
**๐Ÿ”น 8. Example**
1661+
1662+
- ๐Ÿงพ Stored Procedure:
1663+
1664+
```sql
1665+
CREATE PROCEDURE GetEmployeeById
1666+
@EmpId INT
1667+
AS
1668+
BEGIN
1669+
SELECT * FROM Employees WHERE EmployeeId = @EmpId;
1670+
END;
1671+
```
1672+
1673+
- ๐Ÿงฎ User Defined Function (Scalar):
1674+
1675+
```sql
1676+
CREATE FUNCTION GetEmployeeSalary(@EmpId INT)
1677+
RETURNS INT
1678+
AS
1679+
BEGIN
1680+
DECLARE @Salary INT;
1681+
SELECT @Salary = Salary FROM Employees WHERE EmployeeId = @EmpId;
1682+
RETURN @Salary;
1683+
END;
1684+
```
1685+
1686+
**โœ… Summary Table:**
1687+
1688+
| Feature | Stored Procedure | User Defined Function |
1689+
| --------------------------------- | ------------------ | -------------------------- |
1690+
| Returns multiple values? | โœ… Yes | โŒ No (except table-valued) |
1691+
| Modifies data? | โœ… Yes | โŒ No |
1692+
| Used in SELECT/WHERE? | โŒ No | โœ… Yes |
1693+
| Handles Transactions? | โœ… Yes | โŒ No |
1694+
| Supports Error Handling? | โœ… Yes | โŒ Limited |
1695+
| Calls other procedures/functions? | โœ… Yes | โŒ Not recommended |
1696+
| Use Case | Complex operations | Reusable computations |
1697+
1698+
<div align="right">
1699+
<b><a href="#table-of-contents">โ†ฅ back to top</a></b>
1700+
</div>
1701+
16111702
#### Q. How can you raise custom errors from stored procedure?
16121703

16131704
<div align="right">

0 commit comments

Comments
ย (0)