oracle blob text search - oracle

Is it possible to search through blob text using sql statement?
I can do select * from $table where f1 like '%foo%' if the f1 is varchar, how about f1 is a blob? Any counter part for this?

This is quite possible and easy to do.
Simply use dbms_lob.instr in conjunction with utl_raw.cast_to_raw
So in your case, if t1 is a BLOB the select would look like:
select *
from table1
where dbms_lob.instr (t1, -- the blob
utl_raw.cast_to_raw ('foo'), -- the search string cast to raw
1, -- where to start. i.e. offset
1 -- Which occurrance i.e. 1=first
) > 0 -- location of occurrence. Here I don't care. Just find any
;

If it is a Word or PDF document, look into Oracle Text.

If you are storing plain text it should be a CLOB, not a BLOB, and then you can still query using LIKE. A BLOB contains binary data that Oracle doesn't know the structure of, so it cannot search it in this way.
This works for CLOBs of any length (at least on Oracle 12C):
SQL> create table t1 (c clob);
Table created.
SQL> declare
2 x clob;
3 begin
4 for i in 1..100 loop
5 x := x || rpad('x', 32767, 'x');
6 end loop;
7 x := x || 'z';
8 for i in 1..100 loop
9 x := x || rpad('x', 32767, 'x');
10 end loop;
11 insert into t1 values (x);
12 end;
13 /
PL/SQL procedure successfully completed.
SQL> select dbms_Lob.getlength(c) from t1 where c like '%z%';
DBMS_LOB.GETLENGTH(C)
---------------------
6553401
Note that there is only one 'z' in that 6,554,401 byte CLOB - right in the middle of it:
SQL> select instr(c, 'z') from t1;
INSTR(C,'Z')
------------
3276701

the below code is to display the details from blob as text using UTL_RAW.CAST_TO_VARCHAR2 function then we use substr function to cut the text from the start of expected data till end. however, you can use instr function, LENGTH function , if you know the location of the data you are looking for
select NVL(SUBSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body),
INSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body), '<ns:xml_element>') + LENGTH('<ns:xml_element>'),
INSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body), '</ns:xml_element>') - (
INSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body), '<ns:xml_element>') + LENGTH('<ns:xml_element>'))),
utl_raw.cast_to_varchar2(DBMS_LOB.SUBSTR(blob_body))
) blob_body
from dual
where SUBSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body),
INSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body), '<ns:xml_element>') + LENGTH('<ns:xml_element>'),
INSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body), '</ns:xml_element>') - (
INSTR(UTL_RAW.CAST_TO_VARCHAR2(blob_body), '<ns:xml_element>') + LENGTH('<ns:xml_element>'))) like '%foo%';

Select * From TABLE_NAME
and dbms_lob.instr("BLOB_VARIABLE_NAME", utl_raw.cast_to_raw('search_text'), 1, 1) > 0

Related

Oracle how to address column in PL/SQL FOR LOOP by it's position instead of column name

