Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 query tablename/columnname given value(s)

Author  Topic 

Hommer
Aged Yak Warrior

808 Posts

Posted - 2013-10-16 : 13:33:20
Hi,

First, I totally understand my question is against the sql norm, which is given known tablename/columnname to query the return values.

I want to have this query that returns the table names and column names that contain the given value(s).

Business needs dictated that I have to start from given values instead. That is to say we own the apps developed by vendors, and own the dbs, but no middle tier code, nor data dictionary.

So, I started with this
select distinct object_Name(Object_ID) myTbls, [name] from sys.columns where name like '%mycolname%'
and object_Name(Object_ID) not like 'fn%'

I have to guess the possible column name as the starter.

I realize if the condition value is not very unique, the return dataset could be huge. It is a performance nightmare and it is useless. So I hope the solution will let me specify, say return the first 1000 matches.

Any one know any this kind of script? I don't even know how to google it. But the business case is there. I don't think I am along.

Thanks!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-10-16 : 13:39:55
this is not against the sql norm - you're still getting values from known tables/columns (the system tables).
select TOP 1000 <blah blah> will limit your results to 1000 rows.



Be One with the Optimizer
TG
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2013-10-16 : 13:47:34
The problem is that I don't know the blahblah to start with.

Select top 1000 colname_I_dont_have from table_name_I_dont_have
where col_2_I_dont_know = 'this is the value I have from the app UI'

That is what I meant by against the norm, not the 1000 part.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-10-16 : 13:53:18
Sorry, I thought the columns were [object_id], [name] and the table was [syscolumns]. So you mean you are constructing the query based on the results of the query you posted? If so then why can't you build the "top 1000" into your query string you are constructing?

Perhaps you should explain the big picture objective maybe someone can suggest a better way.


Be One with the Optimizer
TG
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2013-10-16 : 13:53:43
Here's what I've used in the past: http://vyaskn.tripod.com/search_all_columns_in_all_tables.htm

Tara Kizer
SQL Server MVP since 2007
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-10-16 : 13:55:55
Are you searching for string values, or for numeric values? If you are searching for string values, there are scripts availabe on the web that can do that for you. For example:

http://gallery.technet.microsoft.com/scriptcenter/c0c57332-8624-48c0-b4c3-5b31fe641c58
http://beyondrelational.com/modules/2/blogs/78/posts/11138/how-to-search-a-string-value-in-all-columns-in-the-table-and-in-all-tables-in-the-database.aspx

If you are searching for an integer value, the same method could be adapted - you can cast the integer column to varchar and then search.

If you are looking for floating point values, you would need to develop a query that looks through all numeric columns having values within an upper and lower bound.

Edit:
Go to Top of Page

Hommer
Aged Yak Warrior

808 Posts

Posted - 2013-10-16 : 14:21:45
Thanks everyone!

I should be able to work from these samples.

Go to Top of Page
   

- Advertisement -