How to specify spaced out column names in bcp command?

Cataster 681 Reputation points
2021-01-08T19:42:11.837+00:00

I am running the following command but i am getting an error:

bcp "select * from (select [Style Code] as [Style Code],[MY Code] as [MY Code] union select [Style Code],[MY Code] from UnPivoted_Table)q order by case [Style Code] when [Style Code] then 0 ELSE 1 END" queryout "\\server.domain.com\CSV_Files\file1.csv" -c -t, -T -S "server1.db.com" -d "DB1"

error:

Starting copy...
SQLState = S0022, NativeError = 207
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'Style Code'.
SQLState = S0022, NativeError = 207
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Invalid column name 'MY Code'.
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Unable to resolve column level collations

I attempted to put double single quotes around the column names, but thats resulted in error as well:

Starting copy...
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near ''.
SQLState = 37000, NativeError = 102
Error = [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Incorrect syntax near ')'.
SQLState = S1000, NativeError = 0
Error = [Microsoft][ODBC Driver 13 for SQL Server]Unable to resolve column level collations

I am following the answer from here because im trying to export the column headers as well to the csv file (otherwise only the data is getting exported if i dont specify the columns in the bcp cmndlet)

I think the error has something to do with the column formatting, because the original answer has non-spaced columns without brackets, but since my columns have spaces in the name, i need to use bracket, but then how do i resolve this error?

Developer technologies | Transact-SQL
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-01-08T19:55:44.987+00:00

    Did you also try the next command?

    bcp "select * from (select 'Style Code' as [Style Code], 'MY Code' as [MY Code] union select [Style Code], [MY Code] from UnPivoted_Table) q order by case [Style Code] when 'Style Code' then 0 ELSE 1 END" queryout "\\server.domain.com\CSV_Files\file1.csv" -c -t, -T -S "server1.db.com" -d "DB1"
    

2 additional answers

Sort by: Most helpful
  1. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-01-08T22:33:34.84+00:00

    Just repeating what I said in another thread: there is an article on my web site where you can learn how to export a file with headers without using UNION: http://www.sommarskog.se/bulkload.html

    0 comments No comments

  2. Erland Sommarskog 121.9K Reputation points MVP Volunteer Moderator
    2021-01-10T12:58:35.16+00:00

    So there is a problem in the forum with this thread. I have received an email notification that Cataster has made a comment, and I can see that there are 8 comments to Viorel's answer, but the forum only displays one. The email notification only has text, so I may be missing any graphics.

    all i want to do is export the data from the view so i can reimport it into a table. the table will have the same data type, the same columns, the same structure. Its all in the same SQL database/instance.

    In that case, you should use native format and you should not bother about column headers. As you may have realised by now, adding headers with BCP is an exercise on the more advanced side.

    When exporting for data for this purpose, you should use native format (since a conversion to text format could cause loss of precision in some cases). You should also use a format file, but don't worry, because it is easy:

    bcp mydb.dbo.myview format null -f myview.fmt -n -T -S server\instance
    bcp mydb.dbo.myiew out myview.bcp -f myview.fmt -T -S server\instance
    

    That is, you use BCP to generate a format file. Why you should use a format file? This is something I learned the hard way... (There can be subtle differences between source and target which the format file can sort out.)

    If you get the error Unable to open BCP host data-file when you trying to import the data, that most likely means that the file you specify does not exist.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.