Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1608,6 +1608,97 @@ DROP PROCEDURE IF EXISTS total_duration;
```

#### Q. What is the difference between Stored Procedure and User Defined Function?

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:

**๐Ÿ”น 1. Definition & Purpose**

| Feature | **Stored Procedure** | **User Defined Function (UDF)** |
| ------- | ---------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------- |
| 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. |
| Type | Procedural programming object. | Deterministic programming object (usually returns value(s) only). |

**๐Ÿ”น 2. Return Type**

| Feature | **Stored Procedure** | **User Defined Function** |
| ------------ | ---------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------- |
| 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. |

**๐Ÿ”น 3. Usage in SQL Statements**

| Feature | **Stored Procedure** | **User Defined Function** |
| ------------------- | -------------------------------------------------------------------- | --------------------------------------------------------------------------------- |
| 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`. |
| Example | `EXEC GetCustomerDetails` | `SELECT dbo.GetDiscount(100)` |

**๐Ÿ”น 4. Side Effects (Data Modification)**

| Feature | **Stored Procedure** | **User Defined Function** |
| ----------------------------------------- | -------------------- | ---------------------------------------------------------------------------------------- |
| Can modify data (INSERT, UPDATE, DELETE)? | โœ… Yes | โŒ No (only for scalar functions; table-valued UDFs can **read** but not **modify** data) |


**๐Ÿ”น 5. Transactions**

| Feature | **Stored Procedure** | **User Defined Function** |
| -------------------- | ----------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| Transactions Support | Can handle and manage transactions using `BEGIN TRANSACTION`, `COMMIT`, `ROLLBACK`. | Cannot handle transactions. No transaction control allowed. |

**๐Ÿ”น 6. Error Handling**

| Feature | **Stored Procedure** | **User Defined Function** |
| -------------- | ------------------------------------------------------- | ----------------------------------------------------- |
| Error Handling | Supports `TRY...CATCH` blocks and can use `RAISEERROR`. | Limited or **no** advanced error handling mechanisms. |


**๐Ÿ”น 7. Performance**

| Feature | **Stored Procedure** | **User Defined Function** |
| ----------- | ------------------------------------------------------------------- | ---------------------------------------------------------------------------------------- |
| Performance | More flexible and efficient for batch processing and complex logic. | Suitable for quick computations and small logic. Can have performance issues if misused. |

**๐Ÿ”น 8. Example**

- ๐Ÿงพ Stored Procedure:

```sql
CREATE PROCEDURE GetEmployeeById
@EmpId INT
AS
BEGIN
SELECT * FROM Employees WHERE EmployeeId = @EmpId;
END;
```

- ๐Ÿงฎ User Defined Function (Scalar):

```sql
CREATE FUNCTION GetEmployeeSalary(@EmpId INT)
RETURNS INT
AS
BEGIN
DECLARE @Salary INT;
SELECT @Salary = Salary FROM Employees WHERE EmployeeId = @EmpId;
RETURN @Salary;
END;
```

**โœ… Summary Table:**

| Feature | Stored Procedure | User Defined Function |
| --------------------------------- | ------------------ | -------------------------- |
| Returns multiple values? | โœ… Yes | โŒ No (except table-valued) |
| Modifies data? | โœ… Yes | โŒ No |
| Used in SELECT/WHERE? | โŒ No | โœ… Yes |
| Handles Transactions? | โœ… Yes | โŒ No |
| Supports Error Handling? | โœ… Yes | โŒ Limited |
| Calls other procedures/functions? | โœ… Yes | โŒ Not recommended |
| Use Case | Complex operations | Reusable computations |

<div align="right">
<b><a href="#table-of-contents">โ†ฅ back to top</a></b>
</div>

#### Q. How can you raise custom errors from stored procedure?

<div align="right">
Expand Down