how to daclare queue data-structure in oracle? - oracle

is There Any Way to Declare a Queue Data Structure Which be Accessible Globally in Oracle or in a Specific Schema?
With Globally i Mean be Accessible in All Procedures of Database or Specific Schema
edited :
i find out i can make FIFO QUEUE by package. so i wrote a code but i get error. any one know why i get this error?
package spec:
CREATE OR REPLACE PACKAGE HR.TEST_PACKAGE IS
PROCEDURE ADDQ (TABLE_NAME VARCHAR2);
FUNCTION DELQ RETURN VARCHAR2;
END TEST_PACKAGE;
/
package body:
CREATE OR REPLACE PACKAGE BODY HR.TEST_PACKAGE IS
REAR NUMBER := 0;
FRONT NUMBER := 0;
TYPE QUEUE_TYPE IS VARRAY(10) OF VARCHAR2(15);
QUEUE_ARRAY QUEUE_TYPE;
PROCEDURE ADDQ (TABLE_NAME VARCHAR2) IS
BEGIN
REAR := (REAR+1) MOD 10;
IF (REAR = 0) THEN
REAR := 10;
END IF;
QUEUE_ARRAY(REAR) := TABLE_NAME;
END ADDQ;
FUNCTION DELQ RETURN VARCHAR2 IS
BEGIN
IF (FRONT = REAR) THEN
RETURN 'EMPTY';
ELSE
FRONT := (FRONT + 1) MOD 10;
IF (FRONT = 0) THEN
front := 10;
END IF;
RETURN QUEUE_ARRAY(FRONT);
END IF;
END DELQ;
END TEST_PACKAGE;
/
when i run this example code:
create or replace procedure hr.test is
begin
hr.test_package.addq('a');
hr.test_package.addq('b');
dbms_output.put_line(hr.test_package.delq);
end test;
i get this error:
ORA-06531: Reference to uninitialized collection
06531. 00000 - "Reference to uninitialized collection"
*Cause: An element or member function of a nested table or varray
was referenced (where an initialized collection is needed)
without the collection having been initialized.
*Action: Initialize the collection with an appropriate constructor
or whole-object assignment.
and this error occur when wants to run this part of a code:
QUEUE_ARRAY(REAR) := TABLE_NAME;

Problem Solved. following code will declare a FIFO QUEUE in oracle.
the addq procedure will insert in queue and delq function will delete and return a value from queue.
package spec:
CREATE OR REPLACE PACKAGE HR.TEST_PACKAGE IS
PROCEDURE ADDQ (TABLE_NAME VARCHAR2);
FUNCTION DELQ RETURN VARCHAR2;
END TEST_PACKAGE;
/
package body:
CREATE OR REPLACE PACKAGE BODY HR.TEST_PACKAGE IS
REAR NUMBER := 0;
FRONT NUMBER := 0;
TYPE QUEUE_TYPE IS VARRAY(10) OF VARCHAR2(15);
QUEUE_ARRAY QUEUE_TYPE := QUEUE_TYPE() ;
PROCEDURE ADDQ (TABLE_NAME VARCHAR2) IS
BEGIN
REAR := (REAR+1) MOD 10;
IF (REAR = 0) THEN
REAR := 10;
END IF;
QUEUE_ARRAY.EXTEND;
QUEUE_ARRAY(REAR) := TABLE_NAME;
END ADDQ;
FUNCTION DELQ RETURN VARCHAR2 IS
BEGIN
IF (FRONT = REAR) THEN
RETURN 'EMPTY';
ELSE
FRONT := (FRONT + 1) MOD 10;
IF (FRONT = 0) THEN
front := 10;
END IF;
RETURN QUEUE_ARRAY(FRONT);
END IF;
END DELQ;
END TEST_PACKAGE;
/

Related

push procedure pop function with sequence number in oracle

