SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Export in txt from sql (in a pivot way)


Export in txt from sql (in a pivot way)

Author
Message
antonela
antonela
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 176
Hello everybody,

I have to export the records of a table in a .txt file but in a strange way (in a column). For example I have this table with 3 fields:

create table prova
(cod varchar(1),
des1 varchar(2),
des2 Varchar (2)
)

insert into prova values (1, 'A1', 'A2')
insert into prova values (2, 'B1', 'B2')


I need to have the output in this way:

1 cod 1
1 des1 A1
1 des2 A2
8 S
1 cod 2
1 des1 B1
1 des2 B2
8 S


So, I have to put '1' at the begining of every field and '8 S' at the end of every record.
Anyboody helps me to do this?

Thank you very much.
Sean Lange
Sean Lange
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27324 Visits: 17557
antonela (5/23/2014)
Hello everybody,

I have to export the records of a table in a .txt file but in a strange way (in a column). For example I have this table with 3 fields:

create table prova
(cod varchar(1),
des1 varchar(2),
des2 Varchar (2)
)

insert into prova values (1, 'A1', 'A2')
insert into prova values (2, 'B1', 'B2')


I need to have the output in this way:

1 cod 1
1 des1 A1
1 des2 A2
8 S
1 cod 2
1 des1 B1
1 des2 B2
8 S


So, I have to put '1' at the begining of every field and '8 S' at the end of every record.
Anyboody helps me to do this?

Thank you very much.


It is hard to figure out exactly what you want for output here. This should at least get you started.


select d.Val
from prova
cross apply (values('1 cod ' + Cod),('1 des1 ' + des1),('1 des2 ' + des2),('8 S')) d(Val)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
antonela
antonela
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 176
Thank you very much.
antonela
antonela
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 176
Do you know how can I do the same in sql server 2005?
This functions properly only in sql server 2008 and 2012.
Luis Cazares
Luis Cazares
SSCoach
SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)SSCoach (17K reputation)

Group: General Forum Members
Points: 17442 Visits: 19122
The alternative could be the official unpivot.

SELECT Val
FROM (SELECT CAST( '1 cod ' + CAST( cod AS varchar(10)) AS varchar(20)) col1,
CAST( '1 des1' + des1 AS varchar(20)) col2,
CAST( '1 des2' + des2 AS varchar(20)) col3,
CAST( '8 S' AS varchar(20)) col4
FROM #prova) p
UNPIVOT(Val FOR Cols IN (col1, col2, col3, col4))u


Or you could use UNION ALL for 2000 and previous versions.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91104 Visits: 41151
antonela (5/26/2014)
Do you know how can I do the same in sql server 2005?
This functions properly only in sql server 2008 and 2012.


Heh... this is why you should always pick the correct forum to post your questions on. Because you posted on 2012 forum, people just naturally assume that your question is based on 2012 instead of 2005. Posting on the correct forum means that you'll get a compatible answer instead of having to wait for someone to rehash your code for an earilier version.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
antonela
antonela
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 176
You're right.
Unfortunately when I wrote the first post I didn't know the customer still uses 2005.
antonela
antonela
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 176
Thank you very much
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)SSC Guru (91K reputation)

Group: General Forum Members
Points: 91104 Visits: 41151
antonela (5/27/2014)
You're right.
Unfortunately when I wrote the first post I didn't know the customer still uses 2005.


Ah... got it. Thanks for the feedback.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
antonela
antonela
SSC Journeyman
SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)SSC Journeyman (81 reputation)

Group: General Forum Members
Points: 81 Visits: 176
For me is very important to have the right order in the table.
For the first record I must to have the value of the first field of my original table, in the last record I must to have '8 S' that indicates me I have the fix value I put (but for the last record).
Is it possible to put another field (count) :1, 2, 3 etc?

val count
1 cod 1 1
1 des1 A1 2
1 des2 A2 3
8 S 4
1 cod 2 5
1 des1 B1 6
1 des2 B2 7
8 S 8


When I have many records I am not sure I have the right sorting.
I insert the result of the query in a table and when I call "select top 100 percent * from table" I don't have '8 S' on the last record.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search