【问题标题】:How to export to file with bcp with header如何使用带有标题的 bcp 导出到文件
【发布时间】:2015-05-27 19:48:09
【问题描述】:

您好,我有查询工作文件要导出到 .txt 文件,我只想添加标题,任何帮助都会非常有用,列名非常有用

声明@sql varchar(8000) select @sql = 'bcp "select top 50 * from [Alltrigger].[dbo].[customlook]" queryout \live-ftp\sftp_upload\allfile\saad_test.txt -c -t^| -T -S' + @@服务器名 执行大师..xp_cmdshell @sql --- 头文件 exec master..xp_cmdshell 'BCP "选择
'提供的列名' " queryout \prod-ftp\sftp_upload\allstate\header.csv -c T -t'

请回复,我已尝试将头文件分开,但它不起作用

【问题讨论】:

    标签: sql-server-2008 tsql ssms


    【解决方案1】:

    您需要为 bcp 使用 queryout 选项,并通过字符串文字构建您的列列表。您也可以摆脱动态生成的 SQL。

    bcp "select '[guid]', 'uploadDate', 'fileName', 'email', 'profileTypeID', 'firstName', 'lastName', 'prospectID', 'prospe‌​ctingPermission', 'relationshipPermission', 'advicePermission', 'transactionalPermission', 'p‌​olicy', 'postalCode', 'language', 'phone', 'businessPhone', 'cell', 'country', 'province', 'city', 'expiryDate', 't‌​ransactionDate', 'source', 'consentBy', 'agentNumber', 'aia', 'suppression' union all select * from [Alltrigger].[dbo].[custom]" queryout \\prod-ftp\sftp_upload\allstate\mark_test.txt -c -t^| -T -t
    

    【讨论】:

    • 嗨,格雷格,我试过了,它不工作,所以它就像 select 'col','col2' 现在最后我是否必须从 any_table 中做...请建议
    • 请看我上面的repro。有UNION ALL。如果您仍然感到困惑,请分享您刚刚尝试过的完整陈述。
    • 嗨,格雷格,感谢您的回复,我做到了,但在第一列给我错误,我应该把我的代码发给你吗
    • 是的,发布您的复制品,这是查看问题所在的唯一方法。
    • declare atsql varchar(8000) select atsql = 'bCP "select '[guid]', 'uploadDate','fileName',email,profileTypeID,firstName,lastName,prospectID,prospectingPermission,relationshipPermission,advicePermission ,transactionalPermission,policy,postalCode,language,phone,businessPhone,cell,country,province,city,expiryDate,transactionDate,source,consentBy,agentNumber,aia,suppression union all select * from [Alltrigger].[dbo].[custom] " queryout \\prod-ftp\sftp_upload\allstate\mark_test.txt -c -t^| -T -t' + atatservername exec master..xp_cmdshell atsql (at = @)
    猜你喜欢
    • 2020-07-01
    • 1970-01-01
    • 2020-01-17
    • 2010-11-24
    • 1970-01-01
    • 2022-12-18
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多