Package Components in Oracle with Examples
In this article, I am going to discuss Package Components in Oracle with Examples. Please read our previous article where we discussed How to Create Package Specification Without a Body in Oracle with Examples.
Package Components in Oracle
This topic is very important because we need to understand the rules of packages. Let’s learn this concept with a small example.
create or replace package p_test is c_var1 constant number:=10; c_var2 varchar2(100):='welcome'; procedure print; end; ------------ create or replace package body p_test is c_var3 varchar2(100):='hi there'; procedure print is c_var4 varchar2(100):='hi'; begin dbms_output.put_line('this variable came from package spec. '||c_var1); dbms_output.put_line('this variable came from package spec. '||c_var2); dbms_output.put_line('this variable came from package body. '||c_var3); dbms_output.put_line('this variable came from print Proc. '||c_var4); end; end;
Here from the above package and package body, we can see that we have variables like c_var1, c_var2, c_var3, and c_var4. We have created the package called p_test. We have also created a procedure called print.
The variables c_var1 and c_var2 are referred to as public variables or global variables. These variables can be referenced anywhere in the package body.
In the package body, we have defined the variables c_var3 and c_var4. The variable c_Var3 can be referenced anywhere in the package body. We have a procedure print and the variable c_var4 is declared inside the procedure, so this c_var4 can be referenced only inside the procedure print. Let’s go ahead and compile this package.
So, the package p_test is now compiled. We have declared the c_var1 with a constant number of 10 and c_var2 is declared as ‘welcome’. So, let’s go ahead and compile the package body.
So, the PL/SQL block inside the package body is just explained where the variable and where the variable exists. So, the package and package body are compiled. Let’s try to test the package.
We can see the procedure inside the package is executed successfully and then the DBMS_OUTPUT shows the values. This example shows us how the variables are used inside the packages.
In the next article, I am going to discuss Recompile Packages in Oracle with Examples. Here, in this article, I try to explain Package Components in Oracle with Examples. I hope you enjoy this Package Components in Oracle article.