Is it possible to write an SQL to retrieve column names from a table using the data that exists in the columns? If yes, how? If not, is Oracle doing anything about it?
-
I have a set of data that exists in some of the columns of a table in an Oracle database. Using SQL, I would like to retrieve the column names that have the data. For eg, I have the names Ram and Sam (the data). With these in hand, I'd like to retrieve all the columns in a table that have these data in them.
-
Answer:
Assuming Oracle database, something like this will work: SET SERVEROUTPUT ON DECLARE TYPE COLUMNS_T IS TABLE OF VARCHAR2(30); T_COLUMNS COLUMNS_T; found_ind INT; BEGIN SELECT COLUMN_NAME BULK COLLECT INTO T_COLUMNS FROM all_tab_cols WHERE TABLE_NAME = UPPER('dim_tenant') AND OWNER = UPPER('SLALOMBNB') AND DATA_TYPE = 'VARCHAR2'; FOR I IN T_COLUMNS.FIRST .. T_COLUMNS.LAST LOOP found_ind := '0'; EXECUTE IMMEDIATE ' SELECT SUM(1) FROM dim_tenant WHERE ' || T_COLUMNS(I) || ' = ''Philip''' INTO found_ind; IF found_ind >= 1 THEN DBMS_OUTPUT.PUT_LINE(T_COLUMNS(I)); END IF; END LOOP; END; Notes: If the data you're looking for exists in columns other than VARCHAR2 data type, you'll need to adjust the query from all_tab_cols accordingly. If you have number data types you're looking for, you'll probably need a separate object and query for numeric values. This sounds like a very weird business scenario. There's probably serious data modeling problems if you don't know the name of the column the data you're looking for exists in. Obviously, replace my table of dim_tenant and owner SLALOMBNB with your own table_name and schema name. Obviously, replace "Philip" with the text you're looking for. Output goes to DBMS_OUTPUT. Make sure you run that first statement above DECLARE to get the output. This query basically just loops through all the column names and applies a where clause to a simple select statement that selects a dummy column. It is horrifically inefficient but I can't think of a better way to do it off the top of my head. So be warned might run super slow on large data sets.
Chris Schrader at Quora Visit the source
Other answers
You can do in 2 ways : http://localhost/phpmyadmin/url.php?url=http%3A%2F%2Fdev.mysql.com%2Fdoc%2Frefman%2F5.5%2Fen%2Fdescribe.html&token=3fcd3a573adb46befcfc593612206c36escribe table_name or SHOW COLUMNS FROM table_name
Naveed Ramzan
I don't think there is any straight-forward way to do that, because that is not what a relational database is meant to support. I am almost sure that there is no simple SQL query that can do that for you. Of course, you could write a PL/SQL script that uses metadata of all tables to comb through columns of matching data types looking for your data. And maybe there are data warehousing tools that can help.
Raman Divakaran
Related Q & A:
- How to skip columns empty in CSV file when importing into MySQL table using LOAD DATA INFILE?Best solution by Stack Overflow
- How to add primary key from multiple table as a foreign key in a table in sql server 2008?Best solution by stackoverflow.com
- Can I estimate the row count of a large mysql table using the disk space?Best solution by Database Administrators
- How to make collapsible columns for a table?Best solution by Stack Overflow
- Is it possible to delete my yahoo 360 page so I can start all over, if yes HOW?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.