Simple question aboout PL/SQL nested tables
Hello, my question is about nested tables. Suppose I have a nested table:
TYPE int_array IS TABLE OF INT;
v_int_array int_array;
Is there a built in function to allow me to search v_int_array for a specific integer value?
George
Use MEMBER OF operator:
declare
TYPE int_array IS TABLE OF INT;
v_int_array int_array := int_array(1,2,3,4,5);
begin
if 1 member of v_int_array
then
dbms_output.put_line('Found it!');
end if;
end;
Found it!
PL/SQL procedure successfully completed.
SQL> SY.
Similar Messages
-
Simple java class for SQL like table?
Dear Experts,
I'm hoping that the java people here at the Oracle forums will be able to help me.
I've worked with Oracle and SQL since the 90s.
Lately, I'm learning java and doing a little project in my spare time.
It's stand alone on the desktop.
The program does not connect to any database
(and a database is not an option).
Currently, I'm working on a module for AI and decision making.
I like the idea of a table in memory.
Table/data structure with Row and columns.
And the functionality of:
Select, insert, update, delete.
I've been looking at the AbstractTableModel.
Some of the best examples I've found online (they actually compile and work) are:
http://www.java2s.com/Code/Java/Swing-JFC/extendsAbstractTableModeltocreatecustommodel.htm
http://tutiez.com/simple-jtable-example-using-abstracttablemodel.html
Although they are rather confusing.
In all the examples I find, there always seems to be
at least three layers of objects:
Data object (full of get/set methods)
AbstractTableModel
GUI (JFrame, JTable) (GUI aspect I don't need or want)
In all the cases I've seen online, I have yet to see an example
that has the equivalent of Delete or Insert.
Just like in SQL, I want to define a table with columns.
Insert some rows. Update. Select. Delete.
Question:
Is there a better java class to work with?
Better, in terms of simpler.
And, being able to do all the basic SQL like functions.
Thanks a lot!Hi Timo,
Thanks. yes I had gone thru the java doc already and they have mentioned to use java.sql.Struct, but the code which got generated calls constructor of oracle.jpub.runtime.MutableStruct where it expects oracle.sql.STRUCT and not the java.sql.STRUCT and no other constructor available to call the new implementation and that is the reason i sought for some clues.
protected ORAData create(CmnAxnRecT o, Datum d, int sqlType) throws SQLException
if (d == null) return null;
if (o == null) o = new CmnAxnRecT();
o._struct = new MutableStruct((STRUCT) d, _sqlType, _factory);
return o;
here CmnAxnRecT is the class name of the generated java class for sqlType.
Thanks again. Please let me know if you have any more clues.
Regards,
Vinothgan AS -
Simple Question on & in SQL query
I am executing the following query from the SQL*Plus (my data in the table contains P&G as the value of the column for name)
select * from emp where name = 'P&G'
This rightly prompts me for the value of &g
Please let me know any solution other than the following
1. set define off
2. set define <to some other charecter>
3. Execute the query in some third party tools other than sql*plusIs there a way we can do set define off from a
procedure - meaning I want something to perform the
same way as set define off inside a procedureDo you mean a stored procedure?
Stored procedures (and/or packages) do not have a problem with the ampersand character. That's only an issue when you are using SQL*Plus. -
Using Nested Table in Select Statement
Hi all ,
Can i use the PL/SQL nested table or Varray
in the select statement as a normal table joined with other database tables.
i.e.
I have a nested table NT_1 in PL/SQL proc
i have to use this NT_1 in the select statement as
select xxx from
tab_1,
tab_2,
NT_1
where
< some conditional joins >.
Please help me in this regard.
regds
Dhananjaya.Hyou can not use a varray as part of a SQL Statement in order to build joins.
Can you explain better what do you want to do ?
Joel P�rez -
Is there any way of passing a PL/SQL nested table (IS TABLE OF xxx) when calling a procedure or package across a DB Link in 9i? It does not appear to be possible due to the limitation on use of user types.
ThanksI would be inclined to discourage you from using nested tables in almost any context. What is the business problem you are trying to solve?
Not coding problem ... business problem.
Perhaps we can suggest a better way to approach the problem. -
How to join PLSQL nested table with normal tables ?
my requirement is to delete the rows in a normal oracle table in one statement, based on the values collected in a PL/SQL nested table which is filled by another program.
In the following example, i need to delete the rows in
the table c_journals where the columns branch_code and
id are having values which exist in the PL/SQL
nested table's columns a and b respectively
declare
type test_type IS RECORD (a VARCHAR2(20), b number(20));
type test_table is table of test_type index by binary_integer;
test_object test_table;
cnt number;
begin
test_object(1).a := '1000';
test_object(1).b := 1;
test_object(2).a := '1006';
test_object(2).b := 4;
/* to be completed */
delete c_journals where ....
end;
Is this possible in Oracle ? Iam using the version 9iR2
Thanks in advanceBoth of the following delete statements should work. For the second the two types need to be created as database objects.
declare
type test_type IS RECORD (a VARCHAR2(20), b number(20));
type test_table is table of test_type index by binary_integer;
test_object test_table; -- := test_table(test_type('1000',1), test_type('1006',4));
cnt number;
begin
test_object(1).a := '1000';
test_object(1).b := 1;
test_object(2).a := '1006';
test_object(2).b := 4;
for i in test_object.first .. test_object.last loop
delete from c_journals cj
where test_object(i).a = cj.branch_code
and test_object(i).b = cj.id;
end loop;
end;
create or replace type test_type IS object (a VARCHAR2(20), b number(20));
create or replace type test_table is table of test_type;
declare
test_object test_table := test_table(test_type('1000',1), test_type('1006',4));
cnt number;
begin
delete from c_journals cj
where exists (select 'x' from table(cast(test_object as test_table)) t
where cj.branch_code = t.a and cj.id = t.b);
end;
/ -
Hi ,
Please assist as how can we do this thing
i have a nested table of object type
create or replace type a1 as object
a number,
b varchar2(30),
region varchar2(30)
create type a1_array s table of a1;
declare
v_a1 a1;
v_a1_array a1_array:=a1_array();
begin
v_a1= a1(1, '1' , 'AUS');
v_a1_array.EXTEND;
v_a1_array(1):=v_a1;
v_a1= a1(2, '2' , 'AUS');
v_a1_array.EXTEND;
v_a1_array(2):=v_a1;
v_a1= a1(3, '3' , 'NAM');
v_a1_array.EXTEND;
v_a1_array(3):=v_a1;
end;
Now, i have v_a1_array having 3 rows 2 with AUS region and one with NAM region.
Using SQL can i extract only 'AUS' rows and fetch in ARRAY OF TYPE v_a1_array (using Where clause and Table () functions )
Any help will be highly appreciated. Please assist. I have oracle 11g
ThanksHi,
GPU has already shown you how to do. I will just modify my original one:
SQL*Plus: Release 11.2.0.1.0 Production on Thu Aug 22 22:14:42 2013
Copyright (c) 1982, 2010, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
SQL> set serveroutput on
SQL>
SQL> DECLARE
2 v_a1 a1;
3 v_a1_array a1_array := a1_array ();
4 v_a1_array2 a1_array;
5 BEGIN
6 v_a1 := a1 (1, '1', 'AUS');
7 v_a1_array.EXTEND;
8 v_a1_array (1) := v_a1;
9 v_a1 := a1 (2, '2', 'AUS');
10 v_a1_array.EXTEND;
11 v_a1_array (2) := v_a1;
12 v_a1 := a1 (3, '3', 'NAM');
13 v_a1_array.EXTEND;
14 v_a1_array (3) := v_a1;
15
16 SELECT a1(a, b, region)
17 BULK COLLECT INTO v_a1_array2
18 FROM TABLE (v_a1_array)
19 WHERE region = 'AUS';
20
21 FOR c1 IN (SELECT *
22 FROM TABLE (v_a1_array2))
23 LOOP
24 DBMS_OUTPUT.put_line ('A='||c1.a||', B='||c1.b||', REGION='||c1.region);
25 END LOOP;
26 END;
27 /
A=1, B=1, REGION=AUS
A=2, B=2, REGION=AUS
PL/SQL procedure successfully completed.
If you consider your question answered, please mark this thread as answered.
Regards.
Alberto -
Using FOR .. LOOP counter in handling of PL/SQL procedures with nest. table
Hi all!
I'm learning PL/SQL on Steve Bobrovsky's book (specified below sample is from it) and I've a question.
In the procedure of specified below program used an integer variable currentElement to get reference to the row of nested table of %ROWTYPE datatype.
Meanwhile, the program itself uses a common FOR .. LOOP counter i.
DECLARE
TYPE partsTable IS TABLE OF parts%ROWTYPE;
tempParts partsTable := partsTable();
CURSOR selectedParts IS
SELECT * FROM parts ORDER BY id;
currentPart selectedParts%ROWTYPE;
currentElement INTEGER;
PROCEDURE printParts(p_title IN VARCHAR2, p_collection IN partsTable) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(' ');
DBMS_OUTPUT.PUT_LINE(p_title || ' elements: ' || p_collection.COUNT);
currentElement := p_collection.FIRST;
FOR i IN 1 .. p_collection.COUNT
LOOP
DBMS_OUTPUT.PUT('Element #' || currentElement || ' is ');
IF tempParts(currentElement).id IS NULL THEN DBMS_OUTPUT.PUT_LINE('an empty element.');
ELSE DBMS_OUTPUT.PUT_LINE('ID: ' || tempParts(currentElement).id || ' DESCRIPTION: ' || tempParts(currentElement).description);
END IF;
currentElement := p_collection.NEXT(currentElement);
END LOOP;
END printParts;
BEGIN
FOR currentPart IN selectedParts
LOOP
tempParts.EXTEND(2);
tempParts(tempParts.LAST) := currentPart;
END LOOP;
printParts('Densely populated', tempParts);
FOR i IN 1 .. tempParts.COUNT
LOOP
IF tempParts(i).id is NULL THEN tempParts.DELETE(i);
END IF;
END LOOP;
FOR i IN 1 .. 50
LOOP
DBMS_OUTPUT.PUT('-');
END LOOP;
printParts('Sparsely populated', tempParts);
END;
/When I've substituted an INTEGER global variable with such FOR .. LOOP counter, an APEX have returned an error "ORA-01403: no data found".
DECLARE
TYPE partsTable IS TABLE OF parts%ROWTYPE;
tempParts partsTable := partsTable();
CURSOR selectedParts IS
SELECT * FROM parts ORDER BY id;
currentPart selectedParts%ROWTYPE;
PROCEDURE printParts(p_title IN VARCHAR2, p_collection IN partsTable) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(' ');
DBMS_OUTPUT.PUT_LINE(p_title || ' elements: ' || p_collection.COUNT);
FOR i IN 1 .. p_collection.COUNT
LOOP
DBMS_OUTPUT.PUT('Element is ');
IF tempParts(i).id IS NULL THEN DBMS_OUTPUT.PUT_LINE('an empty element.');
ELSE DBMS_OUTPUT.PUT_LINE('ID: ' || tempParts(i).id || ' DESCRIPTION: ' || tempParts(i).description);
END IF;
END LOOP;
END printParts;
BEGIN
FOR currentPart IN selectedParts
LOOP
tempParts.EXTEND(2);
tempParts(tempParts.LAST) := currentPart;
END LOOP;
printParts('Densely populated', tempParts);
FOR i IN 1 .. tempParts.COUNT
LOOP
IF tempParts(i).id is NULL THEN tempParts.DELETE(i);
END IF;
END LOOP;
FOR i IN 1 .. 50
LOOP
DBMS_OUTPUT.PUT('-');
END LOOP;
printParts('Sparsely populated', tempParts);
END;
/When I've tried to handle this code in SQL*Plus, the following picture have appeared:
Densely populated elements: 10
Element is an empty element.
Element is ID: 1 DESCRIPTION: Fax Machine
Element is an empty element.
Element is ID: 2 DESCRIPTION: Copy Machine
Element is an empty element.
Element is ID: 3 DESCRIPTION: Laptop PC
Element is an empty element.
Element is ID: 4 DESCRIPTION: Desktop PC
Element is an empty element.
Element is ID: 5 DESCRIPTION: Scanner
Sparsely populated elements: 5
DECLARE
ERROR at line 1:
ORA-01403: no data found
ORA-06512: at line 14
ORA-06512: at line 35What's wrong in code(or what I have not understood)? Help please!942736 wrote:
What's wrong in code(or what I have not understood)? Help please!First code. You have collection of 10 elements:
1 - null
2 - populated
3 - null
4 - populated
5 - null
6 - populated
7 - null
8 - populated
9 - null
10 - populated
Then you delete null elements and have 5 element collection
2 - populated
4 - populated
6 - populated
8 - populated
10 - populated
Now you execute:
printParts('Sparsely populated', tempParts);Inside procedure you execute:
currentElement := p_collection.FIRST;
This assingns currentElement value 2. Then procedure loops 5 times (collection element count is 5). Element 2 exists. Inside loop procedure executes:
currentElement := p_collection.NEXT(currentElement);
which assigns currentElement values 4,6,8,10 - all existing elements.
Now second code. Everything is OK until you delete null elements. Again we have:
2 - populated
4 - populated
6 - populated
8 - populated
10 - populated
Again you execute:
printParts('Sparsely populated', tempParts);Now procedure loops 5 times (i values are 1,2,3,4,5):
FOR i IN 1 .. p_collection.COUNT
Very first iteration assingns i value 1. And since collection has no element with substript 1 procedure raises no data found.
SY. -
SQL Loader Constraints with Column Objects and Nested Tables
I am working on loading a Table that (god forbid) contains columns, column objects, and nested tables (which contains several depth of column objects). My question is does SQL Loader have a hidding undocumented feature where it states how the column objects must be grouped in refereneced to the nested tables within the loader file? I can load the various column objects, and nested tables fine right now, however, I am loading them all in strange and insane order. Can anyone answer this question? Thanks.
PeterI just noticed that my email is wrong. If you can help, plese send email to [email protected]
thanks. -
Sql*loader and nested tables
I'm having trouble loading a nested table via sqlldr in Oracle 10g and was hoping someone could point me in the right direction. I keep getting the following error:
SQL*Loader-403: Referenced column not present in table mynamespace.mytable
Here's an overview of my type and table definitions:
create type mynamespace.myinfo as object
i_name varchar2(64),
i_desc varchar2(255)
create TYPE mynamespace.myinfotbl as TABLE of mynamespace.myinfo;
create table mynamespace.mytable
Info mynamespace.myinfotbl,
note varchar2(255)
NESTED TABLE Info STORE AS mytable_nested_tab;
My control file looks like this:
load data
infile 'mydatafile.csv'
insert into table mynamespace.mytable
fields terminated by ',' optionally enclosed by '"'
TRAILING NULLCOLS
Info nested table count(6)
Info column object
i_name char(64),
i_desc char(255)
note
Example mydatafile.csv would be something like:
lvl1,des1,lvl2,des2,lvl3,des3,lvl4,des4,lvl5,des5,lvl6,des6,a test data set
I can't figure out why sqlldr keeps rejecting this control file. I'm using 'direct=false' in my .par file.
Any hints?I just noticed that my email is wrong. If you can help, plese send email to [email protected]
thanks. -
I ve been using nested tables ( with the 'index by' clause ).
I understand that the created array is stored in the RAM memory ( and not in the database files ).
1. Does this mean that my program might not run one day when the machine is running low on RAM ?
2. If yes, will there be an exception or something ?
3. I understand that the memory is freed when the 'session ends'. When does my session end ? ( Is it when my PROCEDURE/function ends or is it when I quit sqlplus )
4. Are nested tables/collections thread safe ?
If I declare a nested table within the procedure, and if the procedure is called simultaneously by two clients, will there be a problem ?
Thanks1. Yes
2. Yes, possibly "PLS-00996: out of memory" although there may be others
3. PL/SQL does garbage collection at various times, including when your session ends (i.e. when you disconnect from Oracle) and when a variable goes out of scope (e.g. when a procedure ends). I don't think the full list of rules is documented.
4. Yes, although I'm not sure what happens in connection pooling. -
SQL Loader, nested tables and default values
Is there a way to specify a default value for a nested table entry when SQL*Loader encounters a 'null' value?
I want to avoid this:
Record 5: Rejected - Error on table LEVEL_DESC, column LEVELS.
NULL nested table element is not allowedUse the NULLIF parameter in your control file for the nested table objects.
e.g
LOAD DATA
INFILE 'level_data.dat'
INTO TABLE LEVEL
(LEVEL_ID POSITION (01:05) CHAR
LEVEL_NAME POSITION (07:20)
LEVEL_DESC COLUMN OBJECT
(LEVELS POSITION (22:25) CHAR NULLIF LEVEL_DESC.LEVELS=BLNAKS,
... )) -
Hello, I am trying to figure out why I get the following error message
ORA-00604: error occurred at recursive SQL level 1
ORA-22914: DROP of nested tables not supported
I am using the TOAD tool to create tables using CTAS and I have managed to create several tables using the same SQL that is failing now with the only difference being a portion of the WHERE clause.
Any help will be very much appreciatedORA-22914: DROP of nested tables not supported
Cause: An attempt was made to DROP a nested table.
Action: Nested tables cannot be explicitly dropped. nested tables can
only be dropped by dropping their containing parent table. -
Hallo everyone,
I have a problem with nested tables. I can create them and insert data with the sql command line. It is not possible in the browser.
But I cannot view the values - neither in the command line nor in the browser.
In the browser, oracle shows this error-message:
'ORA-01031 insufficient privileges' (I have admin rights!!)
Is it possible that Oracle Express Edition doesn't support all object-oriented features?
Thanks in advanceI downloaded the latest apex 3.0.1, but itshouldn't
be used with Oracle XE according to theinstallation
guide.Where did you read that? There's even a XE releated
install guide:
[url=http://www.oracle.com/technology/products/databas
e/application_express/html/3.0.1_and_xe.html]Oracle
Database XE and Application Express 3.0.1C.
when I downloaded apex, the documentation in the database requirement (2.1) says:
"Note:
Do not install this version of Oracle Application Express on Oracle Database Express Edition (Oracle Database XE)."
Maybe the documentation is not up to date, because I downloaded last week -
PL/SQL add procedure with nested table
Hi,
I am trying to do a procedure to input information for one order and another for 2 orders.
The information I have so far is as follows:
Drop table Orders cascade constraints;
Drop type item_type;
Drop type Item_nested;
Create or Replace Type item_type AS Object (
Cat_code Varchar2(6),
Amount_ord Number(3),
Cost Number(5,2) );
Create or Replace Type item_nested as table of item_type;
Create Table Orders (
Order_no Varchar2(8) constraint pkorder primary key,
Customer_name Varchar2(30),
AddressLine1 Varchar2(20),
AddressLine2 Varchar2(20),
AddressLine3 Varchar2(20),
Town Varchar2(20),
Postcode Varchar2(10),
Country Varchar2(20),
Order_items item_nested,
Order_date Date)
Nested Table Order_items
Store as nested_items return as locator;
This has so far worked but I have not managed the insert procedure.
I am using Oracle SQL*plus
Thanks
SGWhat I think I need is something of the sort
Create or replace procedure add_order (ordno in Varchar2, Cust_name in varchar2, add1 in varchar2,
add2 in varchar2, add3 in Varchar2, Addtown in varchar2, Pstcde in Varchar2, addcountry in varchar2, ord_date in date)
AS
Begin
DBMS_OUTPUT.PUT_LINE ('Insert attempted');
Insert into Orders (Order_no,Customer_name, AddressLine1, AddressLine2, AddressLine3,
Town, Postcode, Country, Order_Date)
values (ordno, Cust_name,add1, add2, add3, Addtown, Pstcde, addcountry,
ord_date);
Commit;
DBMS_OUTPUT.PUT_LINE ('Insert successful');
Exception
When Others then DBMS_OUTPUT.PUT_LINE ('ERROR');
DBMS_OUTPUT.PUT_LINE ('Procedure failed');
End;
SG
Maybe you are looking for
-
Hi guys, we have one program where we calculate currency conversion rates e.g, BUD 2009,BUD 2010,ACT 2009,ACT 2010,LE1 2009 ....In my program they have code which calculates BUD CY(Budget current year rate),BUD PY(Budget previous year rate), ACT CY(A
-
Hello, I am helping someone out that is in the process of upgrating Forms developed in 6i to 11g Fusion, and their databases fom 9i to 11g. They need to show feasibility of the task first, and test the forms. They have two new_ boxes already setup wi
-
Hi All, I am having a problem with dates, scripts and the debugger. I have a procedure that takes a date value as an in_variable. procedure_x(in_date date) I usually call this procedure from a script. select to_date(my_date,'mm/dd/yyyy') into my_var
-
Controlling MDB lifecycle Programmatically
Hi all, Is there a way to control MDB life cycle (by code)? e.g. to stop it from consuming messages at runtime. Any help would be appreciated.
-
Ok so my problem in a nutshell: I can not access my shared itunes library on any of the other [3] macs in my house nor on my apple TV. Hear is some miscellanies information about my problem: - I'm pretty sure this is some how related to a messed up f