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
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Return all columns with same name and same data

Author  Topic 

carmined58
Starting Member

1 Post

Posted - 2012-04-17 : 22:18:58
I'm trying to generate a query that will return all columns in a database with the same name, FolderID, that have the same value, 8508.
I have found tons of answers searching Goole that will provide the results for both, but separately. For instance, there is a query for returning all tables with the column "FolderID."
SELECT t.name AS table_name,
SCHEMA_NAME(schema_id) AS schema_name,
c.name AS column_name
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
ORDER BY schema_name, table_name;

There is a query for returning the data "8508." This SP is called either "SearchAllTables" or "SearchForString."

However, I cannot find any examples that will return all columns with a specific name AND a specific value.

Can anyone help me?

Thank you!

yosiasz
Master Smack Fu Yak Hacker

1635 Posts

Posted - 2012-04-17 : 23:30:34
that might require you to do some dynamic query. in your query you are querying for metadata not data of the metadat

[code]
declare @SQL varchar(max)

SELECT @SQL = 'SELECT * FROM dbo.' + t.name + ' WHERE CreatedAt > 2012-03-03 ' + CHAR(10) + CHAR(13)
FROM sys.tables AS t
INNER JOIN sys.columns c ON t.OBJECT_ID = c.OBJECT_ID
WHERE c.name = 'CreatedAt'

exec(@SQL)
[code]
<><><><><><><><><><><><><><><><><>
If you don't have the passion to help people, you have no passion
Go to Top of Page
   

- Advertisement -