• 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 http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/