Solved

export data

Posted on 2004-08-05
6
1,350 Views
Last Modified: 2008-02-01
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
Comment
Question by:TEALTEAL
6 Comments
 
LVL 4

Expert Comment

by:andertst
ID: 11729460
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
 

Author Comment

by:TEALTEAL
ID: 11729611
No it wraps the same if I widen my window...thanks
0
 
LVL 17

Expert Comment

by:BillAn1
ID: 11730011
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 50

Expert Comment

by:Lowfatspread
ID: 11730647
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
 
LVL 17

Expert Comment

by:BillAn1
ID: 11731393
The maximum you can set with -w is 80 , in this case I think this is not enough, hence the wrapped text.
0
 
LVL 9

Accepted Solution

by:
crescendo earned 250 total points
ID: 11731516
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 Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now