Stored Procedure used in PosgreSQL

32
Professor: Dr. Shu-Ching Chen TA: Hsin-Yu Ha Stored Procedure used in PosgreSQL

description

Stored Procedure used in PosgreSQL. Professor: Dr. Shu-Ching Chen TA: Hsin -Yu Ha. What are stored procedures. A subroutine available to applications that access a relational database system. PL/ pgSQL : A loadable procedural language. Creates functions and trigger procedures - PowerPoint PPT Presentation

Transcript of Stored Procedure used in PosgreSQL

Page 1: Stored Procedure used in  PosgreSQL

Professor: Dr. Shu-Ching ChenTA: Hsin-Yu Ha

Stored Procedure used in PosgreSQL

Page 2: Stored Procedure used in  PosgreSQL

What are stored proceduresA subroutine available to applications that

access a relational database system.PL/pgSQL : A loadable procedural language.

Creates functions and trigger proceduresAdds control structuresPerforms complex computationInherits all user-defined types, functionsCan be defined to be trusted by the serverEasy to use

Page 3: Stored Procedure used in  PosgreSQL

Why do we need stored procedure

One Query Wait, receive, process/computeDatabase

ServerIntern

et

Reduce roundtrips across the networkCan make security easier to manageAre precompiled

Page 4: Stored Procedure used in  PosgreSQL

Structure of PL/pgSQL

Page 5: Stored Procedure used in  PosgreSQL

Declarations (1)Declaring PL/pgSQL variable

Page 6: Stored Procedure used in  PosgreSQL

Declarations (2)Declaring PL/pgSQL variable and assigning

values

Page 7: Stored Procedure used in  PosgreSQL

Declarations (3)Declaring Function Parameters(1) directly give a name to the parameter in the command

(2) name ALIAS FOR $n;

Page 8: Stored Procedure used in  PosgreSQL

Declarations (4)Directly using argument variables

Page 9: Stored Procedure used in  PosgreSQL

Declarations (5)Attributes

%TYPE attribute

Page 10: Stored Procedure used in  PosgreSQL

Declarations (6)Attributes

%ROWTYPE attribute

Page 11: Stored Procedure used in  PosgreSQL

Comment syntax Single-line comments

Block comments

Page 12: Stored Procedure used in  PosgreSQL

Basic Statements (1)Assignment

Executing a Command with NO RESULT – PERFORM

Page 13: Stored Procedure used in  PosgreSQL

Basic Statements (2)Executing a Command with a Single-row

result

Page 14: Stored Procedure used in  PosgreSQL

Basic Statements (3)Example

Page 15: Stored Procedure used in  PosgreSQL

Basic Statements (4)

Page 16: Stored Procedure used in  PosgreSQL

Basic Statements (5)FOUND – Boolean variable

Page 17: Stored Procedure used in  PosgreSQL

Control Structures(1)RETURN expression

Page 18: Stored Procedure used in  PosgreSQL

Control Structures(2)IF statements

IF … THENIF … THEN … ELSEIF … THEN … ELSIF … THEN … ELSE

Page 19: Stored Procedure used in  PosgreSQL

Control Structures(3)CASE statements

CASE … WHEN … THEN … ELSE … END CASECASE WHEN … THEN … ELSE … END CASE

Page 20: Stored Procedure used in  PosgreSQL

Control Structures(4)LOOP

EXIT

Page 21: Stored Procedure used in  PosgreSQL

Control Structures(5)CONTINUE

WHILE

Page 22: Stored Procedure used in  PosgreSQL

Control Structures(6)FOR (Integer Variant)

Page 23: Stored Procedure used in  PosgreSQL

Control Structures(7)FOR (Looping through query results)

Page 24: Stored Procedure used in  PosgreSQL

Control Structures(8)Trapping Errors

http://www.postgresql.org/docs/9.1/static/errcodes-appendix.html#ERRCODES-TABLE

Page 25: Stored Procedure used in  PosgreSQL

Cursors (1)Declaring Cursor Variables

OPEN FOR query

Page 26: Stored Procedure used in  PosgreSQL

Cursors (2)Using Cursors

FETCH

MOVE

NEXT PRIORFIRSTLASTABSOLUTE countRELATIVE countFORWARDBACKWORD

Page 27: Stored Procedure used in  PosgreSQL

Cursors (3)Using Cursors

CLOSE

Returning Cursor

Page 28: Stored Procedure used in  PosgreSQL

Cursors (4)Looping Through a Cursor’s Result

Page 29: Stored Procedure used in  PosgreSQL

Errors and MessagesRAISE

Example

Page 30: Stored Procedure used in  PosgreSQL

ReferencePostgreSQL Manuals PostgreSQL 9.1

http://www.postgresql.org/docs/9.1/static/index.html

Practical PostgreSQLhttp://www.faqs.org/docs/ppbook/c19610.htm

Page 31: Stored Procedure used in  PosgreSQL

Stored Procedure in PgAdmin

1

2 3

Page 32: Stored Procedure used in  PosgreSQL

Stored Procedure in PgAdmin