ofina
July 17th, 2008, 05:32 PM
I have a column value that I know is there somewhere, but I don't know where. I don't even know which column it's in.
Is there an SQL query (SQL Server 2000) that can help me find where the value is located?
Fiona
compavalanche
July 17th, 2008, 05:34 PM
I need more info.
I think your asking you have a column that is named something but forget the exact name?
You could view the schema. Or do you need to this programatically? There are ways but its vendor specific. For example sysobjects for mssql.
Provide more info and I can be more specific.
Edit: Oops I see you are using SQL Server, sysobjects may be what you want.
cjard
July 18th, 2008, 04:36 AM
once, in oracle, I ran a query like this:
SELECT
'INSERT INTO db_col_values '||
'SELECT DISTINCT '||
' '''||tab_name||''' as tab_name, '||
' '''||col_name||''' as col_name, '||
' '||coll_name||' as col_val '||
'FROM '||tab_name||';'
FROM
all_tab_cols
It is an sql that wrote a LOT of sql statements like this:
INSERT INTO db_col_values SELECT DISTINCT 'mytable', 'mycolumn1', mycolumn1 FROM mytable;
INSERT INTO db_col_values SELECT DISTINCT 'mytable', 'mycolumn2', mycolumn2 FROM mytable;
INSERT INTO db_col_values SELECT DISTINCT 'mytable', 'mycolumn3', mycolumn3 FROM mytable;
, which I then pasted into the editor and ran. Over the course of a few hours, the database collected every distinct table name, column name and column value
Now, I sit and think "hmm.. where in the database would i find columns having value XYZ"
And I can write:
SELECT * from db_col_values WHERE col_value = 'XYZ'
Remember, you can write SQL, that creates SQL, and then run it!