28
Submitted To : Submitted By : Er.Naresh Garg Sandeep Kumar(4742) Lecturer in RDBMS Pralabh Jain(4736) Puneet Jain(4739) Shiv Kumar(4748) Yohal Garg(D-8206)

Procedures/functions of rdbms

Embed Size (px)

Citation preview

Page 1: Procedures/functions of rdbms

Submitted To: Submitted By:Er.Naresh Garg Sandeep Kumar(4742)Lecturer in RDBMS Pralabh Jain(4736) Puneet Jain(4739) Shiv Kumar(4748) Yohal Garg(D-8206)

Page 2: Procedures/functions of rdbms

Stored procedures and Functions

Ο Collections of SQL and PL/SQL statements.

Ο Stored in complied from in the database.

Ο Can call others and self.

Ο Can be called from all client environments

Ο Procedures and function (including remote) are the same, except a function returns a values and a procedure does not.

Page 3: Procedures/functions of rdbms

Uses for procedures

Ο Define central well-known business functions.

- Create an order

- Delete a customer

Ο Store batch job in the database

-Weekly account rollups

Ο Encapsulate a transaction

- Gather and process information from remote nodes

Ο Funnel activity on a table through a single path

- all changes to employee salaries should change department budgets.

Page 4: Procedures/functions of rdbms

Creating a procedure

Argument Modes

IN Data value comes in from the callingprocess and is not changed

OUT No data value comes in from the callingprocess; on normal exit ,value of argumentis passed back to caller

IN OUT Data value comes in from the callingprocess, and another value is returned onnormal exit

Page 5: Procedures/functions of rdbms

Creating a procedure

Ο Example

Ο Tip:Write each procedure in a text file, and save(both P-code and source code is saved in the database)

CREATE PROCEDURE fire_employee (empid NUMBER)ASBEGIN … DELETE FROM emp WHERE empno= fire_employee.empid;END

Page 6: Procedures/functions of rdbms

Creating and changing a function

ExampleCREATE FUNCTION get_bal (acc_no NUMBER(4)) RETURN NUMBER(11,2);IS acc_bal NUMBER(11,2);BEGIN SELECT balance INTO acc_bal FROM accounts WHERE account_id_no=acc_no; RETURN (acc_bal);END;

Page 7: Procedures/functions of rdbms

Statements in proceduresΟ Valid statements in a procedure or function

Ο Restricted statements

• SQL DML or PL/SQL statements• Calls to other procedures and functions stored in the database• Calls to other procedures and functions in a remote database

• DDL• Dynamic SQL• In trigger, COMMIT,SAVEPOINT, and ROLLBACK

Page 8: Procedures/functions of rdbms

Executing a stored procedureΟ From within a PL/SQL block

Ο On a remote node

Ο From SQL*DBA and some ORACLE tools

Ο Within an anonymous block (when EXECUTE not available)

Ο From a precompiler application

fire_employee (empno);scott.fire_employee (empno);

scott.fire_employee@ny (empno);

EXECUTE fire_employee (1043)

SQLPLUS>BEGINFIRE_EMPLOYEE(1043);END;

EXEC SQL fire_employee (:empno);

Page 9: Procedures/functions of rdbms

Specifying procedure argumentsΟ Example

Ο Positional method

Ο Named method

• CREATE PROCEDURE update_sal (empno NUMBER, bonus NUMBER, sal_incr NUMBER) ….;

• List values in the order they are declared update_sal (7000,20,500);

• List argument names and values in any order, using special syntax update_sal (bonus=>20, sal_incr=>500, empno=>7000);

Page 10: Procedures/functions of rdbms

Specifying procedure argumentsΟ Combination method

• Use both positional and named methods

• Once you specify a named parameter, must use named method for all remaining

update_sal Legal (7000,sal_incr=>500,bonus=>20);

update_sal Illegal (empno=>7000, sal_incr=>500,20);

Page 11: Procedures/functions of rdbms

How procedures are entered into the database

Ο PL/SQL engine compiles the source code

Ο ORACLE stores a procedure in a database:

Ο SQL in procedure not stored in parsed form

• Object name• Source code• Parse tree• Pseudo code(P-code)• Syntax errors in dictionary table• Dependency information

• Uses shared and cached SQL• Allows SQL to be optimized dynamically (without recompiling referencing procedures)

Page 12: Procedures/functions of rdbms

PL/SQL Compilation ErrorsΟ Compile done by PL/SQL engine in RDBMSΟ Error stored in the databaseΟ To view errors:

• Use SQL*DBA command SHOW ERRORS• Select from errors views• USER_ERRORS• ALL_ERRORS• DBA_ERRORS

Page 13: Procedures/functions of rdbms

USER-DEFINED System ErrorsΟ Any procedure can raise an error and return a user defined error message

and error number

Ο Error number range is -20000 to -20999

Ο Range always reserved for user defined errors

Ο Oracle does not check if user defined error numbers are used uniquely

Ο Raise error within PL/SQL block with procedure

Ο Full pathname of procedure may be needed in early releases

raise application_error (error_number,’text of the message’)

