简体   繁体   English

如何在SQL Server查询中将多个选择语句导出到不同的csv文件

[英]How to export multiple select statements to different csv files in SQL Server query

I have a long SQL server query, which runs on different tables, does some pre-processing and spits out different select table outputs. 我有一个很长的SQL Server查询,该查询可在不同的表上运行,进行一些预处理并吐出不同的选择表输出。 I want them to be stored in different csv files. 我希望它们存储在不同的csv文件中。

simplified eg. 简化的

-- some declare variables and other pre-processing,
-- then save the outputs to different CSV files
select * from Table1 -- save to Table1_Output.csv
select * from Table2 -- save to Table2_Output.csv

Now, I can use this to run SQLCmd and save the output for a single select statement to a single CSV. 现在,我可以使用来运行SQLCmd并将单个select语句的输出保存到单个CSV。 I need to be able to save the various outputs to different CSVs 我需要能够将各种输出保存到不同的CSV

The first option in the solution above is not viable as that requires manual intervention and cannot be used in a scripting environment. 上面的解决方案中的第一个选项不可行,因为它需要手动干预并且不能在脚本环境中使用。

Thanks 谢谢

Personally, I'd just use PowerShell if you can't use SSIS: 就个人而言,如果您不能使用SSIS,我将只使用PowerShell:

$SQLServer = 'SQLSERVERNAME';
# If you have a non-default instance such as with SQL Express, use this format
# $SQLServer = 'SQLSERVERNAME\SQLINSTANCENAME';
$Database = 'MyDatabase';

$ConnectionString = "Data Source={0};Initial Catalog={1};Integrated Security=SSPI" -f $SQLServer, $Database;
$CommandText = @'
DECLARE @ID int;
SET @ID  = 123456;
SELECT ID, First_Name FROM dbo.Person WHERE ID = @ID;
SELECT ID, Last_Name FROM dbo.Person WHERE ID = @ID;
'@;

$SqlConnection = New-Object System.Data.SqlClient.SqlConnection;
$SqlConnection.ConnectionString = $ConnectionString;

$SqlCommand = New-Object System.Data.SqlClient.SqlCommand;
$SqlCommand.CommandText = $CommandText;
$SqlCommand.Connection = $SqlConnection;

$SqlDataAdapter = New-Object System.Data.SqlClient.SqlDataAdapter;
$SqlDataAdapter.SelectCommand = $SqlCommand;

$DataSet = New-Object System.Data.DataSet;

$SqlConnection.Open();
$SqlDataAdapter.Fill($DataSet) | Out-Null;
$SqlConnection.Close();
$SqlConnection.Dispose();

$DataSet.Tables[0] | Export-Csv -NoTypeInformation -Path 'U:\FirstNames.csv';
$DataSet.Tables[1] | Export-Csv -NoTypeInformation -Path 'U:\LastNames.csv';

When multiple results are returned, sqlcmd prints a blank line between each result set in a batch. 返回多个结果时, sqlcmd在批处理中的每个结果集之间打印空白行。 The output can then be processed with sed '/^$/q' or csplit -s - '/^$/' '{*}' to split into individual files: How to split file on first empty line in a portable way in shell . 然后可以使用sed '/^$/q'csplit -s - '/^$/' '{*}'来处理输出,以分割成单个文件: 如何以可移植的方式在第一个空行中分割文件外壳

UPD If sygwin is not an option custom VBScript can run sqlcmd through WScript.CreateObject("WScript.Shell") .Exec and process output line-by-line with .StdOut.ReadLine() until .StdOut.AtEndOfStream : Getting command line output in VBScript (without writing to files) UPD如果不是sygwin选项,则自定义VBScript可以通过WScript.CreateObject("WScript.Shell") .Exec运行sqlcmd,并使用.StdOut.ReadLine() .StdOut.AtEndOfStream处理输出,直到.StdOut.AtEndOfStream获取命令行输出在VBScript中(不写入文件)

An alternative approach is to use SSIS . 另一种方法是使用SSIS SQL Server Integration Services is great tool for moving data in, around and out of databases. SQL Server Integration Services是用于将数据移入数据库或从数据库移出的出色工具。 It's also easy to schedule SSIS packages, using the SQL Agent, should you wish to automate. 如果您希望实现自动化,则可以使用SQL代理轻松计划SSIS包。

This would require a little more effort up front. 这将需要更多的努力。 You would need to break your existing batch into several smaller SQL statements. 您需要将现有的批处理分成几个较小的SQL语句。

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

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