Posted by: ptilley125 on: April 3, 2009
PL/SQL, or Procedural SQL, allows for procedural or programming code and SQL statements with databases. Basic SQL has its limitations and PL/SQL supports many useful functions including conditional statements, loops, and error trapping. Many of these functions are available in programming languages such as Java. Users can create:
Anonymous blocks – a set of statements that are usually run once.
DECLARE variable:= value;
BEGIN statements;
END;
Triggers – execute code when a specific action occurs
CREATE OR REPLACE TRIGGER name
AFTER INSERT OR UPDATE OF column ON table
BEGIN commands;
END;
Stored Procedures – named collection of statements
CREATE OR REPLACE PROCEDURE name
AS BEGIN commands;
END;
Cursors – Allows you to return more than one value without generating an error. Holds data returned from a SQL query in the memory of the DBMS server. Inside of a DECLARE section use the following code:
CURSOR name IS select-query;
Use OPEN, FETCH, or CLOSE after declaring the cursor.
More detail on pg 357-358 of the book.
Functions – allows you to return a value from a named collection of statements
CREATE FUNCTION name
BEGIN commands;
RETURN (value or expression);
END;
Use the line DBMS_OUTPUT.PUT_LINE(); to produce a specific message to the server output.
To create a loop, use to following code between the BEGIN and END statements:
WHILE condition
LOOP statements;
END LOOP;