I want to write code for push with procedure and pop with Function .
create or replace package pushpop_demo as
procedure push(val varchar2);
function pop return varchar2;
end pushpop_demo;
create or replace package body pushpop_demo as
subtype my_string_subtype is varchar2(100);
type varchar2_ntt is table of my_string_subtype;
stuff varchar2_ntt := varchar2_ntt();
procedure push(val varchar2)
is
begin
stuff.extend;
stuff(stuff.last) := val;
end push;
function pop return varchar2
is
subtype my_string_subtype varchar2(100);
begin
if stuff is not empty then
val := stuff(stuff.last);
stuff.delete(stuff.last);
end if;
return val;
end pop ;
but I get error , my question is how can I do this problem with sequence number ??? or other solution .my code does not run anyway . also I do not want use the package .just with procedure and Function .please help me
You need / statement terminators to terminate each PL/SQL block.
You are missing an END; statement at the end of the package body.
You have not declared the VAL variable in the function.
You do not want to re-declare the my_string_subtype type (as it will shadow sub-type declared in the package body).
You can initialise the collection in the declaration section or an alternative is to initialise it in the BEGIN ... END section of the package body.
create or replace package pushpop_demo as
procedure push(val varchar2);
function pop return varchar2;
end pushpop_demo;
/
create or replace package body pushpop_demo as
subtype my_string_subtype is varchar2(100);
type varchar2_ntt is table of my_string_subtype;
stuff varchar2_ntt; -- := varchar2_ntt();
procedure push(val varchar2)
is
begin
stuff.extend;
stuff(stuff.last) := val;
end push;
function pop return varchar2
is
val my_string_subtype;
begin
if stuff is not empty then
val := stuff(stuff.last);
stuff.delete(stuff.last);
end if;
return val;
end pop;
BEGIN
stuff := varchar2_ntt();
END;
/
Then you can use:
BEGIN
pushpop_demo.push('World');
pushpop_demo.push('Hello');
END;
/
and then:
BEGIN
DBMS_OUTPUT.PUT_LINE(pushpop_demo.pop());
DBMS_OUTPUT.PUT_LINE(pushpop_demo.pop());
END;
/
Outputs:
Hello
World
fiddle

Oracle Reference to uninitialized composite while passing object as output parameter PL/SQL

I have a problem with "ORA-06530: Reference to uninitialized composite". The object is as far as I understand initialiazed.
CREATE OR REPLACE TYPE t_RetVal AS OBJECT
(
var_n_retVal NUMBER,
var_n_sqlcode NUMBER,
var_v_sqlerrm VARCHAR(255),
CONSTRUCTOR FUNCTION t_RetVal(SELF IN OUT NOCOPY t_RetVal) RETURN SELF AS RESULT
);
/
CREATE OR REPLACE TYPE BODY t_RetVal AS
CONSTRUCTOR FUNCTION t_RetVal(SELF IN OUT NOCOPY t_RetVal) RETURN SELF AS RESULT IS
BEGIN
SELF.var_n_retVal := 1;
SELF.var_n_sqlcode := 0;
SELF.var_v_sqlerrm := '';
RETURN;
END;
END;
create or replace FUNCTION get_Something(
id IN NUMBER,
out_t_RetVal OUT NOCOPY t_RetVal
)
RETURN NUMBER
IS
retVal NUMBER := 1;
BEGIN
out_t_RetVal.var_n_retVal:= 1;
out_t_RetVal.var_n_sqlcode := 0;
out_t_RetVal.var_v_sqlerrm := '';
RETURN out_t_RetVal;
END;
In the procedure i do the following and keep getting "ORA-06530: Reference to uninitialized composite".
create or replace procedure PROC is
var_t_SubFuncRetVal t_RetVal;
v_n_retVal number := 0;
v_error_text varchar2(1000);
BEGIN /*PROCEDURE START*/
var_t_SubFuncRetVal := t_RetVal();
v_n_retVal := get_Something( id, var_t_SubFuncRetVal);
IF (var_t_SubFuncRetVal.var_n_retVal >= 0) THEN
IF (var_t_SubFuncRetVal.var_n_retVal = 0) THEN
....
ELSE
....
END IF;
ELSE
v_error_text := 'Error message is '|| 'SQLCODE: '||var_t_SubFuncRetVal.var_n_sqlcode||' -- ERROR: '||var_t_SubFuncRetVal.var_v_sqlerrm;
END IF;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END PROC;
Why is Oracle reporting this error ? How to properly pass object as out parameter ?

Wrong number or TYPES of arguments, error in PL/SQL

