You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+91Lines changed: 91 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1608,6 +1608,97 @@ DROP PROCEDURE IF EXISTS total_duration;
1608
1608
```
1609
1609
1610
1610
#### 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)** |
| 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. |
| Return Value | Can return **0or 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** |
| Use inSELECT/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** |
| 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** |
| 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 |
0 commit comments