SQL to search objects, including stored procedures, in Oracle - sql

I need to write some sql that will allow me to query all objects in our Oracle database. Unfortunately the tools we are allowed to use don't have this built in.
Basically, I need to search all tables, procedures, triggers, views, everything.
I know how to search for object names. But I need to search for the contents of the object.
i.e. SELECT * FROM DBA_OBJECTS WHERE object_name = '%search string%';
Thanks,
Glenn

I'm not sure I quite understand the question but if you want to search objects on the database for a particular search string try:
SELECT owner, name, type, line, text
FROM dba_source
WHERE instr(UPPER(text), UPPER(:srch_str)) > 0;
From there if you need any more info you can just look up the object / line number.
For views you can use:
SELECT *
FROM dba_views
WHERE instr(UPPER(text_vc), UPPER(:srch_str)) > 0

i'm not sure if i understand you, but to query the source code of your triggers, procedures, package and functions you can try with the "user_source" table.
select * from user_source

I would use DBA_SOURCE (if you have access to it) because if the object you require is not owned by the schema under which you are logged in you will not see it.
If you need to know the functions and Procs inside the packages try something like this:
select * from all_source
where type = 'PACKAGE'
and (upper(text) like '%FUNCTION%' or upper(text) like '%PROCEDURE%')
and owner != 'SYS';
The last line prevents all the sys stuff (DBMS_ et al) from being returned. This will work in user_source if you just want your own schema stuff.

i reached this question while trying to find all procedures which use a certain table
Oracle SQL Developer offers this capability, as pointed out in this article : https://www.thatjeffsmith.com/archive/2012/09/search-and-browse-database-objects-with-oracle-sql-developer/
From the View menu, choose Find DB Object. Choose a DB connection. Enter the name of the table. At Object Types, keep only functions, procedures and packages. At Code section, check All source lines.

ALL_SOURCE describes the text source of the stored objects accessible to the current user.
Here is one of the solution
select * from ALL_SOURCE where text like '%some string%';

In Oracle 11g, if you want to search any text in whole database or procedure below mentioned query can be used:
select * from user_source WHERE UPPER(text) LIKE '%YOUR SAGE%'

Related

Need to identify a table in all objects in database

I need to identify a table that is mentioned anywhere in database (in stored proc, views, and, etc.). I tried to find a query online, but couldn't find it. Any help would be great!
I use the free SQL Search plugin for MS Management Studio for things like that: http://www.red-gate.com/products/sql-development/sql-search/
I often use this snippet when I'm looking for dependencies. In this case, you would replace the text with what you're searching (assuming you're on MS SQL Server):
USE [DBNAME]
SELECT OBJECT_NAME(id)
FROM syscomments
WHERE [text] LIKE '%enter_search_here%'
GROUP BY OBJECT_NAME(id)
You can also look for specific object types by adding a check for object property:
WHERE OBJECTPROPERTY(id, 'IsTable') = 1
Here is a LIST of useful object properties!

sql or trick to search through whole database

is there a way to actually query the database in a such a way to search for a particular value in every table across the whole database ?
Something like a file search in Eclipse, it searches accross the whole worspace and project ?
Sorry about that .. its MS SQL 2005
SQL Workbench/J has a built in tool and command to do that.
It's JDBC based and should also work with SQL Server.
You will need to use the LIKE operator, and search through each field separately. i.e.
SELECT * FROM <table name>
WHERE (<field name1> LIKE '%<search value>%') OR
(<field name2> LIKE '%<search value>%') OR
... etc.
This isn't a quick way though.
I think the best way would be to
1) programatically generate the query and run it
2) use a GUI tool for the SQL server you are using which provides this functionality.
In mysql you can use union operator like
(SELECT * from table A where name = 'abc') UNION (SELECT * from
table B where middlename = 'pqr')
and so on
use full text search for efficency
http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html
Well, your best bet is to write a procedure to do this. But to give you some pointers you can use the INFORMATION_SCHEMA.Tables to get a list of all the tables in a given database and INFORMATION_SCHEMA.Columns to get a list of all columns. These tables also give you the datatype of columns. So you will need a few loops on these tables to do the magic.
It should be mentioned most RDBMSs nowadays support these schemas.
In phpmyadmin, go to your database, reach the search tab.
Here you will be able to select all of your tables and search through your entire db in one time.

