Deleting and retrieving values using PL/SQL stored procedures - Oracle
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.
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.empno%type) as Invalid_empno exception; begin delete from emp where empno=p_empno; if sql%notfound or sql%rowcount=0 then rollback; raise Invalid_empno; end if; commit; exception when invalid_empno then raise_application_error(-20001, 'Employee does not exist'); when others then raise_application_error(-20011, sqlerrm); end; /
You can understand that the above code is very similar to the “p_emp_update” code given in previous section. Now we shall proceed with retrieving information.
There exist several methods for retrieving information available within an Oracle database. If you would like to retrieve one or very few definite values, then it is advisable to proceed with OUT parameters. If you would like to retrieve an entire row (or set of rows), it is preferable to work with REF CURSORS. If you require more information about REF CURSORS, I have already contributed a separate article on REF CURSORS, which you can easily find on this web site.
Now, in this scenario, I shall simply proceed with retrieving a set of values using OUTPUT parameters.
create or replace procedure p_emp_details(p_empno emp.empno%type, p_ename OUT emp.ename%type, p_sal OUT emp.sal%type, p_deptno OUT emp.deptno%type) as begin select ename, sal, deptno into p_ename, p_sal, p_deptno from emp where empno = p_empno; exception when no_data_found then raise_application_error(-20001, 'Employee not found'); when too_many_rows then /* this would not happen generally */ raise_application_error(-20002, 'More employees exist with the same number'); when others then raise_application_error(-20003, SQLERRM); end; /
In the above stored procedure, you can observe that all the parameters (except p_empno) are OUTPUT parameters. They are mainly used to push the data OUT of the stored procedure to the calling program. To execute the above stored procedure, you may have to use a separate script (or PL/SQL block) as follows:
declare s emp.sal%type; en emp.ename%type; d emp.deptno%type; begin p_emp_details(7369, en,s,d); dbms_output.put_line('name of employee: ' || en); dbms_output.put_line('Salary: ' || s); dbms_output.put_line('Deptno: '||d); end; /
The above script simply declares a few variables to hold the values returned by the stored procedure.
My next article will look into the aspects of retrieving multiple rows using REF CURSOR, packages, helper routines etc. My further upcoming articles will deal with DAL (Data Access Layer) using ODP.NET together with ASP.NET to access these stored procedures (for www.aspfree.com). I hope you enjoyed this article and any comments, suggestions, feedback, bugs, errors, enhancements etc. are highly appreciated at http://jagchat.spaces.live.com