BCP - Need to output only 1 row per file.

  • Hello,

    I am using BCP to export data to a file which is easy enough. The issue is that I need to output only 1 row per file. I've looked online but can't find how to do this (or if it's possible).

    All I can think of doing is looping through the data set and running BCP for each row. But, how do I create and execute dynamic BCP?

    Thanks,

    Brett

  • Just write a SELECT TOP 1 ... query and use that to drive bcp:

    https://msdn.microsoft.com/en-us/library/ms162802.aspx

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Hi Kevin,

    Thanks for the fast response.

    The specific piece of this I'm needing help on is how to dynamically create and execute the BCP command (in a batch file or PowerShell).

    Thanks,

    Brett

  • Since bcp is a command-line tool and I'm old school, I would use DOS scripting for this. Works fine, lasts a long time. 🙂 If you have xp_cmdshell enabled on your SQL Server you could use that too and write your bcp line using tsql if you want.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Hi Kevin,

    OK. I think I've got it now.

    Thanks,

    Brett

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

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