简体   繁体   English

sqlcmd使用SQL服务器存储过程导出数据到csv

[英]Sqlcmd export data to csv using SQL Server stored procedure

When I execute the stored procedure in SQL Server and export data out to .csv (using "Save Result As"), the output .csv was formatted very nicely. When I execute the stored procedure in SQL Server and export data out to .csv (using "Save Result As"), the output .csv was formatted very nicely.

My goal is to extract data in this format, with the headers:我的目标是以这种格式提取数据,带有标题:

PK1,2005/010,#,Not Analysed,CVOI001,C Voice 001 Test Account,PI -Local,11/11/2019,-571.430,C,T00012                        
PK1,2005/010,#,Not Analysed,32400,Annual bonus,PI -Local,11/11/2019,571.430,D,T00012                        

But when I use sqlcmd to extract data from the same stored procedure (the goal is to automate this extraction task), the format is very different with "------" to separate the headers and a lot of padding in the fields.但是当我使用sqlcmd从同一个存储过程中提取数据时(目标是自动化这个提取任务),格式与“------”分隔标题和字段中的大量填充非常不同. Sorry I cannot post the headers as the post will exceed the character limit.抱歉,我无法发布标题,因为帖子将超过字符限制。

Here's my code and the current output.这是我的代码和当前的 output。 Is there anything I should change?有什么我应该改变的吗?

sqlcmd -S SUNSERVER -E -Q "Exec SunSystemsData.[dbo].[z_UniFocusExtract] $(BUCode), $(LCode), $(TCodeNo), $(NoDayBack)" 
       -v BUCode='PK1' LCode='ANAL_T1' TCodeNo='2' NoDayBack='-100' -s "," -o "C:/Test/Test.csv"
PK1    ,2005/010,#              ,Not Analysed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ,CVOI001        ,C Voice 001 Test Account                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        ,PI -Local                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ,11/11/2019          ,              -571.430,C  ,T00012                        
PK1    ,2005/010,#              ,Not Analysed                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ,32400          ,Annual bonus                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ,PI -Local                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       ,11/11/2019          ,               571.430,D  ,T00012                        

You can add one more parameter to remove the headers from the csv file generated.您可以再添加一个参数以从生成的 csv 文件中删除标头。

Sqlcmd msdn link Sqlcmd msdn 链接

-h-1 removes column name headers from the result

-h headers -h 标头
Specifies the number of rows to print between the column headings.指定列标题之间要打印的行数。 The default is to print headings one time for each set of query results.默认为每组查询结果打印一次标题。 This option sets the sqlcmd scripting variable SQLCMDHEADERS.此选项设置 sqlcmd 脚本变量 SQLCMDHEADERS。 Use -1 to specify that headers not be printed.使用 -1 指定不打印标题。 Any value that is not valid causes sqlcmd to generate an error message and then exit.任何无效值都会导致 sqlcmd 生成错误消息然后退出。

For removing the extra padding spaces in the column width, add -W parameter.要删除列宽中的额外填充空间,请添加 -W 参数。 I have tested it.我已经测试过了。 It is working fine.它工作正常。

-W This option removes trailing spaces from a column. -W 此选项从列中删除尾随空格。 Use this option together with the -s option when preparing data that is to be exported to another application.准备要导出到另一个应用程序的数据时,将此选项与 -s 选项一起使用。 Cannot be used with the -y or -Y options.不能与 -y 或 -Y 选项一起使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM