1 / 25

PL/pgSQL Triggers and Stored Procedures

Learn about triggers and stored procedures using PL/pgSQL in database systems. Includes examples and syntax.

hurlbut
Download Presentation

PL/pgSQL Triggers and Stored Procedures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CS422 Principles of Database SystemsTriggers and Stored Procedures using PL/pgSQL Chengyu Sun California State University, Los Angeles

  2. PL/pgSQL Example CREATE FUNCTION add10( int, int ) RETURNS int AS ' BEGIN RETURN $1 + $2 + 10; END; ' LANGUAGE plpgsql;

  3. PL/pgSQL General Syntax CREATE [OR REPLACE] FUNCTION func( parameter_types ) RETRUN return_type AS ‘ DECLARE declarations BEGIN statements END; ’ LANGUAGE plpgsql

  4. pgNOTES • No “create procedure” • create or replace • no “drop if exists” though • Parameter and return types • SQL types • row and record • set • void • Single quotes

  5. Comments • SQL-style: -- • C-style: /* */ -- this is an one line comment return $1 + $2; -- return sum of the two params /* a multiple-line comment */

  6. Errors and Messages RAISEINFO‘‘Calling cs_create_job(%)’’, v_job_id; RAISEEXCEPTION‘‘Inexistent ID --> %’’, user_id;

  7. Declarations Syntax: name type [ {default | :=} expression ]; name aliasfor $n Examples: id int default 0; name varchar(20) := ‘‘cysun’’; op1 alias for $1; op2 alias for $2;

  8. Composite Variable Types • Row type: • name table%ROWTYPE; • Record type: • name RECORD;

  9. Composite Return Types • RECORD • Table type • User defined type CREATE TYPE name AS ( attr_name attr_type [,...] );

  10. Statement – Assignment identifier := expression ;

  11. Statement – SELECT INTO • target is of row or record type, or a list of variable names • takes the value of the first row returned • null if no row is returned SELECT INTO target col(s) FROM ... WHERE;

  12. Statement – RETURN • RETURN statement is required even for functions that return void • RETURN NEXT builds up the result set, and does not exit the function RETURN expression; RETURN NEXT expression; RETURN;

  13. Cursors name CURSOR [ ( arguments ) ] FOR query ; Examples: curs1 refcursor; curs2 CURSOR FOR SELECT * FROM table1; curs3 CURSOR (k integer) FOR SELECT * FROM table1 WHERE key=k;

  14. Opening and Closing Cursors • Bound and unbound cursors OPEN curs1 FOR SELECT * FROM table2; OPEN curs2; OPEN curs3(47); CLOSE curs1; CLOSE curs2; CLOSE curs3;

  15. Fetching From a Cursor • Special variable FOUND FETCH cursor INTO target; • Examples: • FETCH curs1 INTO record1; • FETCH curs2 INTO v1, v2, v3;

  16. Statement – Conditionals IF boolean-expression THEN statements END IF; IF boolean-expression THEN statements ELSE statements END IF; IF boolean-expression THEN statements ELSIF boolean-expression THEN statements ELSE statements ] END IF;

  17. Statement – Simple Loops WHILE expression LOOP statements END LOOP; LOOP statements END LOOP; EXIT [ WHEN expression ];

  18. Statement – FOR (Integer) FOR name IN [ REVERSE ] expression .. expression LOOP statements END LOOP; Examples: FOR i IN 1..10 LOOP -- something here END LOOP; FOR i IN REVERSE 10..1 LOOP -- something here END LOOP;

  19. Statement – FOR (Query) FOR record_or_rowtype IN query LOOP statements END LOOP;

  20. Create Triggers CREATE TRIGGER name { BEFORE | AFTER } { event [ OR ... ] } ON table [ FOR [ EACH ] { ROW | STATEMENT } ] EXECUTE PROCEDURE funcname ( arguments )

  21. About Triggers • Event • insert, update, delete • A trigger is associated with a table • The trigger can be executed • once per statement, or • once per row

  22. About Firing Triggers • Multiple triggers are fired in alphabetic order by trigger name • No restrictions on cascading triggers

  23. About Trigger Procedures • Must be defined before the trigger is created • Must be declared as no parameter and return a trigger type • Return • NULL, or • the tuple to be modified

  24. Trigger Procedure in PL/pgSQL • Special variables • NEW, OLD • TG_NAME, TG_WHEN, TG_LEVEL, TG_OP • TG_RELID, TG_RELNAME • TG_NARGS, TG_ARGV[]

  25. Trigger Examples • fk_insert_trig • emp_stamp • http://www.postgresql.org/docs/7.4/static/plpgsql-trigger.html

More Related