Trying to do a BCP export of a table

  • Hi,
    I am Trying to do a BCP export of a table. I am using the following that I got online:
    EXEC master..xp_cmdshell 'bcp "SELECT Dph_index,file_no,phone_no From DMPCNI.dbo.Star"
    queryout "C:\Temp\Star2.csv" -c -T -S PS2016\DMP2'

    This doesn't give an error when I run it, it just comes back with showing me the different options I can use (like -c, -t etc.);  and it does not write to the file.

    Any ideas what I am doing wrong here? 
    Thank you
     

  • I'm not sure about this particular command, but command options are usually case sensitive.
    The output error seems to indicate the -t option should be lowercase.
    You may want to try.


    EXEC master..xp_cmdshell 'bcp "SELECT Dph_index,file_no,phone_no From DMPCNI.dbo.Star"
    queryout "C:\Temp\Star2.csv" -c -t -s PS2016\DMP2'

  • Upon reading the article a bit, I'd point out that you need to use an uppercase '-S' to identify the server name.

  • Thanks but I tries that as well as other options and I still get the same thing, but thanks for your help, and I look at the link you  provided.
    Thanks

  • Also, the -t may not work without a separator arguement such as a comma. i.e. '-t,'

  • Thanks the link helped I got it to export, but for some reason it does not include the column names, Any idea what I can do there?

  • itmasterw 60042 - Tuesday, August 28, 2018 10:00 AM

    Thanks the link helped I got it to export, but for some reason it does not include the column names, Any idea what I can do there?

    Try changing your select to something like this.  Of course, you may have to cast integer columns in your table to varchar type.


    "select 'col1', 'col2',... union all select * from mytable"

  • Yah I seen a lot of people on line had that, and I tried it and but is then says that the BCP failed

  • This is what I had tried:
    SELECT Dph_index, file_no, phone_no UNION ALL SELECT Dph_index, file_no, phone_no FROM

  • itmasterw 60042 - Tuesday, August 28, 2018 10:42 AM

    This is what I had tried:
    SELECT Dph_index, file_no, phone_no UNION ALL SELECT Dph_index, file_no, phone_no FROM

    You need single quotes around the column names. Like this.


    SELECT 'Dph_index', 'file_no', 'phone_no' UNION ALL SELECT Dph_index, file_no, phone_no FROM

  • Jackie Lowery - Tuesday, August 28, 2018 11:48 AM

    itmasterw 60042 - Tuesday, August 28, 2018 10:42 AM

    This is what I had tried:
    SELECT Dph_index, file_no, phone_no UNION ALL SELECT Dph_index, file_no, phone_no FROM

    You need single quotes around the column names. Like this.


    SELECT 'Dph_index', 'file_no', 'phone_no' UNION ALL SELECT Dph_index, file_no, phone_no FROM

    This could be problematic unless ALL the actual column data has a varchar data type, as otherwise every data type in the table would have to be convertible to varchar implicitly.

    Steve (aka sgmunson) 🙂 🙂 🙂
    Rent Servers for Income (picks and shovels strategy)

  • sgmunson - Tuesday, August 28, 2018 12:48 PM

    This could be problematic unless ALL the actual column data has a varchar data type, as otherwise every data type in the table would have to be convertible to varchar implicitly.

    Right.  I was just giving an example.  He would have to use the correct method to convert each column.

Viewing 13 posts - 1 through 12 (of 12 total)

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