Is there a way to address column by it's position in PL/SQL, something like this
BEGIN
FOR r IN (SELECT * FROM table)
LOOP
DBMS_OUTPUT.PUT_LINE(r.(1));
END LOOP;
END;
I tried both bracket types but always get
PLS-00103: Encountered the symbol "(" when expecting one of the following
I also tried BULK COLECT INTO with table of varray but then I get
PLS-00642: local collection types not allowed in SQL statements
And if I declare table with %ROWTYPE and bulk it then it seems to behave the same way as the normal loop.
I tried to look into documentation but couldn't find any example of this scenario.
Using the DBMS_SQL package and the COLUMN_VALUE procedure: but that's a lot of work... probably overkilling for your actual needs: you have to open a sys_refcursor for the query, convert it into a cursor number with DBMS_SQL.TO_CURSOR_NUMBER, collect the descriptions of the columns with DBMS_SQL.DESCRIBE_COLUMNS and loop on the column count and fetch the value with DBMS_SQL.COLUMN_VALUE into a specific variable according to its type...
In general, no. Oracle does not support accessing records by position; only by identifier.
You can approach the problem slightly differently and, instead of using SELECT *, you can explicitly list the columns to select and give them numeric column aliases:
BEGIN
FOR r IN (SELECT dummy AS "1" FROM DUAL)
LOOP
DBMS_OUTPUT.PUT_LINE(r."1");
END LOOP;
END;
/
You are still referring to the columns by name; only now their names are the "numeric" aliases.
Note: Personally, I would stick to using the column names and not try to find work-arounds to use positions as positions can change if columns are deleted from the table.
If you want to have a matrix in PL/SQL you could define one as an indexed table of an indexed table of whatever data type you're dealing with, something like:
type t_row is table of varchar2(1) index by pls_integer;
type t_tab is table of t_row index by pls_integer;
l_matrix t_tab;
The tricky part is populating it. DBMS_SQL is certainly one approach, but partly for fun, you could use XML. The DBMS_XMLGEN package lets you run a query and get the results back as an XML document with a ROWSET root node. You can then count the ROW nodes under that, and assuming there are any, count the nodes within the first ROW - which gives the matrix dimensions.
You can then use nested loops to populate the matrix, and once done, you can refer to an element as for example l_matrix(3)(2).
This is a working example, where the table being queried has values that are all a single character, for simplicity; you would need to change the matrix definition to handle the data you expect.
DECLARE
-- elements of matrix need to be data type and size suitable for your data
type t_row is table of varchar2(1) index by pls_integer;
type t_tab is table of t_row index by pls_integer;
l_matrix t_tab;
l_xml xmltype;
l_rows pls_integer;
l_cols pls_integer;
BEGIN
l_xml := dbms_xmlgen.getxmltype('SELECT * FROM your_table');
SELECT XMLQuery('count(/ROWSET/ROW)'
PASSING l_xml
RETURNING CONTENT).getnumberval()
INTO l_rows
FROM DUAL;
IF l_rows = 0 THEN
dbms_output.put_line('No data');
RETURN;
END IF;
SELECT XMLQuery('count(/ROWSET/ROW[1]/*)'
PASSING l_xml
RETURNING CONTENT).getnumberval()
INTO l_cols
FROM DUAL;
dbms_output.put_line('Rows: ' || l_rows || ' Cols: ' || l_cols);
-- populate matrix
FOR i IN 1..l_rows LOOP
FOR j in 1..l_cols LOOP
l_matrix(i)(j) := l_xml.extract('/ROWSET/ROW[' || i || ']/*[' || j || ']/text()').getstringval();
END LOOP;
END LOOP;
-- refer to a specific element
dbms_output.put_line('Element 3,2 should be H; is actually: ' || l_matrix(3)(2));
-- display all elements as list
FOR i IN 1..l_matrix.COUNT LOOP
FOR j IN 1..l_matrix(i).COUNT LOOP
dbms_output.put_line('Row ' || i || ' col ' || j || ': ' || l_matrix(i)(j));
END LOOP;
END LOOP;
-- display all elements as grid
FOR i IN 1..l_matrix.COUNT LOOP
FOR j IN 1..l_matrix(i).COUNT LOOP
IF j > 0 THEN
dbms_output.put(' ');
END IF;
-- format/pad real data to align it; simple here with single-char values
dbms_output.put(l_matrix(i)(j));
END LOOP;
dbms_output.new_line;
END LOOP;
END;
/
With my sample table that outputs:
Rows: 4 Cols: 3
Element 3,2 should be H; is actually: H
Row 1 col 1: A
Row 1 col 2: B
Row 1 col 3: C
Row 2 col 1: D
Row 2 col 2: E
Row 2 col 3: F
Row 3 col 1: G
Row 3 col 2: H
Row 3 col 3: I
Row 4 col 1: J
Row 4 col 2: K
Row 4 col 3: L
A B C
D E F
G H I
J K L
fiddle
As #astenx said in a comment, you can use JSON instead of XML; which would allow you to populate the matrix like this.
That has raised something else I'd overlooked. My dummy table doesn't have an ID column or an obvious way of ordering the results. For the JSON version I used rownum, but in both that and the original query the order of the rows (in the result set, and thus in the matrix) is currently indeterminate. You need to have a column you can order by in the query, or some other way to determine the sequence of rows.

Need help in converting the clob to varchar in oracle, I have to use the varchar in case function of oracle