sys.standard_utilities. Raise_application_error

Page 14: Procedures/functions of rdbms

USER-DEFINED System Errors Example

CREATE PROCEDURE fire_employee (empid NUMBER)ASBEGIN IF empid <=0 THEN raise_application_error (-20100,’Employee number must be> 0’); ELSE DELETE FROM emp WHERE EMPNO =EMPID; END IF;END;

SQLDBA> EXECUTE FIRE_EMPLOYEE(-1);ORA=-20100: Employee number must be >0

Page 15: Procedures/functions of rdbms

Debugging methodsΟ Version 6

Ο Version 7

Ο Future

• User INSERT’S information into a user defined table, and examines data

• PL/SQL will have methods of I/O to system defined table(TIO$LINES)

• Can use Version6 method

TEXT_IO package

Rolls back or commits with transaction

DEBUG_IO package

Writes despite error, rollback,commit

A PL/SQL debugger

Page 16: Procedures/functions of rdbms

Dependencies and Procedures Ο A procedure is dependent on:

Ο Two types of dependencies

Ο ORACLE automatically checks dependencies

• every database object to which it refers (direct dependency)

• the database objects those objects depend on(indirect dependency)

procedures,functions,packages,tables,views,synonyms,sequences

local: objects are on the same node

remote: objects are on separate nodes

Page 17: Procedures/functions of rdbms

Recompilation of Dependent proceduresΟ When an object changes, its dependent objects are marked for

recompilationΟ Any change to definition of referenced object implies new version

of reference objectΟ Dependent objects must be recompiled if referenced object

changesΟ Recompilation of dependent objects takes place automatically at

runtimeΟ Reasons recompilation could fail

• Changing parameter list in called procedure• Changing definition of or removing referenced column from referenced table• Dropping referenced table

Page 18: Procedures/functions of rdbms

RecompilationΟ Procedure/function can be recompiled be either

Ο If recompilation fails, error information is put to error table

• RDBMS automatically, when next accessed(only if marked for recompilation)

• Manually by the user, using ALTER PROCEDURE command

Page 19: Procedures/functions of rdbms

Manual Recompilation

ALTER PROCEDURE

schema

Procedure COMPILE

Ο Example ALTER PROCEDURE add_department COMPILE

Page 20: Procedures/functions of rdbms

Changing a procedureΟ To modify a procedure, replace it:

Ο OR REPLACE option:

Ο CREATE without OR REPLACE on existing procedure fails

CREATE OR REPLACE PROCEDURE fire_employee AS . . . END;

• Recreates the procedure even if it already exists• Retains existing grants (need not reissue)• Creates procedure even if there are syntax errors• Marks dependent objects for recompilation • Users executing old procedure finish that call: next invocation gets

new procedure• Facilitates development (one step)

Page 21: Procedures/functions of rdbms

Dropping a procedure

DROP PROCEDURE

schema

Procedure

ExampleDROP PROCEDURE fire_employee;

Marks dependent objects for recompilation

Page 22: Procedures/functions of rdbms

Privileges for proceduresΟ Example

Ο Procedure executes under the authority of owner, not user executing procedure

Ο User of procedure need not have access to objects inside procedure

Ο Can only GRANT privileges on an entire package, not a procedure, function,or variable defined in the package

GRANT EXECUTE ON scott.hire_fire TO mkennedy WITH GRANT OPTION;

Page 23: Procedures/functions of rdbms

Privileges for proceduresPROCEDURE system privileges apply to procedures, functions, and packages

To do this Need either And CREATE CREATE

PROCEDURE or CREATE ANY PROCEDURE system privilege

Owner must have access to all objects referenced in the procedure

ALTER Own the procedure or ALTER ANY PROCEDURE system privilege

DROP Own the procedure or DROP ANY PROCEDURE system privilege

Page 24: Procedures/functions of rdbms

Privileges for procedures

To do this Need either And

Execute aprocedure or accessa package construct

Own the procedureor be grantedEXECUTEPRIVILEGE orEXECUTE ANYPROCEDUREsystem privilege

Procedure ownermust be explicitlygranted access to alldatabase objects inthe procedure(notthrough roles)

Page 25: Procedures/functions of rdbms

Benefits of ProceduresΟ Security

Ο Integrity

Ο Performance

• Executes under security domain of procedure’s owner• Provides controlled indirect access to database objects to non- privileged users

• Defines allowed operations on data

• Ensures related actions are performed together

• Reduces number of calls to thedatabase• Decreases network traffic• Pre-parses PL/SQL statements

Page 26: Procedures/functions of rdbms

Benefits of ProceduresΟ Memory savings

Ο Productivity

• Takes advantages of shared SQL• Requires only one copy of the code for multiple users

• Avoids redundant code for common procedures in multiple applications

• Reduces coding errors: no redundant code written

Page 27: Procedures/functions of rdbms

Benefits of ProceduresΟ Maintainability

Ο High availability

• Enables system wide changes with one update

• Makes testing easier: duplicate testing not needed

• Dependency tracked by ORACLE

• Allows changing procedured on-line while users execute previous version

Page 28: Procedures/functions of rdbms