0% found this document useful (0 votes)
15 views13 pages

Stored Procedure and Function 21-05-2024

Uploaded by

Vaishnavi patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views13 pages

Stored Procedure and Function 21-05-2024

Uploaded by

Vaishnavi patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

STORED ROUTINES

A stored routine is a set of SQL statements


that can be stored in the server

• STORED PROCEDURE
• FUNCTIONS(USER DEFINED)
STORED PROCEDURE

IT’S A METHOD TO ENCAPSULATE REPEATATIVE TASKS

DELIMITER &&
CREATE PROCEDURE procedure_name [[IN | OUT | INOUT] paramet
er_name datatype [, parameter datatype]) ]
BEGIN
Declaration_section
Executable_section
END &&
DELIMITER ;
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.
Each parameter is assigned a name, a data type, and
direction like Input, Output, or Return. If a direction is not
specified, then by default, it is Input.
DELIMITER $$
CREATE PROCEDURE PROCEDURE_NAME( IN PARAMETER)
BEGIN
….
END$$
DELIMITER;
CALL procedure_name ( parameter(s))
OUT Parameters
The OUTPUT parameter is used when you want to
return some value from the stored procedure.

DELIMITER //
CREATE PROCEDURE PROCEDURE_NAME(OUT PARAMETER)
BEGIN
SELECT…INTO
END //
DELIMITER ;

EVERYTIME YOU CREATE A PROCEDURE CONTAINING BOTH IN


AND OUT PARAMETER USE SELECT-INTO STRUCTURE
INOUT PARAMETER

When you want to pass in an initial value,update the value in the function and return
its updated value.

DELIMITER //
CRETE PROCEDURE PROCEDURE_NAME(INOUT PARAMETER)
BEGIN
SELCT..INTO…
END //
DELIMITER ;
MySQL Stored Function
A stored function in MySQL is a set of SQL statements that perform some
task/operation and return a single value. It is one of the types of stored
programs in MySQL.
DELIMITER //
CREATE FUNCTION FUNCTION_NAME(PARAMETER DATATYPE)
RETURNS DATATYPE
BEGIN
DECLARE VARIABLE_NAME DATATYPE
SELECT…;
RETURN VARIABLE_NAME;
END //
DELIMITER;

The function parameter may contain only the IN parameter but can't allow specifying this parameter
1.Return Value:
1. Function: Always returns a single value, either a scalar or a table.
2. Stored Procedure: Can return zero, single, or multiple values.

2.Parameters:
1. Function: Allows only input parameters and does not allow output parameters.
2. Stored Procedure: Allows both input and output parameters.

3.Statements Allowed:
1. Function: Only SELECT statements; DML statements like UPDATE and INSERT are not
allowed.
2. Stored Procedure: Can perform any operation on database objects, including SELECT
and DML statements.
4.Calling from Other Objects:
1. Function: Can be called from a SELECT statement.
2. Stored Procedure: Cannot be called from a SELECT/WHERE or HAVING statement
directly; an EXECUTE statement must be used.
MySQL Variables

Sometimes, you want to pass a value from an SQL


statement to other SQL statements within the
same session.
To do this, you store the value in a user-defined
variable in the first statement and use it in the
subsequent statements.
To create a user-defined variable, you use the following syntax:
@variable_name
MySQL offers two ways to assign a value to a user-defined variable.
1) Using the SET statement
To assign a value to a variable, you can use the SET statement as follows:

SET @variable_name = value;

This statement assigns the value to the @variable_name.


Besides using the assign operator =, you can use the := operator

SET @variable_name := value;

2) Using the SELECT statement


The following SELECT statement assigns a value to the user-defined variable @variable_name:
SELECT value INTO @variable_name;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy