BULK COLLECT into a table of objects - oracle

When attempting to use a BULK COLLECT statement I got error ORA-00947: not enough values.
An example script:
CREATE OR REPLACE
TYPE company_t AS OBJECT (
Company VARCHAR2(30),
ClientCnt INTEGER );
/
CREATE OR REPLACE
TYPE company_set AS TABLE OF company_t;
/
CREATE OR REPLACE
FUNCTION piped_set (
v_DateBegin IN DATE,
v_DateEnd IN DATE
)
return NUMBER /*company_set pipelined*/ as
v_buf company_t := company_t( NULL, NULL);
atReport company_set;
sql_stmt VARCHAR2(500) := '';
begin
select * BULK COLLECT INTO atReport
from (
SELECT 'Descr1', 1 from dual
UNION
SELECT 'Descr2', 2 from dual ) ;
return 1;
end;
The error occurs at the line select * BULK COLLECT INTO atReport.
Straight PL/SQL works fine by the way (so no need to mention it as a solution). Usage of BULK COLLECT into a user table type is the question.

Your company_set is a table of objects, and you're selecting values, not objects comprised of those values. This will compile:
select * BULK COLLECT INTO atReport
from (
SELECT company_t('Descr1', 1) from dual
UNION
SELECT company_t('Descr2', 2) from dual ) ;
... but when run will throw ORA-22950: cannot ORDER objects without MAP or ORDER method because the union does implicit ordering to identify and remove duplicates, so use union all instead:
select * BULK COLLECT INTO atReport
from (
SELECT company_t('Descr1', 1) from dual
UNION ALL
SELECT company_t('Descr2', 2) from dual ) ;

Related

Table variable is filled only with one value

I have a stored procedure which should return several results - but it returns only one row. I think it's the last row in result set.
I am not sure, but I think the problem is in this line of code:
select chi.id bulk collect into v_numbers from dual;
and that this line somehow overrides all previous results (there is several of them for each loop). How to insert into v_numbers without overriding previous results? I know that it's also wrong to insert only one row, but I haven't found solution to insert several rows from chi.
PROCEDURE GET_ATTRIBUTES(
P_AUTH_USE_ID IN NUMBER,
P_CATEGORY_ID IN NUMBER,
P_VERSION_ID IN NUMBER,
P_RESULT OUT TYPES.CURSOR_TYPE
) IS
v_numbers sys.odcinumberlist := null;
BEGIN
FOR item IN
(SELECT ID FROM INV_SRV WHERE SRV_CATEGORY_ID IN
(
SELECT id
FROM inv_srv_category
START WITH parent_category_id = P_CATEGORY_ID
CONNECT BY PRIOR id = parent_category_id
) OR SRV_CATEGORY_ID = P_CATEGORY_ID)
LOOP
for chi in (select s.id
from inv_srv s
start with s.parent_srv_id = item.id
connect by prior s.id = s.parent_srv_id
)
loop
select chi.id bulk collect into v_numbers from dual; --> here I should insert all rows from that loop, but I don't know how
end loop;
END LOOP;
OPEN P_RESULT FOR SELECT t.column_value from table(v_numbers) t; --> only one row is returned
END;
Use BULK COLLECT and FORALL for bulk inserts and better performance. The FORALL statement will allow the DML to be run for each row in the collection without requiring a context switch each time, thus improving the overall performance.
CREATE OR REPLACE PROCEDURE get_attributes (
p_auth_use_id IN NUMBER,
p_category_id IN NUMBER,
p_version_id IN NUMBER,
p_result OUT types.cursor_type
) IS
v_numbers sys.odcinumberlist := NULL;
BEGIN
SELECT s.id
BULK COLLECT --> Bulk collect all values
INTO v_numbers
FROM inv_srv s
start with s.parent_srv_id in (
SELECT ID FROM INV_SRV
WHERE SRV_CATEGORY_ID IN
(
SELECT id
FROM inv_srv_category
START WITH parent_category_id = P_CATEGORY_ID
CONNECT BY PRIOR id = parent_category_id
)
OR SRV_CATEGORY_ID = P_CATEGORY_ID)
connect by prior s.id = s.parent_srv_id;
FORALL i IN 1..v_numbers.COUNT
INSERT INTO your_table VALUES v_numbers ( i ); --> Bulk insert
END;
Every time the loop executes v_numbers will be re populated again and again so, either 1) use v_numbers.extend; v_numbers(v_numbers.last) = "Your Value" or write everything in a single bulk collect.
select s.id
bulk collect into v_numbers
from inv_srv s
start with s.parent_srv_id in (SELECT ID FROM INV_SRV
WHERE SRV_CATEGORY_ID IN
(
SELECT id
FROM inv_srv_category
START WITH parent_category_id = P_CATEGORY_ID
CONNECT BY PRIOR id = parent_category_id
)
OR SRV_CATEGORY_ID = P_CATEGORY_ID)
connect by prior s.id = s.parent_srv_id
This may be considered as a improper use of the PL/SQL loops (often connected with a catastrophic performance) in a situation where a SQL solution exists.
Why don't you simple defines the cursor as follows:
OPEN P_RESULT FOR
select s.id
from inv_srv s
start with s.parent_srv_id in
(SELECT ID FROM INV_SRV WHERE SRV_CATEGORY_ID IN
(SELECT id
FROM inv_srv_category
START WITH parent_category_id = 1
CONNECT BY PRIOR id = parent_category_id
) OR SRV_CATEGORY_ID = 1)
connect by prior s.id = s.parent_srv_id
;
The query is constructed from your outer and inner loop so that it returns the same result.
The transformation may not be trivial in generall case and must be carefully tested, but the performance profit may be high.

