0% found this document useful (0 votes)
1 views

Stored Procedures SQL Notes

The document provides an overview of stored procedures in SQL, detailing their syntax, parameters, variable handling, control flow statements, and error handling. It emphasizes the use of transactions, cursors, temporary tables, and security permissions, while also highlighting the benefits of modularity and reusability. Additionally, it mentions advanced topics such as dynamic SQL and performance analysis.

Uploaded by

Udit Parmar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Stored Procedures SQL Notes

The document provides an overview of stored procedures in SQL, detailing their syntax, parameters, variable handling, control flow statements, and error handling. It emphasizes the use of transactions, cursors, temporary tables, and security permissions, while also highlighting the benefits of modularity and reusability. Additionally, it mentions advanced topics such as dynamic SQL and performance analysis.

Uploaded by

Udit Parmar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Stored Procedures in SQL - Concepts & Examples

1. Procedure Syntax

Stored Procedures are created using:


SQL Server: CREATE PROCEDURE ... AS BEGIN ... END
MySQL: CREATE PROCEDURE ... BEGIN ... END;

Example (SQL Server):


CREATE PROCEDURE GetAllEmployees AS BEGIN SELECT * FROM Employees; END

2. Parameters

- IN: Input parameter


- OUT: Output parameter
- INOUT: Both input and output

Example (MySQL):
CREATE PROCEDURE GetEmployee(IN empId INT) BEGIN SELECT * FROM Employees WHERE Id = empId; END

3. Variable Handling

Variables can be declared and set inside SPs.


Example:
DECLARE @total INT;
SET @total = 100;

4. Control Flow Statements

- IF...ELSE for conditional logic


- CASE for multiple conditions
- WHILE or LOOP for iteration

Example:
IF @salary > 10000 BEGIN PRINT 'High Salary'; END

5. SQL Queries

Stored Procedures often include SELECT, INSERT, UPDATE, DELETE operations.


Stored Procedures in SQL - Concepts & Examples

Example:
INSERT INTO Employees(Name) VALUES ('John');

6. Error Handling

SQL Server: Use TRY...CATCH


MySQL: DECLARE ... HANDLER

Example:
BEGIN TRY ... END TRY BEGIN CATCH ... END CATCH

7. Transactions

Used for atomic operations.


BEGIN TRANSACTION
... SQL Statements ...
COMMIT / ROLLBACK

8. Returning Data

Use SELECT to return data.


Use OUTPUT parameters or RETURN for specific values.

9. Cursors

Used to process rows individually.


DECLARE cursor CURSOR FOR SELECT ...

10. Temporary Tables

Use #Temp or table variables for intermediate results.


Example: SELECT ... INTO #TempTable

11. Permissions and Security

Use GRANT EXECUTE ON PROCEDURE to give access.


Stored Procedures in SQL - Concepts & Examples

12. Modularity & Reusability

Encapsulate logic inside SPs to reuse code and simplify maintenance.

Bonus: Dynamic SQL, Nesting, Performance

You can build SQL dynamically, call SPs inside SPs, and analyze execution plans for performance.

You might also like