Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1400
  • Last Modified:

export data

I need to export data from sql server on a daily basis , getting only new data

Im using master..xp_cmdshell 'osql -E -q "select * from pubs.dbo.authors" -o c:\test.txt'

However I want each row to be on its own line.. And right now I am getting a bunch of blank lines a wrapping.

Any ideas!!

Thanks!
0
TEALTEAL
Asked:
TEALTEAL
1 Solution
 
andertstCommented:
When you say wrapping, if you open the datafile with Notepad or some text editor, does it wrap less if you widen your window?

In Oracle, there is a set linesize command and I would guess the SQL*Server version of that command would be one place to look.

Stephen
0
 
TEALTEALAuthor Commented:
No it wraps the same if I widen my window...thanks
0
 
BillAn1Commented:
osql writes ot a text file, but the max colwidth is 80 chars. It is not really the best way to export data.
You would be much better off using a DTS package to export the data to a file
There you can specify headers, column seperators, line break characters etc
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LowfatspreadCommented:
try the -w parameter ,

and you can get headders , etc with osql

osql
    [-?] |
    [-L] |
    [
        {
            {-U login_id [-P password]}
            | –E
        }
        [-S server_name[\instance_name]] [-H wksta_name] [-d db_name]
        [-l time_out] [-t time_out] [-h headers]
        [-s col_separator] [-w column_width] [-a packet_size]
        [-e] [-I] [-D data_source_name]
        [-c cmd_end] [-q "query"] [-Q "query"]
        [-n] [-m error_level] [-r {0 | 1}]
        [-i input_file] [-o output_file] [-p]
        [-b] [-u] [-R] [-O] [-X[1]]
    ]

0
 
BillAn1Commented:
The maximum you can set with -w is 80 , in this case I think this is not enough, hence the wrapped text.
0
 
crescendoCommented:
You need to set the width of the console before you run OSQL. You do this with the MODE command:

    MODE CON COLS=132

Then you can use the -w parameter on OSQL to set the width. However, this only applies to each session, and so if you run xp_cmdshell twice, once to set the width, once to do the export, you will lose the width setting when the export runs. Therefore, create a batch file and run that, with MODE... as the first line, and OSQL ... as the second.

A better way to do the export is to use BCP, which is happy to do wide rows. Try

    xp_cmdshell('BCP pubs.dbo.authors out C:\test.txt -w -T')

It will give you a tab-separated row, with CRLF at the end of each row, just right for import into Excel etc.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now