I have a requirement where I have to fetch the data from the clob data type , convert to the varchar2, to make a pivot for oracle 10g.
I am using the following
select max(case
when key='abc'
then dbms_lob.substr(value)
end) as data_abc
from table.
if the value is less than 4000 the above query works fine but if it is more than 4000 it shows and error of buffer limit. On reading few blogs I came to know that dbms_lob.substr() can handle only 4000 characters in sql but can handle up to 32k in a pl/sql statement.
if I write a procedure and run it, it works fine. but i want to use it in a function. below is my function :
create or replace FUNCTION CLOBTOVARCHAR
RETURN varchar2 is out_attribute_var varchar2(32767) ;
BEGIN
FOR i IN (select attribute_Value from car_course_attribute where id=1547156)
LOOP
out_attribute_var := dbms_lob.substr(i.attribute_Value, 32000, 1);
END LOOP;
RETURN out_attribute_var;
EXCEPTION
WHEN OTHERS THEN
raise_application_error(-20001, 'An error was encountered - '||SQLCODE||' -ERROR- '||SQLERRM);
END CLOBTOVARCHAR;
If the data is small it works fine but if the data is bigger than 4k, it gives same error back. now I have two questions:
1) Am I doing right by converting the clob to varchar2 as I want to get pivot
2) Is my function correct?
Unfortunately SQL in Oracle supports varchars up to 4000.
Your function won't work in SQL queries.
You can upgrade to oracle 12c which increases this limit up to 32767 characters.
However there is a simple workaround that works on 11g, here is an example of CLOBs pivot for 3 columns:
SELECT (select val from xx
where rowid = a_rid ) a,
(select val from xx
where rowid = b_rid ) b,
(select val from xx
where rowid = c_rid ) c
from (
select max( case key when 'A' then rowid end ) a_rid,
max( case key when 'B' then rowid end ) b_rid,
max( case key when 'C' then rowid end ) c_rid
from xx
);
Here is SQLFiddle demo with 3 strings, each of them contains 7996 characters.
A result row in this demo is very wide, it has over 150 "horizontal pages"
I am surprised that SQLFiddle can display rows 24K characters wide
A third query in this demo display lengths of pivoted colums, each of them has 7996 characters.
dbms_lob.substr( clob_column, for_how_many_bytes, from_which_byte );
for example:
select dbms_lob.substr( x, 4000, 1 ) from T;
will get me the first 4000 bytes of the clob. Note that when using SQL as I did, the max length is 4000. You can get 32k using plsql:
declare
my_var long;
begin
for x in ( select X from t )
loop
my_var := dbms_lob.substr( x.X, 32000, 1 );
....
This was accurate as of 2016 or so. As of Oracle Database 12c, we now support extended varchars2's - up to a length of now 32k - which may be fewer than 32,000 and change characters if using Multi-Byte character set (2,3,or 4 bytes per character)
Originally answered by Tom here
I have a working scenario for an 8k long string into a varchar2 using just SQL on LiveSQL here
The code for a 12c environment with extended varchars enabled.
create table long_vars2 (a integer, xyz varchar2(8000), clobs clob);
create or replace procedure p( p_x in int, p_new_text in varchar2 )
as
begin
insert into long_vars2 (a, xyz, clobs) values ( p_x, p_new_text, p_new_text );
end;
/
exec p(1, rpad('*',8000,'*') );
select dbms_lob.substr( clobs, 7000, 1 ) from long_vars2;

