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

SQL Stored Procedures

SQL stored procedures allow reusable SQL code to be saved and called repeatedly. They can accept parameters to make the procedure code dynamic. Stored procedures offer benefits like reduced network traffic, ease of maintenance, and security. They are defined using CREATE PROCEDURE and called using CALL. Parameters can be IN (default), OUT, or INOUT depending on whether the value is passed into or out of the procedure.

Uploaded by

Keerthana P
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views

SQL Stored Procedures

SQL stored procedures allow reusable SQL code to be saved and called repeatedly. They can accept parameters to make the procedure code dynamic. Stored procedures offer benefits like reduced network traffic, ease of maintenance, and security. They are defined using CREATE PROCEDURE and called using CALL. Parameters can be IN (default), OUT, or INOUT depending on whether the value is passed into or out of the procedure.

Uploaded by

Keerthana P
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SQL 

Stored Procedures
• a collection of pre-compiled SQL
statements stored inside the database
• a subroutine or a subprogram
• A procedure always contains a name,
parameter lists, and SQL statements
• A procedure is called a recursive stored
procedure when it calls itself
INTRODUCTION
• A stored procedure is a prepared SQL code that
you can save, so the code can be reused over
and over again.
• SQL query (write over and over again)->save it
as a stored procedure->call it to execute it.
• You can also pass parameters to a stored
procedure, so that the stored procedure can act
based on the parameter value(s) that is passed.
benefits of a stored procedure:
• Reduce the Network Traffic: 
• Easy to maintain:
• Secure:
DELIMITER &&  
CREATE PROCEDURE procedure_name [[IN | OUT | IN
OUT] parameter_name datatype [, parameter datatyp
e]) ]    
BEGIN    
    Declaration_section    
    Executable_section    
END &&  
DELIMITER 
MySQL procedure parameter has one of
three modes:
• IN parameter
• It is the default mode. It takes a parameter as input, such as an attribute.
When we define it, the calling program has to pass an argument to the stored
procedure. This parameter's value is always protected.
• OUT parameters
• It is used to pass a parameter as output. Its value can be changed inside the
stored procedure, and the changed (new) value is passed back to the calling
program. It is noted that a procedure cannot access the OUT parameter's initial
value when it starts.
• INOUT parameters
• It is a combination of IN and OUT parameters. It means the calling program can
pass the argument, and the procedure can modify the INOUT parameter, and
then passes the new value back to the calling program.
Procedures with IN Parameter

• DELIMITER &&  
• CREATE PROCEDURE get_student (IN var1 INT)  
• BEGIN  
•     SELECT * FROM student_info LIMIT var1;  
•     SELECT COUNT(stud_code) AS Total_Student 
FROM student_info;    
• END &&  
• DELIMITER ;  
Procedures with OUT Parameter

• DELIMITER &&  
• CREATE PROCEDURE display_max_mark (OUT 
highestmark INT)  
• BEGIN  
•     SELECT MAX(marks) INTO highestmark FRO
M student_info;   
• END &&  
• DELIMITER ;  
Procedures with INOUT Parameter
 CALL statement
• CALL procedure_name ( parameter(s))  
• Create Procedure [Procedure Name]
([Parameter 1], [Parameter 2], [Parameter 3] )
Begin
SQL Queries..
End

• CALL [Procedure Name] ([Parameters]..)


Stored Procedure With One Parameter

• CREATE PROCEDURE SelectAllCustomers @Cit
y nvarchar(30)
AS
SELECT * FROM Customers WHERE City =
@City
GO;

• EXEC SelectAllCustomers @City = 'London';


Stored Procedure With Multiple Parameters

• CREATE PROCEDURE SelectAllCustomers @City
nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City =
@City AND PostalCode = @PostalCode
GO;

• EXEC SelectAllCustomers @City = 'London',


@PostalCode = 'WA1 1DP';

You might also like