update query with OR condition in PL/sql - plsql

I have a update query in PL/SQL where I need to use OR condition based on itemsetid='XXXX or orgid ='YYYYY' this is because not all tables have these 2 fields so I need to use OR condition. I tried as below but it's not working ,
set serveroutput on size unlimited ;
declare
type item_type
is record (
maxSameas maxattribute.sameasattribute%TYPE,
maxTable maxattribute.objectname%TYPE,
maxAttrib maxattribute.attributename%TYPE
);
Type attribArray is table of item_type;
allAttribs attribArray;
cursor ITEM_ATTRIB_CURSOR is
select a.sameasattribute, a.objectname, a.attributename
from maxattribute a, maxobject b
where a.persistent = 1
and a.objectname = b.objectname
and b.persistent = 1
and ((a.sameasattribute is not null
and a.sameasattribute like 'ITEMNUM')
or (a.attributename = 'ITEMNUM'))
and a.objectname <> 'ITEMHIST'
-- and a.objectname <> 'ITEM'
and b.isView = '0'
order by a.objectname asc, a.attributename asc, a.sameasattribute desc;
type itemXrefType
is record (
currValue itemhist.ITEMNUM%type,
oldValue itemhist.OLDITEMNUM%type
);
type itemXrefTable is table of itemXrefType;
itemXref itemXrefTable;
cursor ITEM_VAL_CURSOR is
select itemnum, olditemnum
from itemhist
where olditemnum is not null and itemsetid='XXXXX';
updateStr varchar2 (4000);
queryStr varchar2 (4000);
tableName varchar2 (30);
attribName varchar2(50);
rowKount NUMBER;
begin
DBMS_OUTPUT.ENABLE(NULL);
-- Fetch Cross Reference Data
open item_val_cursor;
fetch item_val_cursor bulk collect into itemXref;
close item_val_cursor;
-- Fetch all Objects with ITEMNUM attribute
open ITEM_ATTRIB_CURSOR;
fetch ITEM_ATTRIB_CURSOR bulk collect into allAttribs;
close ITEM_ATTRIB_CURSOR;
-- Loop through every Object
for i in allAttribs.first..allAttribs.last loop
tableName := allAttribs(i).maxTable;
if (tableName = 'ITEM') then
attribName := 'ITEMNUM';
else
attribName := allAttribs(i).maxAttrib;
end if;
for j in itemXref.first .. itemXref.last loop
-- For each Item Num, update all objects
queryStr := 'select count (1) from ' || tableName ||
' where ' || attribName || '=' || '''' || itemXref(j).oldValue || '''';
-- Get Count before update
EXECUTE IMMEDIATE queryStr into RowKount;
updateStr := 'update ' || tableName ||
' set ' || attribName || ' = ' || '''' || itemXref(j).currValue
|| ''' where ' || attribName || '=' || '''' || itemXref(j).oldValue || ''' and (itemsetid = ''' || 'XXXX' || ''' or orgid = ''' || 'YYYYY' || ''' ) ''' '''';
--dbms_output.put_line (itemXref(j).currValue || ' ::::' || itemXref(j).oldValue);
dbms_output.put_line (updateStr || ':: Records to be updated is ' || rowKount);
-- Execute the Update
EXECUTE IMMEDIATE updateStr;
-- Commit changes
updateStr := 'Commit';
EXECUTE IMMEDIATE updateStr;
-- Get count after update - should be none!
EXECUTE IMMEDIATE queryStr into RowKount;
dbms_output.put_line (' Count of records after the update is ' || rowKount);
end loop;
end loop; --for i in allAttribs
end;
Thanks in advance!

Related

Dynamic Cursor with parameterised schema name and using for BULK INSERT in target table

