Search a value in all tables in a connection (Sql developer) - sql

I found many such questions but the answers where all using Stored Procedures.
I want an answer that uses purely a query in Oracle Sql Developer.
I have a value 'CORE_AO0001031_70_EMail_1' but not sure in which table. The number of tables and the data inside them are very huge.
Doesn't matter if the query is huge and will take time to execute. Is there any such query?
The reason for my asking a query is, I don't have privilege to create a Stored Procedure and I won't be given that privilege. Please help me with a query.

With an SQL you can't, as the queries are going to be dynamic. You have to execute a PL/SQL atleast.
Note: This is a Costly operation!
You can still attempt a full download of all you tables as spooling, and make PERL search into all files. In that case, you need a lot of disk space, but less harm(Just better than the Pl/SQL) to the database
DECLARE
TYPE TY_TABLE_NAMES IS TABLE OF VARCHAR2(30);
L_TABLE_NAMES TY_TABLE_NAMES;
TYPE TY_COLUMN_NAMES IS TABLE OF VARCHAR2(30);
L_COLUMN_NAMES TY_COLUMN_NAMES;
v_SCHEMA_NAME VARCHAR2(30) = 'SYSTEM'; --Your Schema Name
v_QUERY_STRING VARCHAR2(4000);
v_SEARCH_STRING VARCHAR2(4000) := 'CORE_AO0001031_70_EMail_1';
v_SEARCH_FLAG CHAR(1) := 'N';
BEGIN
SELECT ALL_TABLES
BULK COLLECT INTO L_TABLE_NAMES
WHERE OWNER = v_SCHEMA_NAME;
FOR I In 1..L_TABLE_NAMES.COUNT LOOP
SELECT COLUMN_NAME
BULK COLLECT INTO L_COLUMN_NAMES
FROM ALL_TAB_COLUMNS
WHERE TBALE_NAME = L_TABLE_NAMES(I)
AND OWNER = v_SCHEMA_NAME;
FOR J In 1..L_COLUMN_NAMES.COUNT LOOP
BEGIN
v_QUERY_STRING := 'SELECT ''Y'' FROM DUAL WHERE EXISTS (SELECT ''X'' FROM '||L_TABLE_NAMES(I)||' WHERE '||
||L_COLUMN_NAMES(J)|| ' LIKE ''%'|| v_SEARCH_STRING||'%'')';
EXCECUTE IMMEDIATE v_QUERY_STRING INTO v_SEARCH_FLAG;
WHEN NO_DATA_FOUND THEN
v_SEARCH_FLAG := 'N';
END;
IF(v_SEARCH_FLAG = 'Y') THEN
DBMS_OUTPUT.PUT_LINE(v_SEARCH_STRING || ' found in column '||L_COLUMN_NAMES(I)|| ' of table '||L_TABLE_NAMES(I));
BREAK;
END IF;
END LOOP;
IF(v_SEARCH_FLAG = 'Y') THEN
DBMS_OUTPUT.PUT_LINE('Done Searching!');
BREAK;
END IF;
END LOOP;
END;
/

Related

Oracle sql developer| export from query to a table

