[英]Is there a SQL function that allows me to export the query result to a .csv automatically?
I'm trying to back up all the tables in a SQL Server Database.我正在尝试备份 SQL 服务器数据库中的所有表。 Here's the logic I'd like to execute:
这是我要执行的逻辑:
For Each TableName in MyDatabase
SELECT *
FROM TableName
Export Results to C:\TableName.csv
Loop
* Disclaimer that none of these will ever be a better option than a native backup in terms of backing of data. *免责声明,就数据支持而言,这些都不会比本机备份更好。 I'm assuming you simply want some flat file backups.
我假设您只是想要一些平面文件备份。
Several manual options are available from generating the scripts and data via Tasks->Generate Scripts
, to manually running a select * from tablename
and then right clicking ->
Copy including header columns into their own excel files.从通过 Tasks-
Tasks->Generate Scripts
脚本和数据,到手动运行select * from tablename
然后右键单击->
Copy including header columns into their own excel files,有几个手动选项可用。
More automated options involve SSIS packages to handle this sort of enumerator logic.更多自动化选项涉及 SSIS 包来处理这种枚举器逻辑。 I believe that the best way to handle this would be a powershell script.
我相信处理此问题的最佳方法是 powershell 脚本。 Please see @Smor's comment to the following answer: SQL Server Management Studio 2012 - Export all tables of database as csv , for a detailed powershell solution.
请参阅@Smor 对以下答案的评论: SQL Server Management Studio 2012 - 将数据库的所有表导出为 csv ,以获取详细的 powershell 解决方案。
The only T-SQL based querying options I'm aware of would be to attempt something of the following:我知道的唯一基于 T-SQL 的查询选项是尝试以下操作:
SELECT
'insert into OPENROWSET(''Microsoft.Jet.OLEDB.4.0'',
''Excel 8.0;Database=@filepath\'' + t.name + '.csv '', --@filepath would be replaced with your logic for determining filename+path
''SELECT * FROM [SheetName$]'')
select * from ' + t.name + ';'
FROM
sys.tables
And executing the results of this script.并执行该脚本的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.