Home » SQL & PL/SQL » SQL & PL/SQL » find value in table (don't know column name yet) (Oracle 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production)
icon5.gif  find value in table (don't know column name yet) [message #624001] Wed, 17 September 2014 03:37 Go to next message
ecivgamer
Messages: 147
Registered: May 2011
Location: Ukraine
Senior Member
Hi all,

My need is to find value in table (don't know column name yet).

Trying to use column list and loop.
Honestly I'm afraid I can't write proper code to find COLUMN VALUE (2581365), because my code works fine only to find COLUMN NAME (dealid).
Please help me to find COLUMN VALUE.



declare
 
 
l_cnt number;
 
 
begin
for i in ( 
            select column_name from ALL_TAB_COLUMNS where owner = 'CREATOR' AND TABLE_NAME LIKE UPPER('%dbt_reserve%')
         )
loop
select count(1) into l_cnt
From CREATOR.dbt_reserve t
where arcdate between date '2013-09-30' - 60 and date '2013-09-30' + 60
and ISAPPROVED=1
and upper(i.column_name) = upper('dealid'); --to_char(2581365) ;
if l_cnt > 0 then
raise_application_error(-20001, l_cnt || ' ' || i.column_name);
end if;
end loop;
end;
Re: find value in table (don't know column name yet) [message #624005 is a reply to message #624001] Wed, 17 September 2014 03:53 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
For example, let's say I want to know the TABLE_NAME and COLUMN_NAME having the value 'SCOTT'. My test case is a simple one, I know SCOTT is an employee name in EMP table in ENAME column of standard SCOTT schema.

SQL> set serveroutput on;

SQL> DECLARE
  2      lcount NUMBER;
  3      lquery VARCHAR2(200);
  4  BEGIN
  5      FOR data IN (SELECT *
  6                   FROM   user_tab_columns) LOOP
  7          lquery := 'select count(*) from '
  8                    ||data.table_name
  9                    ||' where '
 10                    ||data.column_name
 11                    ||' like ''%SCOTT%''';
 12
 13          EXECUTE IMMEDIATE lquery INTO lcount;
 14
 15          IF lcount > 0 THEN
 16            dbms_output.Put_line(data.column_name
 17                                 ||'-----'
 18                                 ||data.table_name);
 19          END IF;
 20      END LOOP;
 21  END;
 22  /
ENAME-----EMP

PL/SQL procedure successfully completed.

SQL>


I believe, since you are on 11g, there should be better approach using XML solutions.


Regards,
Lalit
Re: find value in table (don't know column name yet) [message #624008 is a reply to message #624001] Wed, 17 September 2014 03:57 Go to previous messageGo to next message
jgjeetu
Messages: 373
Registered: July 2013
Location: www.Orafaq.com/Forum
Senior Member

Do one thing , select * from table by concatenating all column names and give that an alias name .
and use a condition 'where aliasname like '%value%'
for ex.

i want to find value 10 in dept table.
then the query will be

SELECT *
  FROM (SELECT deptno ||'-'|| dname ||'-'|| loc VALUE
          FROM dept)
 WHERE VALUE LIKE '%10%';


maybe this will work for you.
thanks

[Updated on: Wed, 17 September 2014 04:00]

Report message to a moderator

Re: find value in table (don't know column name yet) [message #624011 is a reply to message #624008] Wed, 17 September 2014 04:08 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
jgjeetu wrote on Wed, 17 September 2014 14:27
Do one thing , select * from table by concatenating all column names and give that an alias name .
and use a condition 'where aliasname like '%value%'


*_TAB_COLUMNS does it for you.
Re: find value in table (don't know column name yet) [message #624015 is a reply to message #624001] Wed, 17 September 2014 04:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What an awfully written code!
If any developer gives me such one he will get fire in the second.
Learn how to write readable and maintainable code; this should be your very first task.

Re: find value in table (don't know column name yet) [message #624023 is a reply to message #624015] Wed, 17 September 2014 05:19 Go to previous messageGo to next message
ecivgamer
Messages: 147
Registered: May 2011
Location: Ukraine
Senior Member
Finally, I use this code, it works just fine:

set serveroutput on;

DECLARE
    lcount NUMBER;
    lquery VARCHAR2(200);
BEGIN
    FOR data IN (
                 SELECT * FROM   ALL_TAB_COLUMNS
                 where TABLE_NAME LIKE UPPER('dbt_reserve') 
                   and COLUMN_NAME NOT IN ('MFDEALID')
                ) 
     LOOP
        lquery := 'select count(*) from ' --creator.dbt_reserve'
                  ||data.owner
                  ||'.'  
                  ||data.table_name
                  ||' where ISAPPROVED = 1 AND ARCDATE = DATE ''2013-09-30'' AND  '
                  ||'(' 
                  || data.column_name
                  ||' like ''127638'''
                  ||' OR ' 
                  || data.column_name
                  ||' like ''127639'''
                  ||')' ;

         EXECUTE IMMEDIATE lquery INTO lcount;

          IF lcount > 0 THEN
             RAISE_APPLICATION_ERROR(-20001, LCOUNT || '----' || DATA.COLUMN_NAME || '-----------' || DATA.TABLE_NAME);
--            dbms_output.Put_line(data.column_name
--                                 ||'-----'
--                                 ||data.table_name);
          END IF;
     END LOOP;
END;


Big thanks!

[Updated on: Wed, 17 September 2014 05:23]

Report message to a moderator

Re: find value in table (don't know column name yet) [message #624041 is a reply to message #624001] Wed, 17 September 2014 07:01 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
Quote:

AND TABLE_NAME LIKE UPPER('%dbt_reserve%')


Why are you applying an UPPER function to a literal? Why not just supply the literal in upper-case?
Why are you doing a wild-card search for a table name? What do you do if you have more than one table that meets the wild-card search criteria? Esepcially given that later you select from the very specific table CREATOR.DBT_RESERVE.

Quote:

and upper(i.column_name) = upper('dealid')


Again, why the UPPER functions? The column name *should* be uppper case, and the literal -- well, you can give an upper case literal just as easily as a lower-case, and so eliminate the need of the function.
Re: find value in table (don't know column name yet) [message #624049 is a reply to message #624001] Wed, 17 September 2014 07:46 Go to previous messageGo to next message
EdStevens
Messages: 1376
Registered: September 2013
Senior Member
https://community.oracle.com/thread/3609011
Re: find value in table (don't know column name yet) [message #624052 is a reply to message #624049] Wed, 17 September 2014 07:53 Go to previous messageGo to next message
ecivgamer
Messages: 147
Registered: May 2011
Location: Ukraine
Senior Member
Shame on me for poor design, wrong use of upper function, etc.
I needed to find value at test oracle instance and this mission is complete.
Sorry for cross-posting.
Re: find value in table (don't know column name yet) [message #624069 is a reply to message #624052] Wed, 17 September 2014 08:52 Go to previous message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
ecivgamer wrote on Wed, 17 September 2014 18:23

Sorry for cross-posting.

Nobody would scold you for cross-posting in two different forums, it is bad when you do on the same forum. But it would be good to see the feedback when you get your answer Smile
Previous Topic: Index not used when querying views
Next Topic: TO_NUMBER conversion on NVARCHAR2 column throws ORA-01722: invalid number
Goto Forum:
  


Current Time: Thu Apr 25 10:54:52 CDT 2024