I have to create a list of RECORD and I need to send it to a procedure.
There is my header.
CREATE OR REPLACE PACKAGE tema4 IS
TYPE obj IS RECORD(id INTEGER := 0,percent INTEGER := 0);
TYPE listObj IS TABLE OF obj INDEX BY PLS_INTEGER;
PROCEDURE ex1 (p_listObj IN listObj);
END tema4;
My body.
create or replace PACKAGE BODY tema4 IS
PROCEDURE ex1 (p_listObj IN listObj) IS
BEGIN
DBMS_OUTPUT.PUT_LINE('Cant reach');
END ex1;
END tema4;
And my code that calls procedure ex1.
DECLARE
TYPE obj IS RECORD(id INTEGER := 0,percent INTEGER := 0);
TYPE listObj IS TABLE OF obj INDEX BY PLS_INTEGER;
v_obj obj;
v_listObj listObj;
BEGIN
FOR v_i IN (SELECT ID,BURSA FROM STUDENTI ORDER BY ID) LOOP
v_obj.id := v_i.id;
v_obj.percent := 50;
v_listObj(v_i.id) := v_obj;
END LOOP;
FOR v_i IN v_listObj.FIRST..v_listObj.LAST LOOP
DBMS_OUTPUT.PUT_LINE(v_listObj(v_i).id || ' - ' ||
v_listObj(v_i).percent);
END LOOP;
tema4.ex1(v_listObj); --this line is with problems
END;
PLS-00306: wrong number or types of arguments in call to 'EX1'
Can someone explain me what is wrong in my code? I also tried to create my type as global, but it won't let me because of 'RECORD' keyword.
Don't declare the types again (new types), use the types already declared in the package spec:
DECLARE
v_obj tema4.obj;
v_listObj tema4.listObj;
BEGIN
FOR v_i IN (SELECT ID,BURSA FROM STUDENTI ORDER BY ID) LOOP
v_obj.id := v_i.id;
v_obj.percent := 50;
v_listObj(v_i.id) := v_obj;
END LOOP;
FOR v_i IN v_listObj.FIRST..v_listObj.LAST LOOP
DBMS_OUTPUT.PUT_LINE(v_listObj(v_i).id || ' - ' ||
v_listObj(v_i).percent);
END LOOP;
tema4.ex1(v_listObj); --this line is with problems
END;

'Reference to uninitialized composite' while trying to execute a stored procedure in Oracle with UDTs

There is this package and I am trying to execute using the code below. However I get an error
ORA-06530: Reference to uninitialized composite
Code:
DECLARE
StudyNum InputTyp := InputTyp ();
StudyDetails OutputTyp := OutputTyp ();
BEGIN
StudyNum.EXTEND;
StudyNum (1) := '9071';
my_package.my_procedure(StudyNum, StudyDetails);
END;
/
The package is created as below with user-defined datatypes as input & output params:
Create OR REPLACE Type InputTyp AS VARRAY(200) OF VARCHAR2 (1000);
CREATE TYPE OBJTYP AS OBJECT
(
A NUMBER,
B VARCHAR2 (1000),
C VARCHAR2 (100)
);
CREATE TYPE OutputTyp IS VARRAY (2000) OF OBJTYP;
/
CREATE OR REPLACE PACKAGE my_package
AS
PROCEDURE my_procedure(p_StudyNum IN InputTyp,p_StdyDtl OUT OutputTyp);
END my_package;
/
CREATE OR REPLACE PACKAGE BODY my_package
AS
PROCEDURE my_procedure(p_StudyNum IN InputTyp,p_StdyDtl OUT OutputTyp)
IS
i BINARY_INTEGER := 1;
j BINARY_INTEGER := 1;
CURSOR c_StudyTbl
IS
SELECT A, B, C
FROM my_table
WHERE Study_Number = p_StudyNum(i);
v_StudyTbl OBJTYP;
BEGIN
p_StdyDtl := OutputTyp ();
LOOP
-- This is the first cursor opened for each of the items in the list.
EXIT WHEN i > p_StudyNum.count;
OPEN c_StudyTbl;
LOOP
FETCH c_StudyTbl INTO v_StudyTbl;
EXIT WHEN c_StudyTbl%NOTFOUND;
p_StdyDtl.EXTEND ();
p_StdyDtl (j).A := v_StudyTbl.A;
p_StdyDtl (j).B := v_StudyTbl.B;
p_StdyDtl (j).C := v_StudyTbl.C;
j := j + 1;
END LOOP;
CLOSE c_StudyTbl;
i := i + 1;
END LOOP;
IF c_StudyTbl%ISOPEN
THEN
CLOSE c_StudyTbl;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND
THEN
NULL;
END;
END my_package;
/
you would either need to do :
p_StdyDtl(p_StdyDtl.last) := OBJTYP(null, null, null);
p_StdyDtl (j).A := v_StudyTbl.A;
p_StdyDtl (j).B := v_StudyTbl.B;
p_StdyDtl (j).C := v_StudyTbl.C;
or simpler:
p_StdyDtl(j) := OBJTYP(v_StudyTbl.A, v_StudyTbl.B, v_StudyTbl.C);
your code as-is fails because you've initialised the OutputTyp but not the objtyp part.
but as I said in the prior question of yours, the multiset avoids all of that.

array or list into Oracle using cfprocparam