I have source_table in different 22 schemas and need procedure to create for bulk collect and insert into same target table in oracle stored procedure.
I'm trying and not getting records inserted getting error ORA-00911: invalid character but there is all column from select cursor and traget_table are same in order.
CREATE OR REPLACE PROCEDURE proc_bulk_circle(p_limit IN PLS_INTEGER DEFAULT 10000,
p_activity_date IN DATE,
p_circle IN VARCHAR2) AS
CURSOR act_cur IS
SELECT activity_date,
circle
FROM circle_load_control
WHERE activity_date = p_activity_date
AND circle = circle;
TYPE type_i6 IS TABLE OF act_cur%ROWTYPE INDEX BY BINARY_INTEGER;
i_tab6 type_i6;
v_count NUMBER := 0;
lv_circle VARCHAR2(2);
lv_schema VARCHAR2(20);
TYPE rc IS REF CURSOR;
con_sap_cur rc;
TYPE con_sap_resp IS TABLE OF target_table%ROWTYPE INDEX BY BINARY_INTEGER;
i_tab1 con_sap_resp;
lv_sql_stmt VARCHAR2(32767);
BEGIN
IF p_circle = 'MUM'
THEN
lv_circle := 'MU';
lv_schema := 'MUMBAI';
ELSIF p_circle = 'MAH'
THEN
lv_circle := 'MH';
lv_schema := 'MHRSTR';
ELSE
lv_circle := NULL;
END IF;
FOR myindex IN act_cur
LOOP
i_tab6(v_count) := myindex;
v_count := v_count + 1;
END LOOP;
FOR myindex IN i_tab6.first .. i_tab6.last
LOOP
IF i_tab6(myindex).activity_date = p_activity_date
AND i_tab6(myindex).circle = p_circle
THEN
BEGIN
lv_sql_stmt := 'SELECT acc_id code,
cust_id c_id,
addr_1 address2,
addr_2 address3,
addr_3 address4,
(SELECT SUM(abc) FROM ' || lv_schema || '.details WHERE <some condition with t1> GROUP BY <columns>) main_charges,
(SELECT SUM(extra_charge) FROM ' || lv_schema || '.details WHERE <some condition with t1> GROUP BY <columns>) extra_charges
FROM ' || lv_schema || '.main_source_details t1
WHERE t1.activity_date = ''' || p_activity_date || ''';';
OPEN con_sap_cur FOR lv_sql_stmt;
LOOP
FETCH con_sap_cur BULK COLLECT
INTO i_tab1 LIMIT p_limit;
FORALL i IN 1 .. i_tab1.count
INSERT INTO target_table (column list....)
VALUES(I_TAB1(i).col1,......;
EXIT WHEN con_sap_cur%NOTFOUND;
END LOOP;
COMMIT;
CLOSE con_sap_cur;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('ERR target_table: ' || SQLCODE || '-' || SQLERRM);
END;
ELSE
dbms_output.put_line(p_activity_date || ' DATE IS NOT MATCH');
END IF;
END LOOP;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line(SQLCODE || ' ' || SQLERRM);
END proc_bulk_circle;
/
I believe this comes down to you having a ; in your definition of the sql (see below line)
WHERE t1.activity_date = ''' || p_activity_date || ''';';
when you are defining SQL for dynamic use (and opening a cursor this way is dynamic) you do not include the ;
To show this I have done a shorter example. The below will error in the same way as yours.
declare
v_sql varchar2(100) default 'select ''X'' from dual;';
TYPE rc IS REF CURSOR;
v_cur rc;
type l_tab_type is table of varchar2(1);
l_tab l_tab_type;
begin
open v_cur for v_sql;
loop
fetch v_cur bulk collect into l_tab;
exit;
end loop;
CLOSE v_cur;
end;
/
but simply remove the ; from the line
v_sql varchar2(100) default 'select ''X'' from dual;';
end it all works fine, fixed example below.
declare
v_sql varchar2(100) default 'select ''X'' from dual';
TYPE rc IS REF CURSOR;
v_cur rc;
type l_tab_type is table of varchar2(1);
l_tab l_tab_type;
begin
open v_cur for v_sql;
loop
fetch v_cur bulk collect into l_tab;
exit;
end loop;
CLOSE v_cur;
end;
/
You're doing an awful lot of work here, if your purpose is to insert some rows.
Instead, you could do the insert and select in one go, something like:
CREATE OR REPLACE PROCEDURE proc_bulk_circle(p_activity_date IN DATE,
p_circle IN VARCHAR2) AS
lv_circle VARCHAR2(2);
lv_schema VARCHAR2(20);
v_query CLOB;
e_table_does_not_exist EXCEPTION;
PRAGMA EXCEPTION_INIT(e_table_does_not_exist, -00942);
BEGIN
IF p_circle = 'MUM'
THEN
lv_circle := 'MU';
lv_schema := 'MUMBAI';
ELSIF p_circle = 'MAH'
THEN
lv_circle := 'MH';
lv_schema := 'MHRSTR';
END IF;
IF lv_schema IS NOT NULL
THEN
-- asserting the schema name to avoid sql injection
-- also using a bind variable for the activity_daate predicates
v_query := 'INSERT INTO target_table (<column list>)' || CHR(10) ||
' WITH main_dets AS (SELECT acc_id,' || CHR(10) ||
' cust_id,' || CHR(10) ||
' addr_1,' || CHR(10) ||
' addr_2,' || CHR(10) ||
' addr_3,' || CHR(10) ||
' (SELECT SUM(abc) FROM ' || dbms_assert.simple_sql_name(lv_schema) || '.details WHERE <some condition with t1>) main_charges,' || CHR(10) || -- no need for the group by
' (SELECT SUM(extra_charge) FROM ' || dbms_assert.simple_sql_name(lv_schema) || '.details WHERE <some condition with t1>) extra_charges' || CHR(10) || -- no need for the group by
' FROM ' || dbms_assert.simple_sql_name(lv_schema) || '.main_source_details t1' || CHR(10) ||
' WHERE activity_date = :p_activity_date)' || CHR(10) ||
' circles AS (SELECT activity_date,' || CHR(10) ||
' circle' || CHR(10) ||
' FROM circle_load_control' || CHR(10) ||
' WHERE activity_date = :p_activity_date' || CHR(10) ||
' AND circle = circle)' || CHR(10) || -- did you really mean circle = circle here? This is equivalent to 1=1 (unless circle is null) and is therefore pretty irrelevant! If you want to exclude rows with null values, use "circle is not null" instead
' SELECT md.acc_id,' || CHR(10) ||
' md.cust_id,' || CHR(10) ||
' md.addr_1,' || CHR(10) ||
' md.addr_2,' || CHR(10) ||
' md.addr_3,' || CHR(10) ||
' md.main_charges,' || CHR(10) ||
' md.extra_charges' || CHR(10) ||
' FROM main_dets md' || CHR(10) ||
' CROSS JOIN circles c';
EXECUTE v_query USING p_activity_date, p_activity_date;
COMMIT;
ELSE
raise_application_error(-20001, 'Invalid circle specified: "' || p_circle || '"');
END IF;
END proc_bulk_circle;
/
(N.B. untested.)
I've assumed that activity_date and circle in circle_load_control aren't unique; if they are, you could avoid the cross join and just have an implicit cursor to fetch the row prior to doing the IF p_circle = ... checks.

CREATE OR REPLACE PROCEDURE proc_video_search

Create a procedure called proc_video_search to search for a video and display the name, copy ID, format, and status of the video’s copies. In addition, the checkout dates and due dates are also displayed for unreturned copies. The damaged copies (Status = 'D') are excluded in your output. Sort your output by the video name (Name) and then the copy ID (CopyID).
$ CREATE OR REPLACE PROCEDURE proc_video_search (
p_VideoName VARCHAR2,
p_FormatName VARCHAR2 DEFAULT NULL) as
v_Count NUMBER;
v_TotalCopies NUMBER; v_Avalb NUMBER;v_FormatName VARCHAR2(100);
v_VideoName VARCHAR2(100); v_CopyID VARCHAR2(100);v_DueDate DATE;
v_Status VARCHAR2(100); v_CheckoutDate DATE;
CURSOR asdf IS
SELECT T_VIDEO.Name, T_COPY.CopyID, Status,T_VIDEO_FORMAT.NAME
FROM T_VIDEO
INNER JOIN T_COPY ON T_VIDEO.VideoID = T_COPY.VideoID
INNER JOIN T_VIDEO_FORMAT ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status !='D' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) || '%'
OR UPPER(T_VIDEO_FORMAT.NAME)= UPPER(p_FormatName)
ORDER BY T_VIDEO.Name, T_COPY.CopyID;
BEGIN
SELECT COUNT(*)
INTO v_Count
FROM T_VIDEO
WHERE UPPER(T_VIDEO.Name) like '%' || UPPER(p_VideoName) || '%' ;
IF v_count = 0 THEN
DBMS_OUTPUT.PUT_LINE('**** '||v_Count|| ' results found for ' ||
p_VideoName||'. *****');
RETURN;
END IF;
SELECT count(T_COPY.CopyID) INTO v_TotalCopies
FROM T_COPY INNER JOIN T_VIDEO ON T_COPY.VideoID = T_VIDEO.VideoID
INNER JOIN T_VIDEO_FORMA ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status !='D' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) ||'%'
OR UPPER(T_VIDEO_FORMAT.NAME)=UPPER(p_FormatName);
SELECT count(T_COPY.CopyID)INTO v_Avalb FROM T_COPY
INNER JOIN T_VIDEO ON T_COPY.VideoID = T_VIDEO.VideoID
INNER JOIN T_VIDEO_FORMAT ON T_VIDEO_FORMAT.FormatID =
T_VIDEO.FormatID
WHERE Status ='A' AND UPPER(T_VIDEO.Name) like '%' ||
UPPER(p_VideoName) ||'%'
OR UPPER(T_VIDEO_FORMAT.NAME)=UPPER(p_FormatName);
IF v_TotalCopies >=0 THEN
IF p_FormatName IS NULL THEN
DBMS_OUTPUT.PUT_LINE(v_TotalCopies||' results found for '||
p_VideoName||' . (Available copies:'|| v_Avalb|| ')' );
ELSE
DBMS_OUTPUT.PUT_LINE(v_TotalCopies||' results found for '||
p_VideoName||'('|| p_FormatName||') . (Available copies:'||
v_Avalb|| ')' );
end if;
OPEN asdf;
LOOP
FETCH asdf INTO v_VideoName, v_CopyID, v_Status,
v_FormatName ; exit when asdf%NOTFOUND ;
SELECT COUNT(CheckoutDate)
INTO v_Count FROM T_RENTAL WHERE CopyID = v_CopyID;
IF v_Count = 1 THEN
SELECT CheckoutDate,DueDate
INTO v_CheckoutDate,v_DueDate
FROM T_RENTAL
WHERE CopyID = v_CopyID;
end if;
DBMS_OUTPUT.PUT_LINE(RPAD('-', 53, '-'));
DBMS_OUTPUT.PUT_LINE(RPAD('Name:',30) || RPAD(v_VideoName,15));
DBMS_OUTPUT.PUT_LINE(RPAD('CopyID:',30) || RPAD(v_CopyID,15));
DBMS_OUTPUT.PUT_LINE(RPAD('Format:',30) ||
RPAD(v_FormatName,15));
IF v_Status = 'A' THEN v_Status := 'Available';END IF;
IF v_Status = 'R' THEN v_Status := 'Rented'; END IF;
DBMS_OUTPUT.PUT_LINE(RPAD('Status:',30) || RPAD(v_Status,15));
IF v_Status ='Available' THEN
DBMS_OUTPUT.PUT_LINE(RPAD('CheckoutDate:',30)
||'****************************');
DBMS_OUTPUT.PUT_LINE(RPAD('DueDate:',30)
||'****************************');
ELSE
DBMS_OUTPUT.PUT_LINE(RPAD('CheckoutDate:',30)
||RPAD(TO_CHAR(v_CheckoutDate, 'DD-MON-YYYY'),15));
DBMS_OUTPUT.PUT_LINE(RPAD('DueDate:',30) ||RPAD(TO_CHAR(
v_DueDate, 'DD-MON-YYYY'),15));
END IF; END LOOP; CLOSE asdf; END IF; END proc_video_search ;
EXEC proc_video_search('ANOTHER', 'DVD')`
enter image description here
The problem is with this line of your cursor asdf
OR UPPER(T_VIDEO_FORMAT.NAME)= UPPER(p_FormatName)
Because it says OR, the query can choose to ignore this criteria if it evaluates to false. That is why you are getting results with all the formats; it ignores the filtering.
You have to wrap the OR statement in another AND clause, like so
WHERE Status !='D'
AND UPPER(T_VIDEO.Name) like '%' || UPPER(p_VideoName) || '%'
AND ( /* OR clause here */ )
And then you can handle the case of p_formatName being null or not.

create a procedure with cursor as parameter

I wrote this code and it works ok:
declare
cursor c_emp is
select last_name, first_name from employees;
type c_list is table of employees.last_name%type index by binary_integer;
type c_list2 is table of employees.first_name%type index by binary_integer;
last_list c_list;
first_list c_list2;
counter integer := 0;
begin
for i in c_emp loop
counter := counter + 1;
last_list(counter) := i.last_name;
first_list(counter) := i.first_name;
dbms_output.put_line('Employee(' || counter || '): ' || last_list(counter) || ' ' || first_list(counter));
end loop;
end;
/
This time I am trying to make procedure with parameters that I can insert the table name and column into the cursor. And I have tried with this :
create or replace procedure show_data(tab_name in varchar2, data_list in varchar2)
is
str varchar2(100);
str2 varchar2(100);
column_name varchar2(100);
begin
str := 'select ' || data_list || ' from ' || tab_name;
for vRec in str loop
dbms_output.put_line(str);
end loop;
end;
/
It gave a error which the str is not a cursor. I am not sure that if cursor can be done in this way, but from the error it seems it can't.
Which part of my code is wrong, or because I didn't declare my cursor? But if i declare my cursor, I can't get the parameter by using dynamic sql way.
The below should produce the same result as your original PL/SQL block. Note that the table_name can be dynamic and so can the data_list but you need to know the column names in the data_list to be able to fetch from the cursor and then print them.
create or replace procedure show_data(tab_name in varchar2
, data_list in varchar2)
is
str varchar2(100);
str2 varchar2(100);
column_name varchar2(100);
TYPE cursor_ref IS REF CURSOR;
vRec_cursor cursor_ref;
counter integer = 0;
last_name employee.last_name%TYPE;
first_name employee.first_name%TYPE;
begin
str := 'select ' || data_list || ' from ' || tab_name;
open vRec_cursor for str;
loop
FETCH vRec_cursor INTO last_name, first_name;
EXIT WHEN vRec_cursor%NOTFOUND;
dbms_output.put_line('Employee(' || counter || '): ' || last_name || ' ' || first_name;
counter = counter + 1;
end loop;
end;
/
NOTE: Haven't run the above code yet

Looping with If condition in PL/SQL

I created this block to test three conditions from a table called process. I created the sql query based on the requirements and I inserted it into a FOR LOOP that should return more than a value every time I test a date which matches the date in the process table. My problems with this block is -First: When I test invalid date against the DATE_PO records, I get the ELSE condition, but this starts to loop twice for the number of records in the table process:
anonymous block completed
No records made in this date !
No records made in this date !
The second problem, if no data in this table, how do I make an appropriate condition to test and display a message to the user of no records found?
Table process
NUM DATE_PO STATUS
---------- ----------- -----------
1243 21-DEC-15 CANCEL
5678 21-DEC-15 APPROVE
BEGIN
FOR X IN
(SELECT PO.STATUS,
PO.DATE_PO,
SUM(D.QUANTIY) QUANTITY_SUM,
SUM(NVL(D.QUANTIY,0) * NVL (M.COST,0)) T_VALUE
FROM PROCESS.PO, DELETE_PROCESS, MONEY M
WHERE D.PO = M.PO
AND PO.NUM = D.NUM
GROUP BY PO.STATUS, PO.DATE_PO)
LOOP
IF TO_CHAR(X.DATE_PO, 'mm-yyyy') ='10-2015' THEN --Not valid date
IF X.STATUS ='CANCEL' THEN
DBMS_OUTPUT.PUT_LINE('CANCELLATIONS: ' || X.QUANTITY_SUM );
DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
ELSIF X.STATUS ='HOLD' THEN
DBMS_OUTPUT.PUT_LINE('HOLDS: ' || X.QUANTITY_SUM );
DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
ELSIF X.STATUS = 'APPROVE' THEN
DBMS_OUTPUT.PUT_LINE('APPROVES: ' || X.QUANTITY_SUM );
DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('No records made in this date !');
END IF;
END LOOP;
END;
/
Try this. Hope it helps. Though its not the best solution as it can be
done without using ROW-BY-ROW processing. Bulk COLLECT option can also
be used. Let me know if this helps. Since i dont have workspace please
pardon any syntax erros if any.
DECLARE
lv_cnt PLS_INTEGER;
lv_date VARCHAR2(10):='12-2015';
lv_dt DATE;
BEGIN
BEGIN
SELECT TO_DATE(lv_date,'MM-YYYY') INTO lv_dt FROM DUAL;
EXCEPTION
WHEN OTHERS THEN
dbms_output.put_line('Not a valid Date');
RETURN;
END;
SELECT COUNT(1)
INTO lv_cnt
FROM
(SELECT PO.STATUS,
PO.DATE_PO,
SUM(D.QUANTIY) QUANTITY_SUM,
SUM(NVL(D.QUANTIY,0) * NVL (M.COST,0)) T_VALUE
FROM PROCESS.PO,
DELETE_PROCESS,
MONEY M
WHERE D.PO = M.PO
AND PO.NUM = D.NUM
GROUP BY PO.STATUS,
PO.DATE_PO
)A;
IF lv_cnt > 0 THEN
FOR X IN
(SELECT PO.STATUS,
PO.DATE_PO,
SUM(D.QUANTIY) QUANTITY_SUM,
SUM(NVL(D.QUANTIY,0) * NVL (M.COST,0)) T_VALUE
FROM PROCESS.PO,
DELETE_PROCESS,
MONEY M
WHERE D.PO = M.PO
AND PO.NUM = D.NUM
GROUP BY PO.STATUS,
PO.DATE_PO
)
LOOP
IF X.STATUS ='CANCEL' THEN
DBMS_OUTPUT.PUT_LINE('CANCELLATIONS: ' || X.QUANTITY_SUM );
DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
ELSIF X.STATUS ='HOLD' THEN
DBMS_OUTPUT.PUT_LINE('HOLDS: ' || X.QUANTITY_SUM );
DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
ELSIF X.STATUS = 'APPROVE' THEN
DBMS_OUTPUT.PUT_LINE('APPROVES: ' || X.QUANTITY_SUM );
DBMS_OUTPUT.PUT_LINE('VALUE: '|| X.T_VALUE);
DBMS_OUTPUT.PUT_LINE('DATE: ' || X.DATE_PO );
END IF;
END LOOP;
ELSIF lv_cnt = 0 THEN
dbms_output.put_line('No records found for the user');
END IF;
END;

Search specific value in all field in oracle table

I want to search some keyword in table but I don't know to which column it is belonging to. I have got one of query for that as follows:
variable val varchar2(10)
exec :val := 'KING'
PL/SQL procedure successfully completed.
SELECT DISTINCT SUBSTR (:val, 1, 11) "Searchword",
SUBSTR (table_name, 1, 14) "Table",
SUBSTR (column_name, 1, 14) "Column" FROM cols,
TABLE (xmlsequence (dbms_xmlgen.getxmltype ('select '
|| column_name
|| ' from '
|| table_name
|| ' where upper('
|| column_name
|| ') like upper(''%'
|| :val
|| '%'')' ).extract ('ROWSET/ROW/*') ) ) t
ORDER BY "Table"
Searchword Table Column
KING EMP ENAME
but I am not getting appropriate output.I only got output as:
PL/SQL procedure successfully completed. I have tried but I didn't get satisfactory answer. Can anybody please help..?
The easiest query I can write for such scope is something like:
SELECT *
FROM <table>
WHERE UPPER(column1) LIKE UPPER('%' || :val || '%')
OR UPPER(column2) LIKE UPPER('%' || :val || '%')
OR UPPER(column3) LIKE UPPER('%' || :val || '%')
OR UPPER(column4) LIKE UPPER('%' || :val || '%');
In this query I search for value :val in all columns of the table using OR conditions, so if at least one column contains the value the row is fetched
If you have many columns you can write a query that builds the final query for you, like the following:
SELECT 'SELECT * FROM <table> WHERE ' || LISTAGG(column_name || ' LIKE ''%' || :val || '%''', ' OR ') WITHIN GROUP (ORDER BY column_name)
FROM dba_tab_columns
WHERE table_name = '<table>'
The result of this query is the query to execute. Note that Oracle has a limit of 4000 characters for a string field built in a query. If your where condition is too big the query will fail.
In this case, the only alternative is to write a stored procedure that builds the query and returns it in a CLOB variable, here's an example:
CREATE OR REPLACE FUNCTION build_query(in_table_name IN VARCHAR2, in_search IN VARCHAR2) RETURN `CLOB` IS
lc_query CLOB := 'SELECT * FROM ' || in_table_name || ' WHERE 1=0';
BEGIN
FOR c IN (
SELECT *
FROM user_tab_columns
WHERE table_name = in_table_name
ORDER BY column_name
) LOOP
lc_query := lc_query || ' OR ' || c.column_name || ' LIKE ''%' || in_search || '%''';
END LOOP;
RETURN lc_query;
END;
This function will works and generates strings longer than 4000 characters.

Resources