【问题标题】:exec xp_cmdshell bcp syntaxexec xp_cmdshell bcp 语法
【发布时间】:2018-01-25 01:09:59
【问题描述】:

我似乎找不到正确的语法来使用 sql server management studio 中的 exec xp_cmdshell bcp 导出具有列名的数据。我尝试了以下变体

EXEC xp_cmdshell bcp "select "a_id","b_id","c_id" union select a_id,b_id,c_id from  tablename out 
"\\network_path\a.txt" -c -Uusername -Ppassword -Sservername"

还有

EXEC xp_cmdshell bcp 'select 'a_id','b_id','c_id' union select a_id,b_id,c_id from  tablename out 
'\\network_path\a.txt' -c -Uusername -Ppassword -Sservername'

EXEC xp_cmdshell bcp 'select "a_id","b_id","c_id" union select a_id,b_id,c_id from  tablename out 
"\\network_path\a.txt" -c -Uusername -Ppassword -Sservername'

EXEC xp_cmdshell bcp "select 'a_id','b_id','c_id' union select a_id,b_id,c_id from  tablename out 
'\\network_path\a.txt' -c -Uusername -Ppassword -Sservername"

我已成功使用以下命令导出表格,但我还需要列名。

bcp tablename out "\\network_path\a_test.txt" -c -Uusername -Ppassword -Sservername'

【问题讨论】:

    标签: sql-server bcp xp-cmdshell


    【解决方案1】:

    好的,您实际上需要对标题行进行排序,使其显示在顶部。这应该处理它

    EXEC xp_cmdshell 'bcp "select * from (select ''a_id'' as a_id,''b_id'' as b_id,''c_id'' as c_id union select a_id,b_id,c_id from tablename)q order by case a_id when ''a_id'' then 0 ELSE 1 END" queryout "\\networkpath\a.txt" -c -Uusername -Ppassword -Sservername -ddatabasename'
    

    【讨论】:

    • 用法:bcp {dbtable |查询} {在 |出|查询 |格式} 数据文件 [-m maxerrors] [-f 格式文件] [-e errfile] [-F firstrow] [-L lastrow] [-b batchsize] [-n 本机类型] [-c 字符类型] [-w 宽字符类型] [-N 保留非文本本机] [-V 文件格式版本] [-q 引用标识符] [-C 代码页说明符] [-t 字段终止符] [-r 行终止符] [-i 输入文件] [- o outfile] [-a packetsize] [-S 服务器名称] [-U 用户名] [-P 密码] [-T 可信连接] [-v 版本] [-R 区域启用] [-k 保持空值] [- E 保持身份值] [-h "load hints"]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多