I have a list of values I want to insert into a table via a stored procedure.
I figured I would pass an array to oracle and loop through the array but I don't see how to pass an array into Oracle. I'd pass a list but I don't see how to work with the list to turn it into an array using PL/SQL (I'm fairly new to PL/SQL). Am I approaching this the wrong way?
Using Oracle 9i and CF8.
EDIT
Perhaps I'm thinking about this the wrong way? I'm sure I'm not doing anything new here...
I figured I'd convert the list to an associative array then loop the array because Oracle doesn't seem to work well with lists (in my limited observation).
I'm trying to add a product, then add records for the management team.
-- product table
productName = 'foo'
productDescription = 'bar'
...
...
etc
-- The managementteam table just has the id of the product and id of the users selected from a drop down.
The user IDs are passed in via a list like "1,3,6,20"
How should I go about adding the records to the management team table?
Here is where I am code wise
In theory I pass a list "1,2,3,4" to inserts.addProduct.
inserts.addProduct should call tools.listToArray and return an array.
inserts.addProduct recreates a list with a * delim as a test.
CREATE OR REPLACE PACKAGE tools AS
TYPE array_type is TABLE OF VARCHAR2(225) INDEX BY BINARY_INTEGER;
FUNCTION listToArray(in_list IN VARCHAR,
in_delim IN VARCHAR2 DEFAULT ',')
RETURN array_type;
END tools;
CREATE OR REPLACE PACKAGE BODY tools
AS
FUNCTION listToArray(in_list IN VARCHAR,
in_delim IN VARCHAR2 DEFAULT ',')
RETURN array_type
IS
l_token_count BINARY_INTEGER := 0;
-- l_token_tbl type_array;
i pls_integer;
l_start_pos INTEGER := 1;
l_end_pos INTEGER :=1;
p_parsed_table array_type;
BEGIN -- original work by John Spencer
WHILE l_end_pos <> 0 LOOP
l_end_pos := instr(in_list,in_delim,l_start_pos);
IF l_end_pos <> 0 THEN
l_token_count := l_token_count + 1;
p_parsed_table(l_token_count ) :=
substr(in_list,l_start_pos,l_end_pos - l_start_pos);
l_start_pos := l_end_pos + 1;
END IF;
END LOOP;
IF l_token_count = 0 THEN /* We haven't parsed anything so */
l_token_count := 1;
p_parsed_table(l_token_count) := in_list;
ELSE /* We need to get the last token */
l_token_count := l_token_count + 1;
p_parsed_table(l_token_count) := substr(in_list,l_start_pos);
END If;
RETURN p_parsed_table;
END listToArray; -- Procedure
END tools;
CREATE OR REPLACE PACKAGE inserts AS
TYPE array_type is TABLE OF VARCHAR2(225) INDEX BY BINARY_INTEGER;
PROCEDURE addProduct (inList IN VARCHAR2,
outList OUT VARCHAR2
);
END inserts;
CREATE OR REPLACE PACKAGE BODY inserts
AS
PROCEDURE addProduct (inList IN VARCHAR2,
outList OUT VARCHAR2
)
IS
i NUMBER;
localArray array_type := tools.listToArray(inList);
BEGIN
outList := '';
FOR i IN localArray.first .. localArray.last LOOP
outList := outList || '*' ||localArray(i); -- return a string just to test this mess
END LOOP;
END addProduct;
END inserts;
I'm currently getting an error "PLS-00382: expression is of wrong type" on localArray array_type := tools.listToArray(inList);
final working code (thanks so much!)
-- create sql type collection
CREATE OR REPLACE TYPE array_type is TABLE OF VARCHAR2(225);
/
CREATE OR REPLACE PACKAGE tools AS
FUNCTION listToArray(in_list IN VARCHAR,
in_delim IN VARCHAR2 DEFAULT ',')
RETURN array_type;
END tools;
/
CREATE OR REPLACE PACKAGE BODY tools
AS
FUNCTION listToArray(in_list IN VARCHAR,
in_delim IN VARCHAR2 DEFAULT ',')
RETURN array_type
IS
l_token_count BINARY_INTEGER := 0;
i pls_integer;
l_start_pos INTEGER := 1;
l_end_pos INTEGER :=1;
p_parsed_table array_type := array_type();
BEGIN
WHILE l_end_pos <> 0 LOOP
l_end_pos := instr(in_list,in_delim,l_start_pos);
IF l_end_pos <> 0 THEN
p_parsed_table.extend(1);
l_token_count := l_token_count + 1;
p_parsed_table(l_token_count ) :=
substr(in_list,l_start_pos,l_end_pos - l_start_pos);
l_start_pos := l_end_pos + 1;
END IF;
END LOOP;
p_parsed_table.extend(1);
IF l_token_count = 0 THEN /* We haven't parsed anything so */
l_token_count := 1;
p_parsed_table(l_token_count) := in_list;
ELSE /* We need to get the last token */
l_token_count := l_token_count + 1;
p_parsed_table(l_token_count) := substr(in_list,l_start_pos);
END If;
RETURN p_parsed_table;
END listToArray; -- Procedure
END tools;
/
CREATE OR REPLACE PACKAGE inserts AS
PROCEDURE addProduct (inList IN VARCHAR2,
outList OUT VARCHAR2
);
END inserts;
/
CREATE OR REPLACE PACKAGE BODY inserts
AS
PROCEDURE addProduct (inList IN VARCHAR2,
outList OUT VARCHAR2
)
IS
i NUMBER;
mylist VARCHAR(100);
localArray array_type := array_type();
BEGIN
localArray := tools.listToArray(inList);
mylist := '';
FOR i IN localArray.first .. localArray.last LOOP
mylist := mylist || localArray(i) || '*';
END LOOP;
aList := mylist;
END addProduct;
END inserts;
/
PL/SQL has supported arrays since Oracle 8.0. They used to be called PL/SQL tables which confused the heck out of everybody, so now they are called collections. Find out more.
The problem is, that they are implemented as User-Defined Types (i.e. objects). My reading of the ColdFusion documents suggests that cfprocparam only supports the "primitive" datatypes (number, varchar2, etc). So UDTs are not supported.
I'm not sure what you mean by this:
I'd pass a list but I don't see how to
work with the list to turn it into an
array using PL/SQL
If you mean you want to pass a string of comma separated values ....
"Fox in socks, Mr Knox, Sam-I-Am, The Lorax"
then I have a workaround for you. Oracle doesn't provide a built-in Tokenizer. But a long time ago John Spencer published a hand-rolled solution which works in Oracle 9i on the OTN forums. Find it here.
edit
but... Oracle hates me
Do not despair. The OTN forums have been upgraded a few times since John posted that , and the formatting seems to have corrupted the code somewhere along the way. There were a couple of compilation errors which it didn't use to have.
I have rewritten John's code, including a new function. THe main difference is that the nested table is declared as a SQL type rather than a PL/SQL type.
create or replace type tok_tbl as table of varchar2(225)
/
create or replace package parser is
function my_parse(
p_str_to_search in varchar2
, p_delimiter in varchar2 default ',')
return tok_tbl;
procedure my_parse(
p_str_to_search in varchar2
, p_delimiter in varchar2 default ','
, p_parsed_table out tok_tbl);
end parser;
/
As you can see, the function is just a wrapper to the procedure.
create or replace package body parser is
procedure my_parse ( p_str_to_search in varchar2
, p_delimiter in varchar2 default ','
, p_parsed_table out tok_tbl)
is
l_token_count binary_integer := 0;
l_token_tbl tok_tbl := tok_tbl();
i pls_integer;
l_start_pos integer := 1;
l_end_pos integer :=1;
begin
while l_end_pos != 0
loop
l_end_pos := instr(p_str_to_search,p_delimiter,l_start_pos);
if l_end_pos != 0 then
l_token_count := l_token_count + 1;
l_token_tbl.extend();
l_token_tbl(l_token_count ) :=
substr(p_str_to_search,l_start_pos,l_end_pos - l_start_pos);
l_start_pos := l_end_pos + 1;
end if;
end loop;
l_token_tbl.extend();
if l_token_count = 0 then /* we haven't parsed anything so */
l_token_count := 1;
l_token_tbl(l_token_count) := p_str_to_search;
else /* we need to get the last token */
l_token_count := l_token_count + 1;
l_token_tbl(l_token_count) := substr(p_str_to_search,l_start_pos);
end if;
p_parsed_table := l_token_tbl;
end my_parse;
function my_parse ( p_str_to_search in varchar2
, p_delimiter in varchar2 default ',')
return tok_tbl
is
rv tok_tbl;
begin
my_parse(p_str_to_search, p_delimiter, rv);
return rv;
end my_parse;
end parser;
/
The virtue of declaring the type in SQL is that we can use it in a FROM clause like this:
SQL> insert into t23
2 select trim(column_value)
3 from table(parser.my_parse('Fox in socks, Mr Knox, Sam-I-Am, The Lorax'))
4 /
4 rows created.
SQL> select * from t23
2 /
TXT
------------------------------------------------------------------------------
Fox in socks
Mr Knox
Sam-I-Am
The Lorax
SQL>

Resources