0% found this document useful (0 votes)
7 views16 pages

STORED PROCEDURE and Cursor

A stored procedure in PL/SQL is a precompiled subprogram that performs specific tasks in a database, enhancing efficiency and security. It can be used for data validation, access control, and complex logic operations, but may slow development and be difficult to manage. Cursors are control structures for processing individual rows of data, with bound and unbound types for static and dynamic queries, respectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views16 pages

STORED PROCEDURE and Cursor

A stored procedure in PL/SQL is a precompiled subprogram that performs specific tasks in a database, enhancing efficiency and security. It can be used for data validation, access control, and complex logic operations, but may slow development and be difficult to manage. Cursors are control structures for processing individual rows of data, with bound and unbound types for static and dynamic queries, respectively.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

STORED PROCEDURE

What is mean by stored procedure?


• A stored procedure in PL/SQL is a subprogram stored in the database that can be invoked with a
call statement. It is used to perform a specific task or a set of tasks.
• A stored procedure is a precompiled collection of SQL statements and optional control-of-flow
statements, stored under a name and processed as a unit.
• Stored procedures can encapsulate complex operations, allowing them to be executed with a
single call, making database operations more efficient and secure.

• Stored procedures can be used for a variety of tasks, including:


1. Data validation before it is inserted or updated in a database.
2. Access control by restricting direct access to data and providing a defined interface through
procedures.
3. Data transformation operations that prepare data for analysis or reporting.
4. Complex logic that involves looping, conditionals, and transaction control which would be
inefficient or cumbersome to implement in plain SQL.
Advantages
• Increase application performance because the user-defined functions and
stored procedures are pre-compiled and stored in the PostgreSQL database
server.
• Reusable in many applications. Once you develop a function, you can reuse
it in any applications.
• Reduce the number of round trips between applications and database
servers. All SQL statements are wrapped inside a function stored in the
PostgreSQL database server so the application only hasto issue a function
call to get the result back instead of sending multiple SQL statements and
wait for the result between each call.
Disadvantages
• Slow in software development because stored procedure programming
requires specialized skills that many developers do not possess.
• Difficult to manage versions and hard to debug.
• May not be portable to other database management systems e.g., MySQL
or Microsoft SQL Server.
SYNTAX
create [or replace] procedure procedure_name(parameter_list)
language plpgsql as
$$
declare
-- variable declaration
begin
--stored procedure body
end
$$;
• First,specify the name of the stored procedure after the create
procedure keywords.
• Second, define parameters for the stored procedure. A stored
procedure can accept zero or more parameters.
• Third, specify plpgsql as the procedural language for the stored
procedure. Note that you can use other procedural languages for the
stored procedure such as SQL, C, etc.
• Finally, use the dollar-quoted string constant syntax to define the
body of the stored procedure.
• Parameters in stored procedures can have the in and inout modes.
They cannot have the out mode.
Passing and returning data
• Passing data into a stored procedure in PL/SQL is done through
parameters.There are three types of parameters you can use:
• IN: Used for passing data into the procedure.
CREATE [OR REPLACE] PROCEDURE procedure_name (
param_name1 IN datatype, param_name12 IN datatype ... )
param_name1, param_name2... are unique parameter names.
datatype - defines the datatype of the variable.
• OUT: Used for returning data from the procedure.
CREATE [OR REPLACE] PROCEDURE proc2 (param_name OUT datatype)
• IN OUT: Used for passing data into the procedure and returning modified data.
CREATE [OR REPLACE] PROCEDURE proc3 (param_name IN OUT datatype)
CURSOR
• A cursor is a named control structure used by an application
program to point to and select a row of data from a result set.
• Cursors are typically used in situations where you need to process
individual rows of a result set sequentially.
• The major function of a cursor is to retrieve data, one row at a time,
from a result set, unlike the SQL commands which operate on all the
rows in the result set at one time.
• Cursors are used when the user needs to update records in a
singleton fashion or in a row by row manner, in a database table.
DECLARE
CURSOR cursor_name IS SELECT columns FROM table_name WHERE
conditions;
-- Declare variables to store values fetched from cursor
variable_name table_name.column_name%TYPE;
BEGIN
OPEN cursor_name; -- Open the cursor
FETCH cursor_name INTO variable_name; -- Fetch a row from the
cursor into variables
-- Process the fetched row
-- Repeat FETCH statement as needed
CLOSE cursor_name; -- Close the cursor
END;
Bound cursor
• A bound cursor is associated with a specific query at compile time.
• This means that the query associated with a bound cursor is fixed
and cannot be changed during runtime.
• Bound cursors are typically used for static queries where the result
set is known at compile time.
Unbound Cursor
An unbound cursor is associated with a query at runtime.
This means that the query associated with an unbound cursor can be
changed dynamically during the execution of the program.
Unbound cursors are typically used for dynamic queries where the
result set may change based on user input or other factors.
Declaring a Cursor
• In PL/SQL, you can declare a cursor using the CURSOR declaration
within a DECLARE block.
• Here's the general syntax for declaring a cursor:
DECLARE
CURSOR cursor_name IS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
• Here's a simple example that demonstrates how to declare a cursor:

DECLARE
CURSOR employee_cursor IS
SELECT employee_id, first_name, last_name FROM employees
WHERE department_id = 20;
BEGIN
-- Cursor declaration
-- Placeholder for further code
END;
Opening a Cursor
• In PL/SQL, after declaring a cursor, you need to open it before fetching
data from it. Here's how you can open a cursor:

DECLARE
CURSOR cursor_name IS
SELECT column1, column2, ... FROM table_name WHERE condition;
BEGIN
OPEN cursor_name;
-- Fetch and process rows here
CLOSE cursor_name; -- Close the cursor when done
END;

• After opening a cursor, we can manipulate it using FETCH, MOVE,


UPDATE, DELETE statement
FETCH AND CLOSE
• You can then use the FETCH statement to retrieve rows from the cursor one at a
time, and the CLOSE statement to close the cursor when you're done fetching rows.
DECLARE
CURSOR cursor_name IS
SELECT column1, column2, ...FROM table_name WHERE condition;
BEGIN
OPEN cursor_name;
FETCH cursor_name INTO variable1, variable2;
-- Process the fetched row
CLOSE cursor_name;
END;
• MOVE
In PL/SQL, you can move a cursor to a specific row using the FETCH statement with the
POSITION clause.
This allows you to move the cursor to a particular row in the result set based on its
position.
‘DECLARE
CURSOR cursor_name IS
SELECT column1, column2, ... FROM table_name WHERE condition;
BEGIN
OPEN cursor_name;
-- Move cursor to the 3rd row
FETCH cursor_name INTO variable1, variable2 POSITION 3;
-- Process the row
CLOSE cursor_name;
END’;
• UPDATE
Once a cursor is positioned, we can update row identifying by the
cursor using UPDATE WHERE CURRENT OF statement as follows:
update table_name set column = value, ...where current of
cursor_variable;

DELETE
Once a cursor is positioned, we can delete row identifying by the
cursor using DELETE WHERE CURRENT OF statement
delete from table_name where current of cursor_variable;
Looping through a cursor
• Loops can be used to iterate through records of a cursor.
• Eg:-
DO $$
DECLARE
Cur_books CURSOR FOR SELECT * FROM BOOKS;
row_books RECORD;
BEGIN;
OPEN cur_books;
LOOP
FETCH cur_books INTO row_books;
EXIT WHEN NOT FOUND;
RAISE NOTICE ‘%,%,Rs %’, row_books.TITLE, row_books.author, row_books.price;
END LOOP;
END; $$

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