SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Another way to write this query
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

ahmeds08
Aged Yak Warrior

India
573 Posts

Posted - 02/05/2013 :  12:59:21  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
Dear Experts,
Below query lists out all the tables and their respective column names as field list.
how can i achieve the same output without using cursor,loops and temp objects.

declare @ColName sysname,
@TableName sysname,
@FieldList varchar(MAX),
@LastTableName sysname

select @LastTableName = '',

@FieldList = ''

declare @tb table (TableName sysname null, FieldList varchar(MAX) null)
declare curs cursor for

select c.name, object_name(c.object_id)

from sys.columns c INNER JOIN sys.objects o on c.object_id = o.object_id

where o.type = 'U'

order by o.object_id

open curs

fetch curs into @ColName, @TableName

set @LastTableName = @TableName

while @@FETCH_STATUS = 0

BEGIN
if @LastTableName<> @TableName

BEGIN

insert into @tb values (@LastTableName,@FieldList)

set @FieldList = ''
set @LastTableName = @TableName

END



set @FieldList = case when @FieldList = '' then @ColName else + @FieldList + ',' + @ColName end



fetch curs into @ColName, @TableName

END

deallocate curs

insert into @tb values (@LastTableName,@FieldList)

select * from @tb

Thanks,
Javeed

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/05/2013 :  13:11:43  Show Profile  Reply with Quote
looks like this to me

SELECT t.TABLE_NAME,
STUFF((SELECT ',' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = t.TABLE_NAME FOR XML PATH('')),1,1,'')
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_TYPE='BASE TABLE'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

ahmeds08
Aged Yak Warrior

India
573 Posts

Posted - 02/05/2013 :  13:25:01  Show Profile  Send ahmeds08 a Yahoo! Message  Reply with Quote
Visakh,
Thanks a lot.


quote:
Originally posted by visakh16

looks like this to me

SELECT t.TABLE_NAME,
STUFF((SELECT ',' + COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = t.TABLE_NAME FOR XML PATH('')),1,1,'')
FROM INFORMATION_SCHEMA.TABLES t
WHERE t.TABLE_TYPE='BASE TABLE'


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/



Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52309 Posts

Posted - 02/06/2013 :  00:56:33  Show Profile  Reply with Quote
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.11 seconds. Powered By: Snitz Forums 2000