Export table from database to csv file(将表从数据库导出到 csv 文件)
问题描述
我想:将表从 sql server 数据库导出到逗号分隔的 csv
文件,而不使用 sql server 导入导出向导
I want to:
Export table from sql server database to a comma delimited csv
file without using sql Server import export wizard
我想使用查询来做,因为我想在自动化中使用查询
I want to do it using a query because I want to use the query in automation
有可能吗?我搜索了这个,但没有找到好的答案
Is it possible? I searched for that and didn't find a good answer
推荐答案
一些想法:
1. Run a SELECT statement to filter your data
2. Click on the top-left corner to select all rows
3. Right-click to copy all the selected
4. Paste the copied content on Microsoft Excel
5. Save as CSV
使用 SQLCMD(命令提示符)
示例:
从命令提示符,您可以运行查询并将其导出到文件:
From the command prompt, you can run the query and export it to a file:
sqlcmd -S . -d DatabaseName -E -s, -W -Q "SELECT * FROM TableName" > C:Test.csv
不要引用分隔符,只使用 -s,而不是引用 -s',' 除非你想将引用设置为分隔符.
Do not quote separator use just -s, and not quotes -s',' unless you want to set quote as separator.
更多信息在这里:ExcelSQLServer
注意事项:
这种方法将在文件底部包含受影响的行"信息,但您可以通过在查询本身中使用SET NOCOUNT ON"来摆脱这一点.
This approach will have the "Rows affected" information in the bottom of the file, but you can get rid of this by using the "SET NOCOUNT ON" in the query itself.
您可以运行存储过程而不是实际查询(例如EXEC Database.dbo.StoredProcedure")
You may run a stored procedure instead of the actual query (e.g. "EXEC Database.dbo.StoredProcedure")
示例:
bcp "SELECT * FROM Database.dbo.Table" queryout C:Test.csv -c -t',' -T -S .SQLEXPRESS
将逗号分隔符引用为 -t',' 而非 -t, 很重要
It is important to quote the comma separator as -t',' vs just -t,
此处的更多信息:bcp 实用程序
注意事项:
- 在使用 SQLCMD 时,您可以运行存储过程而不是实际查询
- 您可以使用任何编程语言或批处理文件来自动执行此操作
希望这会有所帮助.
这篇关于将表从数据库导出到 csv 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将表从数据库导出到 csv 文件


- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 更改自动增量起始编号? 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01