SQL query to get the source of a Stored Procedure

I'm using a DB2 database and I'm hoping for a query which will iterate over all stored procedures in a single database and print out the source code of each. No fancy formatting or performance requirements.
The reason for this (in case there's a better way of doing it) is I'm trying to track down usages of a particular table in our stored procs, so I want to be able to do a simple text search through all of them.
Also, I've got access to SQuirreL SQL client if anyone knows of a way via that.
Ah, figured it out. For other's reference:
select ROUTINENAME, TEXT from syscat.routines
where definer not in ('SYSIBM') AND ROUTINESCHEMA='databaseName'
I know this is old, but your answer started me on the right track. We are also using DB2, but don't have syscat.routines visible to us. However we do have SYSIBM.SYSROUTINES and that allows similar by doing
SELECT SCHEMA,
NAME,
TEXT
FROM SYSIBM.SYSROUTINES
WHERE SCHEMA = '<SCHEMA>'
and NAME = '<NAME>'
FOR FETCH ONLY WITH UR;

Searching for Text within Oracle Stored Procedures

I need to search through all of the stored procedures in an Oracle database using TOAD. I am looking for anywhere that the developers used MAX + 1 instead of the NEXTVAL on the sequence to get the next ID number.
I've been doing SQL Server for years and know several ways to do it there but none are helping me here.
I've tried using
SELECT * FROM user_source
WHERE UPPER(text) LIKE '%blah%'
Results are returned but only for my default schema and not for the schema I need to be searching in.
I also tried the below but it just errors
SELECT * FROM SchemaName.user_source
WHERE UPPER(text) LIKE '%blah%'
SELECT * FROM ALL_source WHERE UPPER(text) LIKE '%BLAH%'
EDIT Adding additional info:
SELECT * FROM DBA_source WHERE UPPER(text) LIKE '%BLAH%'
The difference is dba_source will have the text of all stored objects. All_source will have the text of all stored objects accessible by the user performing the query. Oracle Database Reference 11g Release 2 (11.2)
Another difference is that you may not have access to dba_source.
I allways use UPPER(text) like UPPER('%blah%')
If you use UPPER(text), the like '%lah%' will always return zero results. Use '%LAH%'.
For me, the given query didn't work. It was showing no result. I really don't know why. But "Dependancy" feature of SQLDeveloper saved my day!!!.
In SQLDeveloper, when you select the table in lefthand side "connection" view, tables details opened in the "document" view on righthand side. There are many tabs in document view like columns, data, model, constrain etc. One of the tab is "Dependancy". This tabs list all the objects like triggers, indexes, functions, procedures etc where table is refered.
For TOAD, I think, it is "Referantial" and "Used By" tabs. (Not sure about it, please refer TOAD referrance materials)
Hope this will help someone who is struggling with query like me.

How can I see where data in a column comes from?

I don't know if anyone can help me. In my job, I inherited a completely undocumented database (Oracle 11). So far, I've managed to map most of the tables and determine what's going on where. However, there are a few columns that I haven't been able to decipher.
Is there some way of finding out how is the data in the column built? This is not a manual input. Everything seems to point to the data being the result of an entry in a different column in a completely different table.
It might be an impossible task, but any and all suggestions will be more than welcome.
Thanks!
C
Perhaps the data is being inserted in your mystery columns via a trigger? Try looking in the PL/SQL source table in the dictionary:
SELECT owner, name, type, line
FROM dba_source
WHERE UPPER(text) LIKE '%MYSTERY_COLUMN_NAME%'
AND type = 'TRIGGER'; -- use or omit this as desired.
This will get you pointed in some possible places to look.
Good luck!
You can retrieve the complete DDL for a table using the DBMS_METADATA package.
SELECT dbms_metadata.get_ddl('TABLE', 'YOUR_TABLE_NAME', 'YOUR_USER_NAME')
FROM dual;
If those columns are really computed columns then this should be visible in the DDL for the table.
Alternatively you can use SQL Developer to show the DDL for the table
I am presuming that you already have the sql that is in question.
select table_name from dba_tab_columns
where column_name = 'COLUMN_YOU_WANT_TO_KNOW'
will provide all tables that contain a column name that you are looking for. If you do not have dba privileges you can use all_tab_columns instead (which will show all tables your account would have access to).