Using a cursor in another procedure of same package - plsql

Can we use a cursor declared in one procedure of the package in another procedure of the same package?

No, like anything declared inside a procedure it is local to that procedure. However you can declare a cursor at the package level and use it in both procedures.
package body my_pkg is
cursor emp_cur is select * from emp;
procedure p1 is
begin
open emp_cur;
...
close emp_cur;
end p1;
procedure p2 is
begin
open emp_cur;
...
close emp_cur;
end p2;
end;
Note though that if procedure p1 opens emp_cur but doesn't close it, then if p2 tries to open it it will get an exception.

Related

Procedure PL/SQL without parameters

I want to create a procedure without parameters (In SQL DEVELOPER) but I am not sure how to do it, I have tried it in the following way and it sends me an error in "Num1 NUMBER";
create or replace package PK_MAC as
PROCEDURE PR_PRUEBAS
IS
Num1 NUMBER;
BEGIN
Num1 := 2;
end;
end;
You're trying to create a procedure or a package, with a procedure?
Here is a working example of what you're doing, as a package.
Your package will have two parts, a SPEC and a BODY.
The SPEC will publicly share the definition of the procedure and the variable, NUM1.
The BODY will define what the procedure actually does. Since NUM1 is defined already in the context of the package in the spec, I can use it in my procedure in the body.
create or replace package PK_MAC as
num1 integer;
PROCEDURE PR_PRUEBAS;
end;
/
create or replace package body PK_MAC IS
procedure pr_pruebas is
BEGIN
Num1 := 2;
end pr_pruebas;
end PK_MAC;
/

How to Call Procedure and Output Results in a Grid

I would like to execute the following procedure using TOAD. I would like the results of the procedure call to be output into a grid within the TOAD UI. The procedure itself is inside a package named MyPackage. The package creates a type called ct.
Type:
TYPE ct IS REF CURSOR;
Procedure Code:
PROCEDURE GetFailedTransactions (p_fails OUT ct)
IS
BEGIN
OPEN p_fails FOR SELECT *
FROM MDC_FAILURE
WHERE SUCCESS_DT IS NULL;
END;
The :my_out_cursor will prompt for the variables dialog box. Set the type of the variable to cursor and the direction to OUT. Then the output will appear in the grid section.
BEGIN
MyPackage.GetFailedTransactions(:my_out_cursor);
END;

Is it possible to run plsql package under hplsql command using hplsql -f package.sql

I am trying to run a PL/SQL package which contains a procedure with cursor (with parameter) and also procedure with 3 parameters will it work fine in hplsql.
Something like:
create package name as
procedure proc( with 3 input parameters )
end package;
create or replace package body as
variables declaration like variable -schema name .table name %type
cursor cursor name(4 parameters)
select variables
from
columns
procedure proc(parameters)
open cursor
fetch into
close
end proc
end package

creating a dictionary-like collection of values in an oracle package spec

Let's say I have a package spec. that stores constant values, which are extensively used in other packages (their procedures mainly) - so stuff like custom error messages, specific values, even lists like
TYPE myTableType IS TABLE OF VARCHAR2(100);
myObj mytype := NEW myTableType ('value1','value2')
I'd however now like to add a whole "dictionary" structure to it now - so a list of key-> value pairs in that package.
Seems I cannot initialise a table of records in that section, as already asked here:
https://asktom.oracle.com/pls/asktom/f?p=100:11%3A0%3A%3A%3A%3AP11_QUESTION_ID:14334298866128
and it seems a table of objects mentioned in the link is not a solution for me either. Not sure what would be a best workaround of some sort.
Any ideas ?
Given a package specification
CREATE OR REPLACE PACKAGE example IS
TYPE assoc_array_type IS TABLE OF VARCHAR2(100) INDEX BY VARCHAR2(100);
g_const_array assoc_array_type;
PROCEDURE dummy_proc (i_var NUMBER);
END example;
/
You can initialize the associate array of constants as follows
CREATE OR REPLACE PACKAGE BODY example IS
PROCEDURE dummy_proc (i_var NUMBER) IS
BEGIN
dbms_output.put_line(g_const_array('key_1'));
END dummy_proc;
BEGIN
g_const_array('key_1') := 'value_1';
g_const_array('key_2') := 'value_2';
g_const_array('key_3') := 'value_3';
END example;
/
To test, compile spec and body and then call dummy_proc, which will print value_1 to the console. Associative arrays can also be indexed by binary_integer or pls_integer for different needs.
EDIT: This second package shows that you can reference the public array and get the values from initialization in other packages.
CREATE OR REPLACE PACKAGE example_2 IS
PROCEDURE dummy_proc (i_key VARCHAR2);
END example_2;
/
CREATE OR REPLACE PACKAGE BODY example_2 IS
PROCEDURE dummy_proc (i_key VARCHAR2) IS
BEGIN
dbms_output.put_line(example.g_const_array(i_key));
END dummy_proc;
END example_2;
/
BEGIN
example_2.dummy_proc('key_1');
END;
/

How do I use “separate" keyword

I am unable to figure out keyword separate in Ada and its depth concept. Please help me to understand by giving a small example?
Lets say I have a nested procedure
with ada.text_io; use ada.text_io;
procedure main is
procedure proc is
begin
put_line ("i am proc");
end proc;
begin
put_line ("main");
end main;
How to use separate keyword ?
You primarily use the separate keyword to achieve one of 2 effects.
OS specific actions. (Put 2 versions of the procedure / functions in different directories, and compile for 2 different targets)
Separation of a lengthy procedure from surrounding code.
Here is an example to show the syntax.
package_x.ads
package Package_X is
procedure Foo;
procedure Sep;
end Package_X;
package_x.adb
package body Package_X is
procedure Foo is
begin
null;
end Foo;
procedure Sep is separate;
end Package_X;
package_x-sep.adb
separate (Package_X) procedure Sep is
begin
null;
end Sep;
The separate keyword creates a unit of compilation, a subunit, that is compiled independently. The parameter of separate refers to the package in which the subunit is a sub unit of.
So if you had a package body X, then you remove procedure Y from it, you create a sub unit of X by creating a new file in which you place Y, and put "separate(X)" at the start of the file, to indicate that Y is really part of X.

Resources