Click here to monitor SSC
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
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 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
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16639 Visits: 17026
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 Moden's 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
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 Visits: 176
Thank you very much.
antonela
antonela
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 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
SSCrazy Eights
SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)SSCrazy Eights (8.6K reputation)

Group: General Forum Members
Points: 8569 Visits: 18148
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-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45313 Visits: 39936
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
antonela
antonela
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

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

Group: General Forum Members
Points: 63 Visits: 176
Thank you very much
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)SSC-Forever (45K reputation)

Group: General Forum Members
Points: 45313 Visits: 39936
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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
antonela
antonela
Valued Member
Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)Valued Member (63 reputation)

Group: General Forum Members
Points: 63 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