insert 1000+ long numbers into oracle temp table

I was doing a select operation on my table in oracle but was getting ORA-01795 so,
then I try inserting my values in the list of order 1000+ (890623250,915941020,915941021,....1000+ times) into temp table and I can't figure it out how to do it so that later I can do a select from a temp table
So basically my objective is to insert those 1000 id into the temp table of schema
TEMP_L{ID INTEGER} like INSERT INTO TEMP_LINK SELECT(890623254,915941020,1000+ values )
Use a collection. SYS.ODCINUMERLIST is a built-in VARRAY:
INSERT INTO TEMP_LINK ( value )
SELECT COLUMN_VALUE
FROM TABLE( SYS.ODCINUMBERLIST( 890623254,915941020,1000 /* + values */ ) );
Or you can define your own collection:
CREATE TYPE NumberList IS TABLE OF NUMBER;
INSERT INTO TEMP_LINK ( value )
SELECT COLUMN_VALUE
FROM TABLE( NumberList( 890623254,915941020,1000 /* + values */ ) );
However, if you are going to use collections then you don't need to load them into a temp table:
SELECT *
FROM your_table
WHERE your_id MEMBER OF NumberList( 890623254,915941020,1000 /* + values */ )
or
SELECT *
FROM your_table
WHERE your_id IN (
SELECT COLUMN_VALUE
FROM TABLE( 890623254,915941020,1000 /* + values */ )
);
Preferably use SQL* Loader for bulk inserts. One other option would be to construct a query using Excel or notepad++ for all the ids.
INSERT INTO mytable(id)
select 890623250 FROM DUAL UNION ALL
select 915941020 FROM DUAL UNION ALL
...
..

How to write and call an Oracle function in SQL

I have two Oracle tables with a similar structure:
I'd like to write a function in Oracle which sum all values for each ID and returns a pair (ID,Text) where Text='ALERT' if the sum is greater than 100, 'OK' otherwise:
Then, I'd like to execute a query for each table, for example something like that:
SELECT MY_FUN() FROM TABLE_1
SELECT MY_FUN() FROM TABLE_2
Is this a right approach? How could I write this function?
Thanks
Generally it is considered bad practice to call a function in SQL which executes SQL. It creates all kinds of problems.
Here is one solution:
create or replace function my_fun
( p_sum in number) return varchar2 is
begin
if p_sum > 100 then return 'ALERT';
else return 'OK';
end if;
end;
/
Run it like this:
select id, my_fun(sum(val)) as state
from your_table
group by id;
You can do this in sql like the following. If you want to create a function to to this for different tables, you can wrap this in a function that takes the table name and run the query via dynamic SQL, but you are limited to the same columns.
-- begin test data
with test_data(id, value) as
(select 1, 100 from dual union all
select 1, 50 from dual union all
select 2, 75 from dual union all
select 3, 50 from dual union all
select 3, 51 from dual)
-- end test data
select id, sum(value),
case when sum(value) > 100 then 'ALERT'
else 'OK' end AS alert
from test_data
group by id;

Need to write a procedure to fetch given rownums

