Database Interaction with PL/SQL, User-defined Packages - How the above package works
(Page 5 of 5 )
I gave a lot of theory and a large example in the previous section. Let me explain the above package bit by bit. So, first of all consider the following bit:
TYPE t_emprec is RECORD
(
name emp.ename%TYPE,
salary emp.sal%type
);
I declared a RECORD type named ‘t_emprec’ within the package specification. That means it can be used in every sub-program within that package (the minimum consideration).
procedure dispEmp;
procedure dispEmp(p_deptno dept.deptno%type);
The above two are overloaded sub-programs. One sub-program has a single parameter and the other does not have any parameters. That’s the end of the package specification. Now, let us see about the package body.
procedure dispEmp as
cursor c_emp is
select ename, sal from emp;
r_emp t_emprec;
begin
open c_emp;
loop
fetch c_emp into r_emp;
exit when c_emp%notfound;
dbms_output.put_line(r_emp.name || ',' || r_emp.salary);
end loop;
close c_emp;
end;
The above procedure is very similar to the one given in the third section of the article. It just displays all employee names and salaries. The only difference is that it fetches the information into a record based variable of type ‘t_emprec’ which was declared in the package specification.
procedure dispEmp(p_deptno dept.deptno%type) as
cursor c_emp is
select ename, sal from emp
where deptno = p_deptno;
r_emp t_emprec;
begin
open c_emp;
loop
fetch c_emp into r_emp;
exit when c_emp%notfound;
dbms_output.put_line (r_emp.name || ',' || r_emp.salary);
end loop;
close c_emp;
end;
I hope you can understand the above sub-program as it is quite similar except in the case of parameter declaration.
Other extensions to package development
One more point to understand is that you can also have some declarations within the package body straight away. That means you can have the declarations at the top of package body (without having any relation to the sub-programs). Once you have such declarations, those are called private declarations within the package. That means you can use all of those declarations in each of the sub-programs available.
Such types of package body level variable declarations are generally used to implement security within the package context (hiding it from outside world).
Even though I didn’t use any functions in any of the above packages, it doesn’t mean that you shouldn’t use them. You can use them as freely as you wish. And another great feature is that you can call another sub-program within the same sub-program without specifying the package name. Of course you are also allowed to call the sub-programs which are outside the package as well.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |