oracle convert string parameter of stored procedure to number array - oracle

I am writing a stored procedure that needs an WHERE IN clause with an array of numbers. How can I pass this array of numbers to the SP.
What I thought is sending a string like '123,234,345' and than parse it in the SP before using it. Is this a good way of doing it? If yes, how can I make the below code work?
CREATE OR REPLACE PROCEDURE sp_test2
(
ids in varchar2,
cursor_ OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN cursor_ FOR
SELECT *FROM my_table
WHERE my_table.ID IN (
SELECT regexp_substr(ids,'[^:]+', 1, level) AS list FROM dual
CONNECT BY regexp_substr(ids, '[^:]+', 1, level) IS NOT NULL);
END;
/
For example (instead of the conversion code) when use like this: IN(ids)
i get ORA-01722: invalid number error

I really don't want to complicate things
If that's ok with you to simply pass a string of numbers separated by commas as a parameter and split it afterwards, then you can do it like this:
SELECT *FROM my_table
WHERE my_table.ID IN (
SELECT regexp_substr(ids,'[^,]+', 1, level) AS list FROM dual
CONNECT BY regexp_substr(ids, '[^,]+', 1, level) IS NOT NULL);
However, I don't think that using a collection complicates things too much.

Like I suggested, here is how to achieve it using dynamic SQL.
create or replace procedure sp_test2(IDS in varchar2, CURSOR_ out SYS_REFCURSOR)
is
L_SQL varchar2(400);
begin
L_SQL := 'select * from MY_TABLE where ID in (' || IDS || ')';
open CURSOR_ for L_SQL;
end;

Related

Oracle PL/SQL - Receive a list of values as IN parameter, use them in the WHERE clause and return the results in a procedure

I need to create an Oracle procedure that receives a list of values, let's say Customer IDs, and uses those IDs in the WHERE clause of a SELECT statetement searching for all those Customer IDs in a table. The resulting columns and rows of this table must be returned by the procedure to the caller.
The quantity of Customer IDs is variable.
I imagine I should use cursors for this and the procedure would be declared similarly to this:
CREATE PROCEDURE (c_customers IN SYS_REFCURSOR, c_results OUT sys_refcursor) IS BEGIN ... but I don’t know how to manipulate those cursors.
I think you need to take customer ids as string input.
see if the following can help:
CREATE PROCEDURE YOUR_PROC_NAME (
C_CUSTOMERS IN VARCHAR2,
C_RESULTS OUT SYS_REFCURSOR
) IS
BEGIN
OPEN C_RESULTS FOR
SELECT *
FROM YOUR_TABLE
WHERE YOUR_COLUMN IN (
SELECT REGEXP_SUBSTR(C_CUSTOMERS, '[^,]+', 1, LEVEL)
FROM DUAL
CONNECT BY REGEXP_SUBSTR('SMITH,ALLEN,WARD,JONES', '[^,]+', 1, LEVEL) IS NOT NULL
);
END;
Cheers!!
an alternative solution is to use the collections:
First you have to create a global collection as a type
CREATE OR REPLACE TYPE tab_number is TABLE OF NUMBER;
you can then use it as a type of your arguments in your procedure
CREATE PROCEDURE YOUR_PROC_NAME (
in_id_list IN tab_number ,
C_RESULTS OUT SYS_REFCURSOR
) IS
BEGIN
OPEN C_RESULTS FOR
SELECT *
FROM YOUR_TABLE
WHERE YOUR_COLUMN IN (
SELECT column_value from table(in_id_list)
);
END;

ORACLE - Return cursor from stored procedure

I need to return a cursor object from a stored procedure, but I need to process data first.
For example, let's consider this simple stored procedure:
create or replace PROCEDURE TEST
(
query_str IN VARCHAR2,
CURSOR_ OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN CURSOR_ FOR query_str;
END;
This procedure returns data as is, with no postprocessing.
The improvement I need is the following:
process data coming from the execution of query_str;
return the processed data in the form of a cursor.
Anyone could suggest me a way to accomplish this?
Thanks
It is difficult to do what you are suggesting with dynamic SQL (unless all the statements you are passing to the procedure all have a similar output format). If you can know what the SELECT will be then you can store it in a collection and process it:
CREATE TABLE TEST_DATA ( id, name, dt ) AS
SELECT 1, 'A', SYSDATE FROM DUAL UNION ALL
SELECT 2, 'B', DATE '2017-01-01' FROM DUAL;
CREATE TYPE processed_data_obj AS OBJECT(
id INTEGER,
etag VARCHAR2(20)
);
/
CREATE OR REPLACE TYPE processed_data_table AS TABLE OF processed_data_obj;
/
CREATE OR REPLACE PROCEDURE TEST
(
CURSOR_ OUT SYS_REFCURSOR
)
AS
processed PROCESSED_DATA_TABLE;
BEGIN
-- Process it in the select statement
SELECT processed_data_obj(
id,
name || '_' || ROUND( ( dt - DATE '1970-01-01' ) * 24*60*60 )
)
BULK COLLECT INTO processed
FROM test_data;
-- Process it more in PL/SQL
FOR i IN 1 .. processed.COUNT LOOP
processed[i].etag := processed[i].etag || '_' || i;
END LOOP;
OPEN cursor_out FOR
SELECT *
FROM TABLE( processed );
END;
/

Are cursors necessary for queries in a procedure?

I'm rather new to Oracle and I was asked to write a procedure to query some data from a table. I built it with 2 arguments, a cursor and a number. Essentially I have:
PROCEDURE PROC_NAME (
cursor_name IN OUT NOCOPY MY_DEFINED_CURSOR_TYPE,
a_number IN NUMBER);
AS
BEGIN
OPEN CURSOR_NAME FOR
SELECT
column
FROM
table
WHERE
table.dat_value > (SYSDATE - a_number);
END PROC_NAME;
It works like a charm, and I'm able to fetch the column from the cursor. My problem is that the requester doesn't want to pass in a cursor, they just want to pass in the number. I've never created a procedure that doesn't use a cursor to return the values of a query and the examples I have seen only ever do it that way. Is this possible?
You can use a collection:
CREATE PROCEDURE PROC_NAME (
a_number IN NUMBER,
numbers OUT SYS.ODCINUMBERLIST
)
AS
BEGIN
SELECT number_value
BULK COLLECT INTO numbers
FROM table_name
WHERE date_value > (SYSDATE - a_number);
END PROC_NAME;
Also, if you don't want to pass in a cursor then you can just pass one out:
CREATE OR REPLACE PROCEDURE PROC_NAME (
a_number IN NUMBER,
numbers OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN numbers FOR
SELECT number_value
FROM table_name
WHERE date_value > (SYSDATE - a_number);
END PROC_NAME;
Use a function instead ? But it's just a "stylistic" difference compared to procedure out parameter. Anyway the returned value have to be implicitly passed (unlike in SQL Server as noted by #ShannonSeverance).
function f(
p_days in number
) return my_defined_cursor_type is
v_cur my_defined_cursor_type;
begin
open v_cur for
select
column
from
table
where
table.dat_value > (sysdate - p_days);
return v_cur;
end;
/
Usage
declare
v_cur my_defined_cursor_type := f(42);
begin
-- use v_cur as you like
end;
If you want to apply some PL/SQL logic, but remain using select for querying the data (i.e not pass in a cursor - use pipelined functions.
You need to define the TYPEs of the result row and table; FETCH the cursor and PIPE the results in the function.
CREATE or replace type MY_DEFINED_ROW_TYPE as object
(
txt VARCHAR2(30)
);
/
create or replace type MY_DEFINED_TABLE_TYPE as table of MY_DEFINED_ROW_TYPE
/
create or replace function FUN_NAME( a_number IN NUMBER) return
MY_DEFINED_TABLE_TYPE
PIPELINED
as
cur MY_DEFINED_CURSOR_TYPE;
v_txt varchar2(30);
begin
OPEN cur
FOR
SELECT
column
FROM table
WHERE table.dat_value > (SYSDATE - a_number);
LOOP
FETCH cur INTO v_txt;
EXIT WHEN cur%NOTFOUND;
pipe row(v_txt);
END LOOP;
return;
end;
/
The usage:
select * from table (FUN_NAME(2));

Oracle throwing exception for a large string passed in regex

I am trying following SQL :
SELECT REGEXP_SUBSTR ('00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:',
'[^,]+',
1,
LEVEL)
FROM DUAL
CONNECT BY REGEXP_SUBSTR ('00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:',
'[^,]+',
1,
LEVEL)
IS NOT NULL;
This is giving ORA-01704: string literal too long.
I understand the reason that the string passed is more than 4000 characters, but I want to process these many records in a PL/SQL block. String is nothing but the comma-separated values passed from UI to oracle procedure.
e.g :
00102034334:15:11-JUN-13 ~ ord_no:order_line_no:Date (1 order can have many order lines , parent-child relationship)
When I am above SQL with into clause offcourse, in a PL/SQL table type variable.
It is throwing error for me.
How shall I go ahead? Is there any other way parameters can be passed from UI to Oracle in such case.
PL/SQL strings can contain up to 32767 characters, if you want to process strings using PL/SQL, use varchar2(32767) in your procedure. If 32767 characters is still too short, just pass your string as a clob, CLOB's length is unlimited.
declare
long_varchar varchar2( 32767 );
v_clob clob;
begin
long_varchar := q'#[00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:', '[^,]+', 1, LEVEL) FROM DUAL CONNECT BY REGEXP_SUBSTR ('00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:#';
v_clob := long_varchar;
long_varchar := REGEXP_SUBSTR ( long_varchar, '[^,]+');
v_clob := REGEXP_SUBSTR ( v_clob, '[^,]+');
DBMS_OUTPUT.PUT_LINE( long_varchar );
DBMS_OUTPUT.PUT_LINE( v_clob );
end;
/
---- result ----
[00102034334:1:11-JUN-13:1
[00102034334:1:11-JUN-13:1
For very long string,store the data to be processed in a CLOB type variable and parse the data using REGEXP_SUBSTR by passing the CLOB type variable.
declare
v_clob clob;
begin
v_clob := '00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:,00102034334:1:11-JUN-13:1,00102034334:2:11-JUN-13:1,00102034334:3:11-JUN-13:1,00102034334:4:11-JUN-13:1,00102034334:5:11-JUN-13:,00102034334:6:11-JUN-13:,00102034334:7:11-JUN-13:,00102034334:8:11-JUN-13:,00102034334:9:11-JUN-13:,00102034334:10:11-JUN-13:,00102034334:11:11-JUN-13:,00102034334:12:11-JUN-13:,00102034334:13:11-JUN-13:,00102034334:14:11-JUN-13:,00102034334:15:11-JUN-13:,00102034334:16:11-JUN-13:,00102034334:17:11-JUN-13:,00102034334:18:11-JUN-13:,00102034334:19:11-JUN-13:,00102034334:20:11-JUN-13:,00102034334:21:11-JUN-13:,00102034334:22:11-JUN-13:,00102034334:23:11-JUN-13:,00102034334:24:11-JUN-13:,00102034334:25:11-JUN-13:,00102034334:26:11-JUN-13:,00102034334:27:11-JUN-13:,00102034334:28:11-JUN-13:,00102034334:29:11-JUN-13:,00102034334:30:11-JUN-13:,00102034334:31:11-JUN-13:,00102034334:32:11-JUN-13:,00102034334:33:11-JUN-13:,00102034334:34:11-JUN-13:,00102034334:35:11-JUN-13:';
select TO_CHAR (regexp_substr(v_clob,'[^,]+', 1, level)) from dual
connect by TO_CHAR (regexp_substr(v_clob, '[^,]+', 1, level)) is not null;
end;
/

Oracle datasets from two different tables without joining

I need to write a stored procedure that will provide the data from two different tables. Say table1 and table2. These two tables doesn't have any relationship.
Now in SQL Server i can simply create a stored procedures like:
create procedure abc
as
begin
select * from table1;
select * from table2:
end;
Now in oracle, I usually create a SYS_REFCURSOR and do something like:
Open SYS_REFCURSOR_VAR For Select * from table1;
but I don't know how to provide the two result sets from two different tables table1 and table2. I tried to create two different SYS_REFCURSOR one for each table. But when I executed the procedure I got the data from first table only. The second SYS_REFCURSOR doesn't seems to be working.
Anyone have any idea, how to accomplish this?
Try this
create or replace procedure tst
(p_refcursor1 out sys_refcursor,p_refcursor2 out sys_refcursor)
is
begin
open p_refcursor1 for
select * from dual;
open p_refcursor2 for
select * from dual;
end;
So I assume the records you want to pull from each table are effectively the same. e.g. id, name, price. In that case just write your query like
SELECT t1.id AS id, t1.name AS name, t1.unit_price AS price FROM t1
UNION
SELECT t2.id AS id, t2.description AS name, t2.price AS price FROM t2
Not sure if its required, but always good form to have a union return data sets with the same "column" names. So I used the AS in teh example to demonstrate this
You need to give more details about your problem.. specifically
1) the code that you have tried so far
2) How are you accessing your ref cursor to see the results? (SQLPLUS or Java or VB.net)?
Based on your SQL Server code, I am assuming you are trying to get the UNION of the rows from the two tables. Here is the code in Oracle that lets you do that. As you can see, my client tool here is SQLPLUS and I am able to see the values from both the tables.
create table t1(
id number,
name varchar2(10)
);
create table t2(
id number,
name varchar2(10)
);
create or replace procedure get_t1_and_t2(
o_cur out sys_refcursor) is
begin
open o_cur for
select id from t1
union all
select id from t2;
end;
/
Procedure created.
SQL> var rc refcursor;
SQL> exec get_t1_and_t2( :rc);
PL/SQL procedure successfully completed.
SQL> print rc;
ID
----------
1
2
This script shows #Maxim Shevtsov answer working. Run it in SQL*Plus
SET serveroutput ON size 100000
DECLARE
c1 SYS_REFCURSOR;
c2 SYS_REFCURSOR;
v1 VARCHAR2(10);
v2 VARCHAR2(10);
n2 NUMBER;
PROCEDURE tst ( p_refcursor1 OUT SYS_REFCURSOR
, p_refcursor2 OUT SYS_REFCURSOR)
IS
BEGIN
OPEN p_refcursor1 FOR
SELECT 'val1' FROM DUAL;
OPEN p_refcursor2 FOR
SELECT 'val2', 42 FROM DUAL;
END tst;
BEGIN
tst( c1, c2 );
LOOP
FETCH c1 INTO v1;
EXIT WHEN c1%NOTFOUND;
DBMS_OUTPUT.put_line( 'CURSOR1: ' || v1 );
END LOOP;
LOOP
FETCH c2 INTO v2, n2;
EXIT WHEN c2%NOTFOUND;
DBMS_OUTPUT.put_line( 'CURSOR2: ' || v2 || ' ' || n2 );
END LOOP;
END;
/

Resources