Output Stored Procedure to .TXT File - How to exclude column name and seperators

  • I got this to work (mostly), but this is what the file looks like;

    ColumnName

    -----------

    11111

    22222

    33333

    I want it to look like this:

    11111

    22222

    33333

    Here is some example code. You'll need to put in your server, database, and UNC file share. If you don't have a trusted connection, replace the (-E) with (-U USERNAME -P PASSWORD).

    CREATE PROC DBO.TestOutput

    AS

    SET NOCOUNT ON

    SELECT 11111 AS ColumnName

    UNION

    SELECT 22222 AS ColumnName

    UNION

    SELECT 33333 AS ColumnName

    GO

    EXEC Master..xp_cmdshell 'osql.exe -S SERVERNAME -d DATABASENAME -E -Q "EXEC DBO.TestOutput" -o \\SERVERNAME\WritablePath\TestOutput.txt -w 5000'

    I would like to avoid writing a SSIS package for this, and would prefer a simple SQL Agent job (this will run on its own to output flat files to a shared folder). If someone could tell me how to strip the top two lines off that file, that would be great!

    Thanks,

    Chris

  • Have you tried using BCP for this kind of thing? Might be easier.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • Thanks for the reply. BCP seems to be the way to go. The output came out just like I wanted it!

    Many thanks! Can I Kudos your message in any way?

    Chris

  • Nah. But thanks is good enough. 🙂

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply