June 6, 2009 at 4:17 pm
Hi All,
I need to find Same Column Names (duplicate columns) in a set of tables. ex. I have a database which contains 20 tables and some of those tables have same column names. I need to find those column names which exist in more than one table.
Please help me with the query...
Thanks much...
June 6, 2009 at 7:44 pm
there's gotta be a better way, but this is all i could come up with...it works, but seems overly complicated..i need a nap or something:
select object_name(syscolumns.id) as TableName,syscolumns.name As ColumnName ,MyAlias.TheCount
from syscolumns
inner join (
select syscolumns.name ,count(*) AS TheCount from syscolumns
inner join sysobjects on syscolumns.id=sysobjects.id
where sysobjects.xtype='U'
group by syscolumns.name
having count(*) >1) MyAlias
On syscolumns.name = MyAlias.name
Order By MyAlias.TheCount DESC
Lowell
June 7, 2009 at 9:31 am
You can also use the INFORMATION_SCHEMA views:
WITH cols AS
(
SELECT
COLUMN_NAME,
COUNT(*) COLUMN_COUNT
FROM INFORMATION_SCHEMA.COLUMNS
GROUP BY COLUMN_NAME
HAVING COUNT(*) > 1
)
SELECT
c.TABLE_SCHEMA,
c.TABLE_NAME,
cols.COLUMN_NAME,
cols.COLUMN_COUNT
FROM cols
JOIN INFORMATION_SCHEMA.COLUMNS c ON cols.COLUMN_NAME = c.COLUMN_NAME
ORDER BY cols.COLUMN_NAME
Flo
Viewing 3 posts - 1 through 2 (of 2 total)
You must be logged in to reply to this topic. Login to reply