Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Convert rows to columns

0 views
Skip to first unread message

Mark Cooper

unread,
Mar 13, 2003, 9:22:17 AM3/13/03
to
Hello,

I have a table (single column) with about 20 records. In a stored proc
i want to take each of these records and use them as the column name
for a new table.

This has to be a dynamic solution as the number of rows in the first
table is subject to change.

Can anyone guide me on the syntax i need to do this,

Thx, Mark

Anith Sen

unread,
Mar 13, 2003, 12:14:05 PM3/13/03
to
Refer to :
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q175574

--
- Anith
(Please respond only to newsgroups)


Steve Dassin

unread,
Mar 13, 2003, 6:30:14 PM3/13/03
to
For all types of pivoting/crosstabs check out
the RAC utility @
www.rac4sql.net


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

WangKhar

unread,
Mar 14, 2003, 4:20:56 AM3/14/03
to
You could go for something along these lines. not tested code...
Or you could have 2 fields in the table, 1 for fieldnames, 1 for field types.

Hope it helps some.

declare @SQL varchar(2000), @Row varchar(200), @FieldType varchar(30)

set @FieldType = 'varchar(100)'

set @SQL = 'Create Table ''Bob'' ('

create cursor mycursor for select fieldname from table
open mycurs
fetch next from mycursor into @Row

while @@fetchstatus = 0
begin
set @SQL = @SQL + ', '+@Row + @FieldType

fetch next from mycursor into @Row
end
close mycursor
deallocate mycursor

set @SQL = @SQL +')'

exec(@SQL)

Mark Cooper

unread,
Mar 14, 2003, 5:09:40 AM3/14/03
to
Thanks but I wanted to be able to DYNAMICALLY create the columns. The
example on MS requires that you know what the columns are going to be
(eg Quarters in the MS example)

Anyone help with the dynamic column thing??

Mark Cooper

unread,
Mar 14, 2003, 5:10:58 AM3/14/03
to
I would ideally like a snippet of script that I can learn from and
customise if needed...i don't want to have to buy a component - :)

BP Margolin

unread,
Mar 14, 2003, 6:48:40 AM3/14/03
to
Mark.

http://www.sqlmag.com/Articles/Index.cfm?ArticleID=15608

-------------------------------------------
BP Margolin
Please reply only to the newsgroups.
When posting, inclusion of SQL (CREATE TABLE ..., INSERT ..., etc.) which
can be cut and pasted into Query Analyzer is appreciated.

"Mark Cooper" <ma...@liquidjelly.co.uk> wrote in message news:4ffdebd9.03031...@posting.google.com...

0 new messages