Tables that contain a column all in one row in SQL 2000

When i run the code below,
"SELECT b.name AS ColumnName,a.name AS TableName FROM sysobjects AS a INNER JOIN syscolumns b ON a.id = b.id WHERE a.xtype = 'U' ORDER BY 1"
I get the column names and the tables they are in (if there are multiple tables that have the same column name, there are multiple rows for the columns).

I want to display something like this:
Column1                   TableA,TableC,TableT
Column5                   TableAA,TableFC,TableE

Raj's query below does it in SQL 2005. 'With' and 'XML' are new to 2005; I need a way to run this in 2000. how do you do it?

thanks

--2005
WITH CTETable AS
(
	SELECT b.name AS ColumnName,a.name AS TableName 
		FROM sysobjects AS a 
			INNER JOIN syscolumns b ON a.id = b.id 
	WHERE a.xtype = 'U' 
)
SELECT DISTINCT ColumnName, 
  NULLIF(   
    STUFF(   
      (SELECT ', ' + TableName FROM CTETable WHERE ColumnName = D.ColumnName FOR XML PATH('')  
                        ), 1, 2, ''   
    ), ''   
  ) AS AllTables 
FROM CTETable D
WHERE
        (SELECT Count(*) FROM CTETable WHERE ColumnName = D.ColumnName) > 1

Open in new window

LVL 6
anushahannaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you will need a function that concatenates the list per column name.


create function dbo.GetTablesOfColumn(@colname sysname)
returns nvarchar(4000)
as
begin
 declare @res nvarchar(4000)
 select @res = isnull(@res + ',' , '') + a.name
   FROM sysobjects AS a 
   JOIN syscolumns b ON a.id = b.id 
  WHERE a.xtype = 'U' 
    and b.name = @colname
 return @res
end

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Guy Hengel [angelIII / a3]Billing EngineerCommented:
and your query becomes:
select * 
  from (
select a.name, dbo.GetTablesOfColumn(a.Name) tables
  from syscolumns a
 ) sq
where sq.tables like '%,%'

Open in new window

0
anushahannaAuthor Commented:
Thanks angel.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.