如何在 bcp 命令中指定间隔的列名?

How to specify spaced out column names in bcp 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"

错误:

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

我正在关注 的回答,因为我正在尝试将列 headers 也导出到 csv 文件(否则,如果我没有在bcp 命令)

我认为错误与列格式有关,因为原始答案有 non-spaced 列没有括号,但由于我的列名称中有空格,我需要使用括号,但随后我该如何解决这个错误?

我认为您必须在列名称周围设置引号。 运行 Management Studio 中的查询需要单引号。要使用 bcp 运行 它,您需要双单引号。

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"

正确的答案是在它们周围设置单引号,就像这样

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"