Good day,
In DBeaver db manager we are able to transfer data directly from query to a table created by us. We right click>execute>export to a table.
Can anyone help me to do the same in oracle developer environment.
Here I have created a table in the same connection,and want to store query results into table.
Thank you
Right click on your table.
Choose Table - Copy.
Give it a name, and check 'Include Data'
You can see the code we're about to run by clicking on the SQL panel.
declare
l_sql varchar2(32767);
c_tab_comment varchar2(32767);
procedure run(p_sql varchar2) as
begin
execute immediate p_sql;
end;
begin
run('create table "HR".BEERS_COPY as select * from "HR"."BEERS" where '||11||' = 11');
begin
select comments into c_tab_comment from sys.all_TAB_comments where owner = 'HR' and table_name = 'BEERS' and comments is not null;
run('comment on table HR.BEERS_COPY is '||''''||REPLACE(c_tab_comment, q'[']', q'['']')||'''');
for tc in (select column_name from sys.all_tab_cols where owner = 'HR' and table_name = 'BEERS')
loop
for c in (select comments from sys.all_col_comments where owner = 'HR' and table_name = 'BEERS' and column_name=tc.column_name)
loop
run ('comment on column HR.BEERS_COPY.'||tc.column_name||' is '||''''||REPLACE(c.comments, q'[']', q'['']')||'''');
end loop;
end loop;
EXCEPTION
WHEN OTHERS THEN NULL;
end;
end;
So basically a CTAS + we grab the comments for you.
Disclaimer: I'm the product manager for SQL Developer and a Oracle employee.

How to find to which schema or procedure, table and column a particular value belongs to?

I have a 9 digit number, say "234234234", is there a way to find or check its appearance in my database, like in which particular schema or procedure does it fall? and list out all the tables and columns which has that value in pl/sql developer
This query only searches in stored objects that user is allowed to access (procedure, function, package, ...). You could refer to this
Not sure if there is one way to search for that value in all database table.
SELECT *
FROM all_source
WHERE text LIKE '%234234234%';
--AND owner = 'SCHEMA_NAME';
The below block identifies the given string's presence across all the tables in your DB.
declare
num_rows number;
sql_text varchar2(250);
sql_info varchar2(100);
begin
dbms_output.enable(1000000);
for x in (select table_name, column_name from dba_tab_columns
where data_type in ('VARCHAR','VARCHAR2','CHAR')
and owner <> 'SYSTEM')
loop
sql_text:='select count(*) into :num_rows from SYSTEM.'||x.table_name||' where '||x.column_name||' like ''%234234234%''';
-- dbms_output.put_line (sql_text);
execute immediate sql_text into num_rows;
if num_rows>0
then
sql_info:='Table: '||x.table_name||' contains the string';
dbms_output.put_line (sql_info);
end if;
end loop;
end;
/

Check table access for Oracle SQL statements in BLOB

I have a table with a column containing SQL query (select statements only) in BLOB format.
I'm trying to figure out a way to analyse the SQL queries, and check which tables each SQL queries will be accessing.
Anyone done anything similar?
My thought right now is to create a PLSQL loop that:
Converts the BLOB to text to get the SQL query
Execute an explain plan for the query
Look at the result of the explain plan, and get all the lines with TABLE ACCESS
Edit: I'm on Oracle Database 11g Release 11.2.0.4.0 - 64bit if that helps, and have read-only access to the database, so I'll need to create a solution that's not intrusive to the DB.
This could be another simpler method.
Create dynamic views for those SQLs in the BLOB field.
EXECUTE IMMEDIATE 'CREATE OR REPLACE VIEW YOUR_VIEW_NAME AS '||
utl_raw.cast_to_varchar2(dbms_lob.substr(blob_field));
Query user_dependencies to get the referenced table names.
SELECT referenced_name AS table_name
FROM user_dependencies
WHERE type = 'VIEW'
AND NAME = 'YOUR_VIEW_NAME'
AND referenced_type = 'TABLE';
So, here is how I have done it:
DECLARE
b BLOB;
c CLOB;
n NUMBER;
BEGIN
-- << loop_through_table >>
FOR o IN (SELECT TRIM(LABEL) lbl, REQUEST req FROM REQUEST_TABLE) LOOP
b := o.req;
IF ((b is null) OR (LENGTH(b)=0)) THEN
DBMS_OUTPUT.PUT_LINE(o.lbl || ' : No query found');
CONTINUE;
END IF;
-- Convert the BLOB to text to get the SQL query
DBMS_LOB.CREATETEMPORARY(c,TRUE);
n:=1;
-- << loop_through_blob >>
WHILE (n+32767<=LENGTH(b)) LOOP
DBMS_LOB.WRITEAPPEND(c,32767,UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(b,32767,n)));
n:=n+32767;
END LOOP;
DBMS_LOB.WRITEAPPEND(c,length(b)-n+1,UTL_RAW.CAST_TO_VARCHAR2(DBMS_LOB.SUBSTR(b,length(b)-n+1,n)));
-- Execute explain plan
execute immediate 'EXPLAIN PLAN SET STATEMENT_ID=''temp1'' FOR ' || c;
-- Look at the result of the explain plan, and get all the lines of OBJECT_TYPE='TABLE'
-- << loop_through_plan >>
FOR p IN ( SELECT OBJECT_NAME FROM PLAN_TABLE WHERE STATEMENT_ID='temp1' AND OBJECT_TYPE='TABLE') LOOP
DBMS_OUTPUT.PUT_LINE(o.lbl || ', ' || p.OBJECT_NAME);
END LOOP;
execute immediate 'DELETE FROM PLAN_TABLE WHERE STATEMENT_ID=''temp1'' ';
END LOOP;
DBMS_OUTPUT.PUT_LINE('-- Completed --');
END;

Oracle SQL - UNION breaks when "table does not exist"

I am new to Oracle SQL (and not generally experienced in SQL anyway). I would appreciate it if I could get some help with my problem.
I need to query several tables and views (30 or so) and do it as a UNION across all and return a single result set. However, some of the tables may or may not exist. I know the exact names of all 30 tables/views to be queried. I may do other queries prior to doing the large UNION, but eventually, I must return a single result of all rows. I cannot "define" variables in my sql script.
These are constraints placed on my and beyond my control. In order to work with the existing workflow, I have to operate within these contraints.
The problem is that if any of the tables/views do not exist, then the entire UNION fails and I get no results at all. I get this at the first occurrence of the missing view/table:
SQL Error: ORA-00942: table or view does not exist
00942. 00000 - "table or view does not exist"
*Cause:
*Action:
I would like be able to handle this without breaking. I'd like to simply skip over missing tables and return the remaining rows (though I know this is poor design to try to query without knowing if the table exists). It's ok if I end up including error messages as these will be skipped by the parser eventually.
I have something like this. If any of the views do not exist, everything fails and I get no results:
select col1 || col2 || ... from view1
union
select col1|| col2 || ... from view2
union
select name from view3
.
.
select ... from view30;
I can switch to PL/SQL if that helps. I don't have much experience with PL/SQL but I can use it if it can solve the problem. I did some reading on PL/SQL but could not figure out some straightforward way of doing this.
Any help is highly appreciated. Let me know if I can provide any other details.
Thanks.
Colleagues don't say that you CAN'T do that - they are saying you SHOULDN'T, cause of the fundamental problems. But the world isn't perfect - it often gives you a saw and tells you to hammer a nail. There is a solution to your problem - it involves dynamic SQL and some custom PL/SQL code. Simplified solution:
Create a package and a function:
function does_tbl_exists(p_tbl_name in varchar2) return number is
l_stmt varchar2(60);
begin
l_stmt := 'select count(*) from ' ||p_tbl_name;
execute immediate l_stmt;
return 1;
exception
when others then
return 0 ;
end;
Created a test table TEST_TBL with some data. And then we build up our SQL statement in a pl/sql block:
declare
l_count number;
l_stmt varchar2(400);
l_tab_name_1 varchar2(30) := 'TEST_TBL';
l_tab_name_2 varchar2(30) := 'TEST_TBL2';
begin
if test_pck.does_tbl_exists(p_tbl_name => l_tab_name_1) = 1 then
l_stmt := 'select count(*) count_number from ' || l_tab_name_1;
end if;
if test_pck.does_tbl_exists(p_tbl_name => l_tab_name_2) = 1 then
if l_stmt is not null then
l_stmt := l_stmt || ' union all' || chr(10);
end if;
l_stmt := l_stmt || 'select count(*) count_number from ' || l_tab_name_2;
end if;
if l_stmt is not null then
l_stmt := 'select sum(tmp.count_number) from (' || l_stmt || ') tmp';
dbms_output.put_line(l_stmt);
execute immediate l_stmt into l_count;
dbms_output.put_line('count: '||l_count);
else
dbms_output.put_line('Nothing todo.');
end if;
end;
By this template, you can build up your final SQL statement.
But use this as a last resort - I think you should still go to higher-ups and talk with them, that their current "requirement" is "bad" and they should feel "bad" (Insert meme here).

Call stored procedure for each Row with/without using a cursor

I want to a run a stored procedure for almost 1000 records (P_SHIPMENT_GID) in one go and below is the pseudo code.
DECLARE
P_SHIPMENT_GID VARCHAR2(200);
BEGIN
P_SHIPMENT_GID := NULL;
ULE_PKG_UNPLANNED_ICT_CALC.UNPLANNED_ICT_CALC(
P_SHIPMENT_GID => P_SHIPMENT_GID
);
END;
How can I achieve this with or without using cursors?
It is not all that clear what you want to do (where are the 1000 records from?) but here is a "pattern" I am pretty sure you can use :
BEGIN
FOR i IN (SELECT table_name, status FROM user_tables) LOOP
dbms_output.put_line('name : ' || i.table_name ||
' status : ' || i.status);
END LOOP;
END;
This creates a loop on an implicit cursor and allows you to use the returned rows/column in a readable way.
You can write this anonymous block for your requirement. Although its not clear from where you are storing your SHIPMENT_GID values which you wanted to pass to your procedure/pkg.
BEGIN
FOR rec IN ( --Assuming your shipmentid are stored in a table
SELECT SHIPMENT_GID
FROM Your_TABLE)
LOOP
ULE_PKG_UNPLANNED_ICT_CALC.UNPLANNED_ICT_CALC (
P_SHIPMENT_GID => rec.SHIPMENT_GID);
END LOOP;
END;