Using oracle object [message #633427] |
Wed, 18 February 2015 09:51 |
manns
Messages: 59 Registered: March 2006 Location: Bangalore
|
Member |
|
|
created the employee table as
create table employee (id number, first_name varchar2(50))
Created the object as
CREATE TYPE rec_type AS OBJECT (eid VARCHAR2(25), ename VARCHAR2(15))
create type rec_type_collection is table of rec_type
Now how can i read the value to my procedure and insert to employee table.
below one is not working
PROCEDURE print_rec_type (rec rec_type_collection) IS
BEGIN
DBMS_OUTPUT.PUT_LINE(rec.eid);
DBMS_OUTPUT.PUT_LINE(rec.ename);
END;
|
|
|
|
Re: Using oracle object [message #633429 is a reply to message #633427] |
Wed, 18 February 2015 10:00 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
What element of rec.eid do you want to display? I will give you some hints.
you will need a loop
read up on how to use collections (try google for "oracle collections")
|
|
|
|
Re: Using oracle object [message #633431 is a reply to message #633430] |
Wed, 18 February 2015 10:09 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
And what about the data? Why do you think that just declaring a TYPE would suffice to fetch the required rows from the table? You are way behind even understanding the basics.
|
|
|
|
|
|
|
Re: Using oracle object [message #633447 is a reply to message #633435] |
Wed, 18 February 2015 16:59 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
You need to read up on collections and object types.
insert into sometable
select a.*
from table(cast(somevariable as somecollectiontype)) a
/
Basically you use Oracle functions to convert the collection to a set of rows and then do the normal stuff with the row set. This is of course all explained in the manuals. Plus there are numerous examples on the WEB that can be found by searching.
One more thing... once you find a solution, don't forget to come back and post it so that others can see.
Observe the following. In particular, pay attention to all the nesting and use of open/close parenthesis. These are all required and is one of the reasons learning this particular stuff can be frustrating.
17:55:09 SQL> drop type rec_type_collection
17:55:16 2 /
Type dropped.
Elapsed: 00:00:00.00
17:55:16 SQL> drop type rec_type
17:55:16 2 /
Type dropped.
Elapsed: 00:00:00.00
17:55:16 SQL> drop table employee
17:55:16 2 /
Table dropped.
Elapsed: 00:00:00.00
17:55:16 SQL>
17:55:16 SQL> create table employee (id number, first_name varchar2(50))
17:55:16 2 /
Table created.
Elapsed: 00:00:00.00
17:55:16 SQL>
17:55:16 SQL> CREATE TYPE rec_type AS OBJECT (eid VARCHAR2(25), ename VARCHAR2(15))
17:55:16 2 /
Type created.
Elapsed: 00:00:00.01
17:55:16 SQL>
17:55:16 SQL> create type rec_type_collection is table of rec_type
17:55:16 2 /
Type created.
Elapsed: 00:00:00.00
17:55:16 SQL>
17:55:16 SQL> select 1 c1,'Michel Cadot' c2 from dual union all
17:55:16 2 select 2,'manns' from dual union all
17:55:16 3 select 3,'gazzag' from dual union all
17:55:16 4 select 4,'Lalit Kumar B' from dual union all
17:55:16 5 select 5,'Bill B' from dual union all
17:55:16 6 select 6,'BlackSwan' from dual
17:55:16 7 /
C1 C2
---------- -------------
1 Michel Cadot
2 manns
3 gazzag
4 Lalit Kumar B
5 Bill B
6 BlackSwan
6 rows selected.
Elapsed: 00:00:00.01
17:55:16 SQL>
17:55:16 SQL> set linesize 80
17:55:16 SQL>
17:55:16 SQL> select cast(multiset(select * from (
17:55:16 2 select 1 c1,'Michel Cadot' c2 from dual union all
17:55:16 3 select 2,'manns' from dual union all
17:55:16 4 select 3,'gazzag' from dual union all
17:55:16 5 select 4,'Lalit Kumar B' from dual union all
17:55:16 6 select 5,'Bill B' from dual union all
17:55:16 7 select 6,'BlackSwan' from dual
17:55:16 8 )
17:55:16 9 ) as rec_type_collection
17:55:16 10 ) data_as_a_collection
17:55:16 11 from dual
17:55:16 12 /
DATA_AS_A_COLLECTION(EID, ENAME)
--------------------------------------------------------------------------------
REC_TYPE_COLLECTION(REC_TYPE('1', 'Michel Cadot'), REC_TYPE('2', 'manns'), REC_T
YPE('3', 'gazzag'), REC_TYPE('4', 'Lalit Kumar B'), REC_TYPE('5', 'Bill B'), REC
_TYPE('6', 'BlackSwan'))
1 row selected.
Elapsed: 00:00:00.00
17:55:16 SQL>
17:55:16 SQL> insert into employee
17:55:16 2 select a.*
17:55:16 3 from table(cast(
17:55:16 4 (
17:55:16 5 select cast(multiset(select * from (
17:55:16 6 select 1 c1,'Michel Cadot' c2 from dual union all
17:55:16 7 select 2,'manns' from dual union all
17:55:16 8 select 3,'gazzag' from dual union all
17:55:16 9 select 4,'Lalit Kumar B' from dual union all
17:55:16 10 select 5,'Bill B' from dual union all
17:55:16 11 select 6,'BlackSwan' from dual
17:55:16 12 )
17:55:16 13 ) as rec_type_collection
17:55:16 14 ) data_as_a_collection
17:55:16 15 from dual
17:55:16 16 ) as rec_type_collection
17:55:16 17 )
17:55:16 18 ) a
17:55:16 19 /
6 rows created.
Elapsed: 00:00:00.00
17:55:16 SQL>
17:55:16 SQL> select *
17:55:16 2 from employee
17:55:16 3 /
ID FIRST_NAME
---------- --------------------------------------------------
1 Michel Cadot
2 manns
3 gazzag
4 Lalit Kumar B
5 Bill B
6 BlackSwan
6 rows selected.
Elapsed: 00:00:00.01
17:55:16 SQL>
HAVE FUN! Kevin
|
|
|
|
|
Re: Using oracle object [message #633528 is a reply to message #633427] |
Fri, 20 February 2015 11:49 |
Solomon Yakobson
Messages: 3273 Registered: January 2010 Location: Connecticut, USA
|
Senior Member |
|
|
You can also use COLLECT:
SQL> with t as (
2 select 1 c1,'Michel Cadot' c2 from dual union all
3 select 2,'manns' from dual union all
4 select 3,'gazzag' from dual union all
5 select 4,'Lalit Kumar B' from dual union all
6 select 5,'Bill B' from dual union all
7 select 6,'BlackSwan' from dual
8 )
9 select cast(
10 collect(
11 rec_type(
12 c1,
13 c2
14 )
15 )
16 as rec_type_collection
17 ) data_as_a_collection
18 from t
19 /
DATA_AS_A_COLLECTION(EID, ENAME)
--------------------------------------------------------------------------------
REC_TYPE_COLLECTION(REC_TYPE('1', 'Michel Cadot'), REC_TYPE('2', 'manns'), REC_T
YPE('3', 'gazzag'), REC_TYPE('4', 'Lalit Kumar B'), REC_TYPE('5', 'Bill B'), REC
_TYPE('6', 'BlackSwan'))
SQL>
SY.
[Updated on: Fri, 20 February 2015 11:49] Report message to a moderator
|
|
|
|
|