Stored procedures and functions

Procedures and functions stored in the database

Stored procedures

1

stored procedures • Ordinary layered model – – – – user interface functions model data • Layered model with stored procedures – user interface – functions in DB – model = data in DB Stored procedures 2 .Layered models vs.

14-6. if statements. etc. page 321 Stored procedures 3 . – Syntax checked and compiled into p-code • The p-code is stored in the database • Stored procedures’ syntax – General syntax. page 314 – Example.Stored procedures / functions 313 • Stored procedures /functions are – Named blocks of PL/SQL • SQL DDL • Assignments. loops. page 315 • Stored functions’ syntax – General syntax. page 319 – Example fig.

314 • Procedures can have 3 types of parameters – IN • used for input – OUT – INOUT • used for output • side effects ⇒ hard to read / debug the code • used for input + output – Examples • scottTigerStoredProcedures.3 types of parameters.sql • Functions – standard: only IN parameters – Oracle: all kinds of parameters • Don’t use OUT and INOUT with functions! Stored procedures 4 .

…) – Examples page 314 • 2 ways to link formal and actual parameters – Position • Like Java: 1st parameter formal parameter linked to 1st actual parameter.sql Stored procedures 5 . etc.sql – Named • Syntax: formalParameterName => value • Example: calling insertDept in scottTigerStoredProcedures. • Examples: – Figure 14-2. 314 • Syntax – procedureName(formalParameter1.Calling a stored procedure. formalParameter2. page 316 – calling insertDept in scottTigerStoredProcedures.

Some PL/SQL to use in the body of stored procedures and functions • call pName(parameters) • begin … end – call another procedure – statement group • return value – return from a function • variable := value – assignment • if condition then statements else statements end if – Example page 329 • For loop • While loop • General loop – Inner exit statement Stored procedures 6 .

insert. update.SQL statements • Stored procedures / functions can contain SQL statements – select. page 318 • Insert example – insertDept (my own example) Stored procedures 7 . delete • Select syntax [result: one value] – select attr into variable from … • Example figure 14-4.

– Very much like JDBC • Example fig. 268 • Cursor points to the current row.executeQuery(…) Stored procedures 8 .Cursors. page 317 • DECLARE cName CURSOR FOR select statement – declares the select statement – JDBC statement object • OPEN cName – Executes the select statement – JDBC ResultSet rs = statement. 14-3.

page 282 Predefined/named system exceptions – When others • Catches exceptions not already caught – General strategy • Don’t catch exceptions if you don’t know how to handle them properly • Writing to the screen is usually not enough Stored procedures 9 . page 318 Fig.Exception handling • Stored procedures can handles exception – – – – Similar to Java try … catch … Syntax page 314 Example fig. 12-7. 14-4.

Calling a function. 14-7. page 321 – SQL statement • Example page 323 Stored procedures 10 . 320 • Functions can be called from – PL/SQL block (like the body of another procedure / function) • Example fig.

317 Stored procedures 11 .Compilation errors in stored procedures / functions.

Compiling and recompiling stored procedures. • If one of the tables used in a procedures is altered the procedure / function must be recompiled – Alter procedure procedureName compile. Stored procedures 12 . 317 • Stored procedures / functions are automatically compiled when recreated.

etc. 14-7.Packages. functions. 323 • A packages groups a set of logically connected stored procedures. – Kind of module • Built-in packages in Oracle – STANDARD • Many functions used in Oracle – DBMS_OUTPUT • Put_line and other procedures • Example fig. page 321 • You can create your own packages! Stored procedures 13 .

functions. 324 • Package specification – Specification of procedures. 14-10. page 327 Stored procedures 14 .Package structure. etc. etc. • Private part of the package • Syntax page 325 • Example fig. page 325 • Package body – Implementation of procedures. • Public part of the package • Syntax page 324 • Example fig. 14-11. functions.

PUT_LINE(…) – myPackage.Calling a procedure / function in a package. page 328 Stored procedures 15 .ProcedureName(…) – DBMS_OUTPUT.myProcedure(…) – Example fig. 14-12. 326 • Name of procedure / function must be prefixed with the name of the package – PackageName.

Java JDBC API • Pakken java. – CallableStatement extends PreparedStatement • PreparedStatement extends Statement Stored procedures 16 . ?)}"). ?.sql – interface CallableStatement – "factoried" by a connection object • CallableStatement prepareCall(String sql) • cst = prepareCall("{call insertDept(?.

java Stored procedures 17 .JDBC examples • Stored procedure with IN parameters • Stored procedure with IN and OUT parameters • Stored function – CallableStatementExample.

'Anders').registerOutParameter(position.setString(1. type) – results can be obtained after executing the call • value = cstm.setXxx(position.Handling parameters • IN paramaters – handled like parameters to prepared statements • cstm. • OUT parameters + results from functions – register type before executing the call • cstm.getXxx(position) Stored procedures 18 . value). • cstm.

0 $"89.902039 W .943 W .0/:708  .0 $9470/574.2501   5.2505.1:3.

425.943077478389470/ 574.0/:708.

9438  $9470/574.0/:708  .1:3.

425389470/ 574.0/:708  W $9470/574.4253.0/:708.3/70.

1:3.70.9438..9.9070/90574.:942.0/:70.574.-08:80/3.4250/0370.70 ..90/ W 143041909.0/:7088 .

0/:70574.20 .9432:89-0 70.4250 $9470/574.4250/ 907574.0/:70.1:3.0/:708  .

!...3/4907574...08  W 5.809414...0/:708  ..90/ 89470/574.4330..0 $%# W .9438 09.3.0/:708 1:3..0 $* &%!&% W !:9*30..0/:708 W .31:3. 3/4124/:0 W :9 35..70.0874:58..083 7..904:7435.9438:80/3 7.08 $9470/574.0 W 4:.2501   5.

.0/:708  .0 W .0850.905...9:70  W !.94341574.5.0 W !. W !:-.. W !7.0/:708 1:3.7941905.7941905.!.0/:708 1:3..943 $50..2501   5.2501   5.0 W $39.0 $9470/574.5.9438 09..0897:.9438 09.....5.94341574..1.1.0-4/ 2502039..0 W $39.0 W .

574.3..0/:70.

.0  W .9433. 5.0/:70.2041574.1:3..

.0 $9470/574.0 2!74.1:3.0/:70.0..2041905..9432:89-0 57010/9903.20 $* &%!&% !&%* 2!..0/:70 ..0/:708  .0 !..2501   5.20 !74.

4330..0..902039 5705..9434-0.70/$9.38079059    <  .895705.70.-0$9..0/:708  ..-0$9.. 86 39071.9020390903/8 !705.. $973 86 W .902039 $9470/574.! W !....9470/-..03..9020390903/8$9. .902039 1.70/$9.9 W .902039 W !705.-0$9.70.

2508 W $9470/574.902039.209078 W $9470/1:3.0/:709.943 . $9470/574.0/:7095.209078 W $9470/574.7.7.3/ &% 5..0..250 ..0/:708  .-0$9.

 W .9078 .:0.9438 708907950-01470 00.209078 708:9817421:3.892 809 548943 .3/35.2..7.892 809$973   3/078  W .7..:9390.:0  W &%5.30/. W .1907 00...209078945705.20907 548943 950 708:98...0/:708  .3-04-9.209078 W 5.892 09 548943 $9470/574.7.:9390.3/0/05.7.7.892 708907 :9!.70/ 89.9020398 W .

Sign up to vote on this title
UsefulNot useful