Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

PLSQL

The document provides an overview of stored procedures, functions, and triggers in SQL, detailing their structure and usage. It explains how to declare and use stored procedures and cursors in MySQL, including syntax and examples. Additionally, it discusses the advantages and disadvantages of using cursors for data manipulation in databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PLSQL

The document provides an overview of stored procedures, functions, and triggers in SQL, detailing their structure and usage. It explains how to declare and use stored procedures and cursors in MySQL, including syntax and examples. Additionally, it discusses the advantages and disadvantages of using cursors for data manipulation in databases.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming

Maithili Paranjape
Objectives

 Introduction to stored procedures


 Structure of stored procedure
 Writing stored procedure
 What is a cursor?
 Writing a cursor
Basic Programming Structures
 Stored Procedures
 Blocks of code stored in the database that are pre-compiled.
 They can operate on the tables within the database and return scalars or results
sets.
 Functions
 Can be used like a built-in function to provide expanded capability to your SQL
statements.
 They can take any number of arguments and return a single value.
 Triggers
 Kick off in response to standard database operations on a specified table.
 Can be used to automatically perform additional database operations when the
triggering event occurs.
Stored Procedures in MySQL
 A stored procedure contains a sequence of SQL commands stored in
the database catalog so that it can be invoked later by a program
 Stored procedures are declared using the following syntax:
Create Procedure <proc-name>
(param_spec1, param_spec2, …, param_specn )
begin
-- execution code
end;
where each param_spec is of the form:
[in | out | inout] <param_name> <param_type>
 in mode: allows you to pass values into the procedure,
 out mode: allows you to pass value back from procedure to the calling program
Structure of Stored procedure
 DECLARE
 This particular section is optional and can be skipped if no declarations are needed.
 This should be the first section in a PL/SQL block, if present.
 BEGIN
 This can contain both PL/SQL code and SQL code.
 This can contain one or many blocks inside it as a nested block.
 This section starts with the keyword 'BEGIN'.
 This section should be followed either by 'END' or Exception-Handling section (if present)
 EXCEPTION
 This is the section where the exception raised in the execution block is handled.
 This section is the last part of the PL/SQL block.
 Control from this section can never return to the execution block.
 This section starts with the keyword 'EXCEPTION'.
 This section should always be followed by the keyword 'END'.
Declaring Variables
 Variables can have any SQL datatype, such as CHAR, DATE, or NUMBER,
or a PL/SQL-only datatype, such as BOOLEAN or PLS_INTEGER
DECLARE
part_no NUMBER(6);
part_name VARCHAR2(20);
in_stock BOOLEAN;
part_price NUMBER(6,2);
part_desc VARCHAR2(50);
 Variable anchors PL/SQL provides you with a very useful feature
called variable anchors.
 It refers to the use of the %TYPE keyword to declare a variable with the data
Naming convention type is associated with a column’s data type of a particular column in a table.
Database Cursor
 Cursor is a Database object which allows us to process each row and
manipulate its data.
 A Cursor is always associated with a Select Query and it will process each
row returned by the Select Query one by one.
 Using Cursor we can verify each row data, modify it or perform calculations
which are not possible when we get all records at once.
 A simple example would be a case where you have records of Employees
and you need to calculate Salary of each employee after deducting Taxes
and Leaves.
 The major disadvantage of a Cursor is its performance issue. A Cursor can
be really slow when performing operations on large number of records and
your SQL Query may take minutes to execute and produce results.
Using cursor

 A cursor can be viewed as a pointer to one row in a set of rows. The cursor can only
reference one row at a time, but can move to other rows of the result set as needed.
 To use cursors in SQL procedures, you need to do the following:
 Declare a cursor that defines a result set.
 Open the cursor to establish the result set.
 Fetch the data into local variables as needed from the cursor, one row at a time.
 Close the cursor when done
 To work with cursors you must use the following SQL statements:
 DECLARE CURSOR
 OPEN
 FETCH
 CLOSE
use myproject;
DECLARE
@emp_name VARCHAR(MAX),
@emp_salary DECIMAL;

Cursor DECLARE cursor_emp CURSOR


FOR SELECT

Example Ename,
Salary
FROM
Employee;
Steps:
1. Declare two variables to hold OPEN cursor_emp;
the data in attributes, and a
cursor to hold the result of a FETCH NEXT FROM cursor_emp INTO
query @emp_name,
2. Open the cursor @emp_salary;
3. Fetch each row from the
cursor WHILE @@FETCH_STATUS = 0
4. Close the cursor BEGIN
5. Deallocate the cursor PRINT @emp_name + CAST(@emp_salary AS
varchar);
FETCH NEXT FROM cursor_emp INTO
@emp_name,
@emp_salary;

You might also like