如何使用 sqlcmd 从 SQL Server 将数据导出为 CSV 格式?

2023-07-17数据库问题
0

本文介绍了如何使用 sqlcmd 从 SQL Server 将数据导出为 CSV 格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我可以很容易地将数据转储到文本文件中,例如:

I can quite easily dump data into a text file such as:

sqlcmd -S myServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" 
     -o "MyData.txt"

但是,我查看了 SQLCMD 的帮助文件,但没有看到专门用于 CSV 的选项.

However, I have looked at the help files for SQLCMD but have not seen an option specifically for CSV.

有没有办法使用 SQLCMD 将表中的数据转储到 CSV 文本文件中?

Is there a way to dump data from a table into a CSV text file using SQLCMD?

推荐答案

你可以这样运行:

sqlcmd -S MyServer -d myDB -E -Q "select col1, col2, col3 from SomeTable" 
       -o "MyData.csv" -h-1 -s"," -w 700

  • -h-1 从结果中删除列名标题
  • -s"," 将列分隔符设置为 ,
  • -w 700 将行宽设置为 700 个字符(这需要与最长的行一样宽,否则会换行到下一行)
    • -h-1 removes column name headers from the result
    • -s"," sets the column seperator to ,
    • -w 700 sets the row width to 700 chars (this will need to be as wide as the longest row or it will wrap to the next line)
    • 这篇关于如何使用 sqlcmd 从 SQL Server 将数据导出为 CSV 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

      The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

按天分组的 SQL 查询
SQL query to group by day(按天分组的 SQL 查询)...
2024-04-16 数据库问题
77

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

在 Group By 查询中包含缺失的月份
Include missing months in Group By query(在 Group By 查询中包含缺失的月份)...
2024-04-16 数据库问题
12

sql group by 与不同
sql group by versus distinct(sql group by 与不同)...
2024-04-16 数据库问题
37

如何在SQL中返回每个组的增量组号
How to return a incremental group number per group in SQL(如何在SQL中返回每个组的增量组号)...
2024-04-16 数据库问题
8