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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Output individual fields on successive lines

Author  Topic 

jrl
Starting Member

2 Posts

Posted - 2008-01-10 : 11:04:14
I have a series of records made up of field1, field2, field3.

I want to output a report/textfile in this format:

Record1_field1
Record1_field2
Record1_field3
Record2_field1
Record2_field2
Record2_field3
Record3_field1
Record3_field2
Record3_field3

Does anyone know if this is possible please?

Many thanks
John

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-01-10 : 11:14:00
Here is one way (using any table of numbers with cross join)


declare @t table (rowid int, col1 int, col2 int, col3 int)
insert @t values (1,1,2,3)
insert @t values (2,1,2,3)

select rowid
,val = case
when n.number = 1 then t.col1
when n.number = 2 then t.col2
when n.number = 3 then t.col3
end
from @t t
cross join master..spt_values n
where n.type = 'p'
and n.number > 0
and n.number < 4


output:
rowid val
----------- -----------
1 1
1 2
1 3
2 1
2 2
2 3


EDIT:
You are going to run into problems if the different columns are different datatypes. If that is the case you may need to do some CONVERTing.

Be One with the Optimizer
TG
Go to Top of Page

jrl
Starting Member

2 Posts

Posted - 2008-01-10 : 11:19:33
Many thanks TG for such a quick response - I'll try this out
Go to Top of Page
   

- Advertisement -