WHERE condition for "starts with any of the values in a CSV list" [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
how to convert csv to table in oracle
I have a query in PL/SQL that is built to handle input in a variable as a "starts-with" filter:
WHERE product_group LIKE strProductGroup || '%'
As a new "feature", the input variable could contain comma separated values. So, where before I would expect something like "ART", I could now see "ART,DRM".
I'd like to avoid building this query as a string and using EXECUTE IMMEDIATE, if possible. Can anyone think of a way to write a WHERE condition that is the equivalent of saying "starts with any of the values in a CSV list" in Oracle 10g?
Assuming that you don't have any restrictions on creating a couple of additional objects (a collection type and a function), you can parse the list into a collection that you can reference in your query. Tom Kyte has a good discussion on this in his variable "IN" list thread.
If you use Tom's myTableType and in_list function, for example
SQL> create or replace type myTableType as table
of varchar2 (255);
2 /
Type created.
ops$tkyte#dev8i> create or replace
function in_list( p_string in varchar2 ) return myTableType
2 as
3 l_string long default p_string || ',';
4 l_data myTableType := myTableType();
5 n number;
6 begin
7 loop
8 exit when l_string is null;
9 n := instr( l_string, ',' );
10 l_data.extend;
11 l_data(l_data.count) :=
ltrim( rtrim( substr( l_string, 1, n-1 ) ) );
12 l_string := substr( l_string, n+1 );
13 end loop;
14
15 return l_data;
16 end;
17 /
Then you can search for equality relatively easily.
WHERE product_group IN (SELECT column_value
FROM TABLE( in_list( strProductGroup )))
But you want to do a LIKE which is a bit more challenging since you can't do a LIKE on an in-list. You could, however, do something like
select *
from emp e,
(select '^' || column_value search_regexp
from table( in_list( 'KIN,BOB' ))) a
where regexp_like( e.ename, a.search_regexp )
This will search the EMP table for any employees where the ENAME begins with either KIN or BOB. In the default SCOTT.EMP table, this will return just one row, the row where the ENAME is "KING"
I found another post that gave me an idea. In my specific case, the values in the input will all be 3 characters, so I can do the following:
AND SUBSTR(product_group, 0, 3) IN
(SELECT regexp_substr(strProductGroup, '[^,]+', 1, LEVEL)
FROM dual
CONNECT BY LEVEL <= length(regexp_replace(strProductGroup, '[^,]+')) + 1)
I like this solution, because it does not require additional types or functions, but is pretty limited to my specific case.

How can I populate in memory tables, in PLSQL?

I don't know if it is the correct terminology but I call "in memory tables" to the objects created like this:
create type InMemReg is object (field1 varchar2(10), field2 varchar2(20), field3 number);
create type InMemTab is table of InMemReg;
In this case my "in memory table" is "InMemTab". My question is how can I populate this kind of object, when i don't know previously the numbers of elements? I have seen in some places this type of initialization:
declare
v_uno InMemReg := InMemReg('a','b',1999);
v_dos InMemReg := InMemReg('A','Z',2000);
t_tres InMemTab := InMemTab();
begin
t_tres := InMemTab(v_uno, v_dos);
In this situation I have explicitly 2 objects before initialize "t_tres", but in a dynamic scenario where I could have n numbers of elements I don't know how to populate it.
In another OO language could be something like this:
t_tres.add(OtherObject)
The type InMemTab is a nested table in Oracle parlance.
The equivalent to the add method would be to call the extend method and then to assign OtherObject to the last position in the nested table.
SQL> ed
Wrote file afiedt.buf
1 declare
2 v_uno InMemReg := InMemReg('a','b',1999);
3 v_dos InMemReg := InMemReg('A','Z',2000);
4 t_tres InMemTab := InMemTab();
5 begin
6 t_tres.extend;
7 t_tres( t_tres.count ) := v_uno;
8 t_tres.extend;
9 t_tres( t_tres.count ) := v_dos;
10 dbms_output.put_line( 't_tres has ' || t_tres.count || ' elements.' );
11* end;
12 /
t_tres has 2 elements.
PL/SQL procedure successfully completed.
You can factor that out into an add procedure as well
SQL> ed
Wrote file afiedt.buf
1 declare
2 v_uno InMemReg := InMemReg('a','b',1999);
3 v_dos InMemReg := InMemReg('A','Z',2000);
4 t_tres InMemTab := InMemTab();
5 procedure add( p_nt IN OUT InMemTab,
6 p_elem IN InMemReg )
7 as
8 begin
9 p_nt.extend;
10 p_nt( p_nt.count ) := p_elem;
11 end;
12 begin
13 add( t_tres, v_uno );
14 add( t_tres, v_dos );
15 dbms_output.put_line( 't_tres has ' || t_tres.count || ' elements.' );
16* end;
17 /
t_tres has 2 elements.
PL/SQL procedure successfully completed.
It is common to populate the collection from the data itself, meaning you are not explicitly adding sets of strings and numbers, you're pulling the data in from other tables. Because this is a common and natural thing to do with collections, Oracle made it easy via "BULK COLLECT INTO" clause in pl/sql. For example:
DECLARE
TYPE EmployeeSet IS TABLE OF employees%ROWTYPE;
underpaid EmployeeSet;
-- Holds set of rows from EMPLOYEES table.
CURSOR c1 IS SELECT first_name, last_name FROM employees;
TYPE NameSet IS TABLE OF c1%ROWTYPE;
some_names NameSet;
-- Holds set of partial rows from EMPLOYEES table.
BEGIN
-- With one query,
-- bring all relevant data into collection of records.
SELECT * BULK COLLECT INTO underpaid FROM employees
WHERE salary < 5000 ORDER BY salary DESC;
-- Process data by examining collection or passing it to
-- eparate procedure, instead of writing loop to FETCH each row.
DBMS_OUTPUT.PUT_LINE
(underpaid.COUNT || ' people make less than 5000.');
FOR i IN underpaid.FIRST .. underpaid.LAST
LOOP
DBMS_OUTPUT.PUT_LINE
(underpaid(i).last_name || ' makes ' || underpaid(i).salary);
END LOOP;
-- You can also bring in just some of the table columns.
-- Here you get the first and last names of 10 arbitrary employees.
SELECT first_name, last_name
BULK COLLECT INTO some_names
FROM employees
WHERE ROWNUM < 11;
FOR i IN some_names.FIRST .. some_names.LAST
LOOP
DBMS_OUTPUT.PUT_LINE
('Employee = ' || some_names(i).first_name
|| ' ' || some_names(i).last_name);
END LOOP;
END;
/
You don't typically need to worry about extending or how many elements you'll have, you can usually slurp it in and then use the built in features of the collection as you like (counts, loop through, compare different collections, set operations, etc)

using long string(over 4000) in oracle query

I know that in sql varchar2 can only be around 4000.
I know that in oracle PL varchcar2 can be around 32000.
I have a varchar2 variable defined that is over 4000 characters long and I want to use it in a query. I don't want to insert the value into a table. The value is a dilimited string that I am parsing and inserting into a table with this query. This query works when the variable is less than 4000 characters long. Is there a way to make it work with up to 32000 characters?
create global temporary table t(single_element varchar(500),element_no number);
declare
--declared as 32767 but this string contains less than 4000 characters.
--This will work. If you expand the string to 32000 characters it will not work.
myvar varchar2(32767) := 'tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4^~tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testmsg4';
begin
delete from t;
insert into t
SELECT SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element, element_no
FROM (
SELECT
ilv.str,
nt.column_value AS element_no,
INSTR(ilv.str, '^~', DECODE(nt.column_value, 1, 0, 1), DECODE(nt.column_value, 1, 1, nt.column_value-1)) + 2 AS start_pos,
INSTR(ilv.str, '^~', 1, DECODE(nt.column_value, 1, 1, nt.column_value)) AS next_pos
FROM (
select '~' || myvar || '^~' as str,
(Length(myvar) - length(replace(myvar,'^~','')))/2 + 2 as no_of_elements
from dual) ilv,
TABLE(
CAST(
MULTISET(
SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.no_of_elements
) AS number_ntt )) nt
);
end;
The error I get when expanding "myvar" to 32000 characters is
can bind a LONG value only for insert into a LONG column
Is there a way I can get around this size restraint because i'm not actually inserting this value into a table, i'm just using it in the query?
Do you have to define the variable as a VARCHAR2? Could you define it as a CLOB instead?
If I change the declaration of MYVAR from a VARCHAR2(32767) to a CLOB and define the NUMBER_NTT type, your code runs for me
SQL> ed
Wrote file afiedt.buf
SP2-0161: line 2 truncated.
1 declare
2 myvar clob := 'tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3^~tcd4~#testms
<<snip>>
~tcd3~#testmsg3^~tcd4~#testmsg4';
4 begin
5 delete from t;
6 insert into t
7 SELECT SUBSTR(str, start_pos, (next_pos-start_pos)) AS single_element, elem
ent_no
8 FROM (
9 SELECT
10 ilv.str,
11 nt.column_value AS element_no,
12 INSTR(ilv.str, '^~', DECODE(nt.column_value, 1, 0, 1), DECODE
(nt.column_value, 1, 1, nt.column_value-1)) + 2 AS start_pos,
13 INSTR(ilv.str, '^~', 1, DECODE(nt.column_value, 1, 1, nt.colu
mn_value)) AS next_pos
14 FROM (
15 select '~' || myvar || '^~' as str,
16 (Length(myvar) - length(replace(myvar,'^~','')))/2 + 2 as n
o_of_elements
17 from dual) ilv,
18 TABLE(
19 CAST(
20 MULTISET(
21 SELECT ROWNUM FROM dual CONNECT BY ROWNUM < ilv.n
o_of_elements
22 ) AS number_ntt )) nt
23 );
24* end;
25 /
PL/SQL procedure successfully completed.
SQL> select count(*) from t;
COUNT(*)
----------
172
That being said, that's not how I'd parse a delimited string, particularly in PL/SQL. But it does the job.
OK, well this skirts close to the edges of your implementation bias, although remember that forall IS a bulk-binding operation, not a real loop, but have you looked at the dbms_utility.comma_to_table function?
It is an optimized internal oracle parsing function, although with some limitations as you can read about here: http://www.techiegyan.com/2009/02/17/oracle-breaking-comma-separated-string-using-dbms_utilitycomma_to_table/
You would need to replace() to make it comma-delimited, and also double-quotes-enclose if you have parsed fields that starts with numbers, special characters, contains commas, etc
But if your data will allow - it sure makes your code look cleaner (and will likely work much faster too)
declare
myvar varchar2(32000) := 'tcd1~#testmsg1^~tcd2~#testmsg2^~tcd3~#testmsg3';
mycnt binary_integer;
myresults sys.dbms_utility.lname_array;
begin
sys.dbms_utility.comma_to_table('"'||replace(myvar,'^~','","')||'"', mycnt, myresults );
delete from t;
forall ix in myresults.first..myresults.last
insert into tvalues (myresults(ix));
commit;
end;

Resources