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

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).

Related

How can I stop my SQL from returning empty data results?

I usually use Toad to manipulate my Oracle databases, but I even tried SQL manager for this one and it still would not work. I have a table with a few hundred records, and even running a simple
SELECT * FROM customer
will not work. There are no errors, and the data grid that displays pulls all the correct field column names but there are no records shown. What could be causing this?
Does your login schema own the table? If not, verify that any synonym is actually pointing to the object that you think it is. Preface the table name with its owning schema to rule out any conflicts.

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 Easily Document a Database Environment?

HI Frdz, i have around 40 tables in database and i have created a document with all of these table names, fields, datatype, length of datatype, NUll or NOT NULL. but when i was working on tables in database i had to change the table field names, datatype and length of datatype as well. now as i am done with all my coding i have to have that document matching with what i have in database. so do u think is there any easy way i can do this or do i have to manually go through each table and update the doc?
Thanks
You can usually query the INFORMATION_SCHEMA tables for this kind of info.
E.g.,
select *
from INFORMATION_SCHEMA.COLUMNS
You can connect the database to Visio and get a nice database diagram out of it that way. If they are the visual type that is.
The DBdesigner 4 supports a approach to synchronize your entity relationship model and your real SQL table model. At the features page they mention
Model-To-Database syncronisation
I think this should be possible vice-versa.
In Oracle:
select *
from user_tab_columns

how to select a particular table from a storedprocedure which returns multiple table?

I have a storedproc which has multiple select statement.
When it is executed in sql server , it returns multiple tables.
I need a query which will select a particular table from the storedproc e.g. sp_help.
Please help.
If i have a look at this link, it seems that it is not possible.
Astander is right. From the number of tables available in ur SP, it is not possible directly.
However, you can apply some trick to accomplish your work. I am giving an example here. May be you can generate some idea based on this line.
SELECT * FROM sys.Tables where name =
'my_tbl'
As you can make out that I am filtering out the query by a table among all the tables available in my database.
Something of this sort may help you.
Else, if can get the dataset and then from that get the needed datatable from ur frontend code.