I need to write one procedure to pick the record for given rows
for example
procedure test1
(
start_ind number,
end_ind number,
p_out ref cursor
)
begin
opecn p_out for
select * from test where rownum between start_ind and end_ind;
end;
when we pass start_ind 1 and end_ind 10 its working.But when we change start_ind to 5
then query looks like
select * from test where rownum between 5 and 10;
and its fails and not shows the output.
Please assist how to fix this issue.Thanks!
The rownum is assigned and then the where condition evaluated. Since you'll never have a rownum 1-4 in your result set, you never get to rownum 5. You need something like this:
SELECT * FROM (
SELECT rownum AS rn, t.*
FROM (
SELECT t.*
FROM test t
ORDER BY t.whatever
)
WHERE ROWNUM <= 10
)
WHERE rn >= 5
You'll also want an order by clause in the inner select, or which rows you get will be undefined.
This article by Tom Kyte pretty much tells you everything you need to know: http://www.oracle.com/technetwork/issue-archive/2006/06-sep/o56asktom-086197.html
SELECT *
from (SELECT rownum AS rn, t.*
FROM MyTable t
WHERE ROWNUM <= 10
ORDER BY t.NOT-Whatever
-- (its highly important to use primary or unique key of MyTable)
WHERE rn > 5
As a hint, :
Typically we use store-procedures for data validation, access control, extensive or complex processing that requires execution of several SQL statements. Stored procedures may return result sets, i.e. the results of a SELECT statement. Such result sets can be processed using cursors, by other stored procedures, by associating a result set locator, or by applications
I think you are going to use the ruw-number to fetch paged queries.
Try to create a generic select query based on the idea mentioned above.
Two possibilities:
1) Your table is an index-organized table. So its data is sorted. You would select those first rows you want to avoid and based on that get the next rows you are looking for:
create or replace procedure get_records
(
vi_start_ind integer,
vi_end_ind integer,
vo_cursor out sys_refcursor
) as
begin
open vo_cursor for
select *
from test
where rownum <= vi_end_ind - vi_start_ind + 1
and rowid not in
(
select rowid
from test
where rownum < vi_start_ind
)
;
end;
2) Your table is not index-organized, which is normally the case. Then its records are not sorted. To get records m to n, you would have to tell the system what order you have in mind:
create or replace procedure get_records
(
vi_start_ind number,
vi_end_ind number,
vo_cursor out sys_refcursor
) as
begin
open vo_cursor for
select *
from test
where rownum <= vi_end_ind - vi_start_ind + 1
and rowid not in
(
select rowid from
(
select rowid
from test
order by somthing
)
where rownum < vi_start_ind
)
order by something
;
end;
All this said, think it over what you want to achieve. If you want to use this procedure to read your table block for block, keep in mind that it will read the same data again and again. To know what rows 1,000,001 to 1,000,100 are, the dbms must read through one million rows first.

How to compare items in an array to those in a database column using regular expressions?

I'm trying to take a list of elements in an array like this:
['GRADE', 'GRATE', 'GRAPE', /*About 1000 other entries here ...*/ ]
and match them to their occurrences in a column in an Oracle database full of entries like this:
1|'ANTERIOR'
2|'ANTEROGRADE'
3|'INGRATE'
4|'RETROGRADE'
5|'REIGN'
...|...
/*About 1,000,000 other entries here*/
For each entry in that array of G words, I'd like to loop through the word column of the Oracle database and try to find the right-sided matches for each entry in the array. In this example, entries 2, 3, and 4 in the database would all match.
In any other programming language, it would look something like this:
for entry in array:
for each in column:
if entry.right_match(each):
print entry
How do I do this in PL/SQL?
In PL/SQL it can be done in this way:
declare
SUBTYPE my_varchar2_t IS varchar2( 100 );
TYPE Roster IS TABLE OF my_varchar2_t;
names Roster := Roster( 'GRADE', 'GRATE', 'GRAPE');
begin
FOR c IN ( SELECT id, name FROM my_table )
LOOP
FOR i IN names.FIRST .. names.LAST LOOP
IF regexp_like( c.name, names( i ) ) THEN
DBMS_OUTPUT.PUT_LINE( c.id || ' ' || c.name );
END IF;
END LOOP;
END LOOP;
end;
/
but this is row by row processing, for large table it would be very slow.
I think it might be better to do it in a way shown below:
create table test123 as
select 1 id ,'ANTERIOR' name from dual union all
select 2,'ANTEROGRADE' from dual union all
select 3,'INGRATE' from dual union all
select 4,'RETROGRADE' from dual union all
select 5,'REIGN' from dual ;
create type my_table_typ is table of varchar2( 100 );
/
select *
from table( my_table_typ( 'GRADE', 'GRATE', 'GRAPE' )) x
join test123 y on regexp_like( y.name, x.column_value )
;
COLUMN_VALUE ID NAME
------------- ---------- -----------
GRADE 2 ANTEROGRADE
GRATE 3 INGRATE
GRADE 4 RETROGRADE

Resources