如何使用带有标头的bcp导出到文件

时间:2015-05-27 19:48:09

标签: sql-server-2008 tsql ssms

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

声明@sql varchar(8000) 选择@sql ='bcp'从[Alltrigger]中选择前50 *。[dbo]。[customlook]“queryout \ live-ftp \ sftp_upload \ allfile \ saad_test.txt -c -t ^ | -T -S'+ @@ servername exec master..xp_cmdshell @sql ---头文件 exec master..xp_cmdshell'BCP“选择
      '提供列名'“queryout \ prod-ftp \ sftp_upload \ allstate \ header.csv -c T -t'

请回复,我试图使头文件分离,但它无法正常工作

1 个答案:

答案 0 :(得分:0)

您需要对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