December 29, 2008 at 7:46 am
How?
December 29, 2008 at 8:19 am
Sure. The technique below works fast for concatenating values in any column. The cte isn't required, it just makes the final query a little easier to read and understand.
;with cte1(col,tbl) as
(select c.name, object_name(c.object_id)
from sys.columns c
join sys.objects o on c.object_id = o.object_id
where o.type = 'U'
)
SELECT tbl, columnList = substring((SELECT ( ', ' + col)
FROM cte1 c1
WHERE c1.tbl = cgroup.tbl
ORDER BY tbl, col
FOR XML PATH( '' )), 3, 1000 )
FROM cte1 cgroup
GROUP BY tbl
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 29, 2008 at 8:34 am
Dave, sorry I was editing my post when you looked a minute ago. The cut and paste of the code looked really squirrelly because of tabs settings. Please post the entirety of your original question back for others to see.
Let me know if you have any questions.
Bob
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 29, 2008 at 8:45 am
Bob - you must be a SERIOUS mindreader. Coming up with the right code that answer the question (consisting only of Can we?......How>) - that's truly impressive.
Say - do you see 5 or six numbers in your head right now? I need some choices for the Pick 6 tonight:)
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
December 29, 2008 at 8:51 am
Matt: It's a gift. I am but a tool of a Higher Power 😉
If you see my post above, we just had a touch of asynchronous communications. He originally posted a full question complete with sample code which I responded to, but the query I pasted in looked janky and I was editing my post when he came back to look. Instead of adding a reply, he edited his original post. I was confused for a moment too.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 29, 2008 at 8:53 am
.... and if I could pick numbers, I would be retired and skiing in Park City or Alta right now, so you would have never seen me on this forum. 😀
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 29, 2008 at 8:54 am
Here's the op's original question:
Here is a code that is returning a list of all the user tables with their fields list.
My question is can we re-write this code without cursor or looping?
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
December 29, 2008 at 9:00 am
And here's the information_schema 'optional non-cte version'
select t.table_name,
( select stuff(
( select ',' + c.column_name
from information_schema.columns c
where c.table_name = t.table_name
order by c.ordinal_position
for xml path('')
)
, 1, 1, '')
) as fieldlist
from information_schema.tables t
where t.table_type = 'base table'
/Kenneth
December 29, 2008 at 9:02 am
Thanks, Kenneth. Maybe it's holiday silliness, but I kind of like the idea of a post topic being simply "Can we?"
Yes, we can.
Bob (the Builder)
P.S. I like your non-CTE version using information_schema.
__________________________________________________
Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
December 30, 2008 at 12:37 am
Well, the correct answer is 'It depends' 😉
(happy Holidays to all)
/Kenneth
December 30, 2008 at 2:41 am
Dave (12/29/2008)
How?
What? 😉
Failing to plan is Planning to fail
January 6, 2009 at 2:22 am
And why? 🙂
January 6, 2009 at 9:52 am
He's an Indian
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply