PL/SQL Flashcards
What is package?
- Schema object logically groups PL/SQLs
- package specification - interface, public objects
- package definition - not in package specification but only in package definition are private objects
- PL/SQL created inside package can only be dropped with the package (DROP PACKAGE)
What is type?
TODO
What is PL/SQL?
SQL with procedural features of programming language,such as for loop, conditions
https://www.tutorialspoint.com/plsql/index.htm
PL/SQL Basic structure
- block-structured language, each block contains: declarations, execution commands, exception handling
2.EXAMPLE
DECLARE
BEGIN
EXCEPTION
END;
EXAMPLE2 DECLARE message varchar2(20):= 'Hello, World!'; BEGIN dbms_output.put_line(message); END; /
What is a trigger?
- Special stored procedure will run when action (event) is performed (INSERT, DELETE, UPDATE), used to enforce some action, usually 1 table 1 trigger
- Types: After triggers, Instead of triggers(replace the original operation action)
What is a (stored) procedure?
Procedure:
- like a subprogram that is a ‘unit/module’ of the whole program
- used to perform a actions
- Can take input/output parameters
Function:
1. used to compute a value, must return(a single value or a table)
EXAMPLEs:
CREATE PROCEDURE procedure_name AS sql_statement GO; with params
CREATE PROCEDURE SelectAllCustomers @City nvarchar(30), @PostalCode nvarchar(10)
AS
SELECT * FROM Customers WHERE City = @City AND PostalCode = @PostalCode
GO;
What is a Function ?
Difference with stored procedure?
- a set of SQL statements that accept only input parameters, perform actions and return the result.
- A function can return an only a single value or a table.
CREATE OR REPLACE FUNCTION totalCustomers RETURN number IS total number(2) := 0; BEGIN SELECT count(*) into total FROM customers;
RETURN total;
END;
/
Difference
- Procedure can call a function, function can not call a stored procedure
- Function have to return a value, for SP it is optional
- Stored Procedures cannot be used in the SQL statements anywhere in the WHERE/HAVING/SELECT section whereas Function can be.
- Functions that return tables can be treated as another rowset. This can be used in JOINs with other tables.
What is cursor?
- A pointer to the SQL (contect area/memory area)
- Holds a set of rows returned by a SQL statement - called active set
- create a cursor that hold the returned rows of SQL statements
Working with an explicit cursor includes the following steps −
Declaring the cursor for initializing the memory
Opening the cursor for allocating the memory
Fetching the cursor for retrieving the data
Closing the cursor to release the allocated memory
CURSOR c_customers IS
SELECT id, name, address FROM customers;
OPEN c_customers;
FETCH c_customers INTO c_id, c_name, c_addr;
CLOSE c_customers;