• • • • •

Developing Simple PL/SQL Stored Procedures for CRUD Operations Validate information before inserting a row using a PL/SQL stored procedure: code Validate information before inserting a row using a PL/SQL stored procedure: explanation How to update a row in a table using a PL/SQL stored procedure Deleting and retrieving values using PL/SQL stored procedures

Summary: In this article, I shall go through a set of PL/SQL stored procedures which are very frequently used for CRUD operations. These stored procedures are mainly helpful for the developers who develop client applications (involving business logic or user interface design and programming) and who need a data layer to be implemented using PL/SQL stored procedures. The article is not targeted at pure PL/SQL developers.
Developing Simple PL/SQL Stored Procedures for CRUD Operations (Page 1 of 5 ) All the examples in this series have been tested only with Oracle 10g (V10.2). I didn’t really test any of the examples in any of the previous versions. If you have any problems during the execution of these examples, please post in the discussion area. How to insert a row into a table using a PL/SQL stored procedure The following is the stored procedure which deals with inserting a particular row into a table. create or replace procedure p_emp_insert (p_empno emp.empno%type, p_ename emp.ename%type, p_sal emp.sal%type, p_deptno emp.deptno% type) as begin insert into emp ( empno, ename, sal, deptno ) values ( p_empno, p_ename, p_sal, p_deptno ); Commit; exception when dup_val_on_index then raise_application_error(-20001, 'Employee already

The simplest one involves using constraints.deptno% type) as Invalid_sal exception.Validate information before inserting a row using a PL/SQL stored procedure: code (Page 2 of 5 ) In the previous stored procedure.ename%type. Database triggers are very similar to stored procedures. we didn’t do any validations for the information passed to it. The “others” exception is raised when the runtime come across any other errors (apart from “dup_val_on_index. end if. The following is the code available in the error handling (or exception handling) section. which mainly deals with errors/exceptions raised at run-time: when dup_val_on_index then raise_application_error(-20001. 3400. p_deptno emp. But validating data is the most important priority for any type of solution. you can issue the following command at the SQL prompt: SQL> EXEC p_emp_insert(1001. The SQLERRM is a built-in item which mainly contains the error message for the error or exception raised. As we are dealing with stored procedures. To execute the above stored procedure.” The “dup_val_on_index” is mainly raised when the runtime tries to insert a row with a repeated value in a unique or primary column. 'Employee already exists'). but they are automatically executed when the respective DML statement is issued on a table. when others then raise_application_error(-20011. The better option is to use database triggers. when others then raise_application_error(-20011.empno%type. 30) Developing Simple PL/SQL Stored Procedures for CRUD Operations . But constraints may not be suitable if we have any complex validations. Once all the parameters are passed in. / The above is a stored procedure named “p_emp_insert” which accepts four parameters. Validation of data can be achieved using several methods (at the database level). I mainly worked with two of the pre-defined exceptions. namely “dup_val_on_index” and “others. begin if p_sal<100 or p_sal>10000 then raise invalid_sal. it executes an INSERT statement to insert (or add) those values in the form of a row. Let us modify the previous stored procedure to handle a few validations as follows: create or replace procedure p_emp_insert (p_empno emp.” as it is already handled). p_sal emp. sqlerrm). sqlerrm). We can use database triggers to validate the information and to perform a few calculations as well. p_ename emp. I would like to deal with validations (or even calculations) using stored procedures. end. . Invalid_deptno exception.exists'). 'Jag'.sal%type. declare dummy_var varchar(10).

p_ename. when others then raise_application_error(-20011. 'Salary must be in between 100 and 10000'). Developing Simple PL/SQL Stored Procedures for CRUD Operations . 'Department doesn't exist'). / The next section will give you a complete explanation for the above stored procedure.begin select 'exists' into dummy_var from dept where deptno = p_deptno. 'Employee already exists'). Commit. “Invalid_Sal” and “Invalid_Deptno” are two user-defined exceptions which can be raised within our logic according to our requirements. sal. exception when invalid_sal then raise_application_error(-20001. deptno ) values ( p_empno. when invalid_deptno then raise_application_error(-20001. sqlerrm). when others then raise_application_error(-20011. exception. insert into emp ( empno. Let me start with the following: Invalid_sal Invalid_deptno exception. when dup_val_on_index then raise_application_error(-20001.Validate information before inserting a row using a PL/SQL stored procedure: explanation (Page 3 of 5 ) This section explains the code provided in the previous section. p_deptno ). ename. p_sal. end. end. exception when no_data_found then raise Invalid_deptno. sqlerrm). The following is the code fragment which checks for the .

it raises the user-defined exception “Invalid_sal. Coming to the department validation.deptno% type) as Invalid_sal exception. If the “Invalid_deptno” exception is raised.ename%type. begin select 'exists' into dummy_var from dept where deptno = p_deptno. “no_data_found” gets raised.sal%type. the SELECT statement fails. . If any other error occurs.empno%type. Developing Simple PL/SQL Stored Procedures for CRUD Operations . The following is the PL/SQL block which is nested into the main block to handle the same: declare dummy_var varchar(10).validity of salary: if p_sal<100 or p_sal>10000 then raise invalid_sal. The above checks to see whether the department exists or not. which in turn fires the “invalid_deptno” exception. end. if the salary is not in between 100 and 10000. 'Salary must be in between 100 and 10000'). p_deptno emp. p_sal emp.” Once it is raised. If it fails. exception when no_data_found then raise Invalid_deptno.How to update a row in a table using a PL/SQL stored procedure (Page 4 of 5 ) The following is the stored procedure which can be used to modify the information belonging to an employee: create or replace procedure p_emp_update (p_empno emp. when others then raise_application_error(-20011. control (or flow of execution) jumps to the following: when invalid_deptno then raise_application_error(-20001. 'Department doesn''t exist'). If it is not available. Invalid_empno exception. Invalid_deptno exception. and gets the following to be executed: when invalid_sal then raise_application_error(-20001. p_ename emp. end if. the control (or flow of execution) jumps to the exception section. The above statement returns a user-defined message to the application which called the stored procedure. it simply raises a default error. I would like to raise an exception and send a message back to the user. If it doesn’t exist. I need to check whether the given department number exists in the “dept” table or not. sqlerrm). In the above code fragment.

if sql%notfound or sql%rowcount=0 then rollback. the following is the code fragment which needs some attention: if sql%notfound or sql%rowcount=0 then rollback. end if. 'Department doesn't exist'). end. declare dummy_var varchar(10). exception when invalid_sal then raise_application_error(-20001. The above IF condition evaluates whether the previous DML statement (UPDATE in this scenario) updated any information successfully or not. sal=p_sal. update emp set ename=p_ename. . deptno=p_deptno where empno=p_empno. 'Employee does not exist').begin if p_sal<100 or p_sal>10000 then raise invalid_sal. sqlerrm). end if. exception when no_data_found then raise Invalid_deptno. raise Invalid_empno. sqlerrm). when others then raise_application_error(-20011. begin select 'exists' into dummy_var from dept where deptno = p_deptno. when invalid_empno then raise_application_error(-20001. commit. when others then raise_application_error(-20011. 'Salary must be in between 100 and 10000'). end. when invalid_deptno then raise_application_error(-20001. raise Invalid_empno. / In the above stored procedure. end if.

'Employee does not exist').empno%type. I shall simply proceed with retrieving a set of values using OUTPUT parameters.ename%type. if sql%notfound or sql%rowcount=0 then rollback. end. Now. commit.sal%type. 'More employees exist . Now we shall proceed with retrieving information. begin delete from emp where empno=p_empno.empno%type) as Invalid_empno exception. If you require more information about REF CURSORS. when too_many_rows then /* this would not happen generally */ raise_application_error(-20002. p_ename OUT emp. p_deptno from emp where empno = p_empno. p_sal OUT emp. sqlerrm). then it is advisable to proceed with OUT parameters. p_sal. end if.Deleting and retrieving values using PL/SQL stored procedures (Page 5 of 5 ) The following is the code which simply deletes a row from the table based on the employee number sent to it: create or replace procedure p_emp_delete (p_empno emp. There exist several methods for retrieving information available within an Oracle database.deptno%type) as begin select ename. If you would like to retrieve one or very few definite values. 'Employee not found'). exception when invalid_empno then raise_application_error(-20001. I have already contributed a separate article on REF CURSORS. in this scenario. raise Invalid_empno. when others then raise_application_error(-20011. deptno into p_ename. which you can easily find on this web site. / You can understand that the above code is very similar to the “p_emp_update” code given in previous section. sal. create or replace procedure p_emp_details(p_empno emp.Developing Simple PL/SQL Stored Procedures for CRUD Operations . p_deptno OUT emp. exception when no_data_found then raise_application_error(-20001. If you would like to retrieve an entire row (or set of rows). it is preferable to work with REF CURSORS.

end. begin p_emp_details(7369. en emp.s. when others then raise_application_error(-20003.deptno%type.put_line('Salary: ' || s). / The above script simply declares a few variables to hold the values returned by the stored procedure. To execute the above stored procedure. SQLERRM). en. dbms_output.d).sal%type. / In the above stored procedure. end. dbms_output. d emp. dbms_output. . They are mainly used to push the data OUT of the stored procedure to the calling program.ename%type.put_line('name of employee: ' || en).put_line('Deptno: '||d).with the same number'). you can observe that all the parameters (except p_empno) are OUTPUT parameters. you may have to use a separate script (or PL/SQL block) as follows: declare s emp.

Sign up to vote on this title
UsefulNot useful