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.
| Author |
Topic |
|
sg2255551
Constraint Violating Yak Guru
274 Posts |
Posted - 2011-12-21 : 10:11:39
|
| hihow do i rewrite without using cursors or loops?declare @ColName sysname,@TableName sysname,@FieldList varchar(MAX),@LastTableName sysnameselect @LastTableName = '',@FieldList = ''declare @tb table (TableName sysname null, FieldList varchar(MAX) null)declare curs cursor forselect c.name, object_name(c.object_id)from sys.columns c INNER JOIN sys.objects o on c.object_id = o.object_idwhere o.type = 'U'order by o.object_idopen cursfetch curs into @ColName, @TableNameset @LastTableName = @TableNamewhile @@FETCH_STATUS = 0BEGINif @LastTableName <> @TableNameBEGINinsert into @tb values (@LastTableName,@FieldList)set @FieldList = ''set @LastTableName = @TableNameENDset @FieldList = case when @FieldList = '' then @ColName else + @FieldList + ',' + @ColName endfetch curs into @ColName, @TableNameENDdeallocate cursinsert into @tb values (@LastTableName,@FieldList)select * from @tb |
|
|
sunitabeck
Master Smack Fu Yak Hacker
5155 Posts |
Posted - 2011-12-21 : 10:34:12
|
May be this?SELECT DISTINCT c1.Table_Name, c3.columnNamesFROM Information_Schema.[COLUMNS] c1 CROSS APPLY ( SELECT c2.column_name AS [text()], ',' AS [text()] FROM Information_Schema.[COLUMNS] c2 WHERE c1.Table_name = c2.TABLE_NAME FOR XML PATH('') ) c3(columnNames) |
 |
|
|
|
|
|
|
|