Powershell将csv导入SQL查询,导出多个csv

时间:2016-11-04 05:14:34

标签: sql powershell variables export

我正在努力解决这个问题,我只是刚开始在SQL中查看脚本,而且我的powershell非常有限。要求基本上是这样的:

Utilisng Powershell,导入一个csv文件,其中包含一个需要通过循环提供给多个SQL查询的列,为每个不同的查询导出单独的csv文件。

导入csv的示例: 项目(标题) 1000 1001 1002

Powershell的:

$importProjectsCSV = e:\Projects.csv
$server = servername
$database = database

import-csv $importProjectsCSV | ForEach-Object {
$query = "
Select ProjectLeader, ProjectTitle
FROM dbo.PROJECTS
Where Project = $_.Project;

Select ProjectClient, Name
FROM dbo.CLIENTS
Where Project = $_.Project;

$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandText = $query
$command.Connection = $connection

$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
$SqlAdapter.SelectCommand = $command
$DataSet = New-Object System.Data.DataSet
$SqlAdapter.Fill($DataSet)
$connection.Close()


$dataset.Table[0] | Export-csv "E:\" + $_.ProjectName + ".csv"
$dataset.Table[1] | Export-csv "E:\" + $_.ProjectName + ".csv"

问题是变量不会进入SQL查询。 有没有更好的方法来处理这种类型的例子?

感谢任何指针

保罗。

2 个答案:

答案 0 :(得分:0)

如果我得到了所有权利,你可以用这样的方式做到这一点:

$Projects = Import-Csv -Path 'C:\Temp\Projects.csv'

ForEach ($Project in $Projects.Projects) {
    $Query= @" 
Select ProjectLeader, ProjectTitle
FROM dbo.PROJECTS
Where Project = '%{0}';
"@ -f $Project
    # just dummy action
    $results = Invoke-Sqlcmd -ServerInstance "foo" -Database "bar" -Query $Query
    # In Case results is a dataset do something like
    $results | Export-Csv -Path ("E:\{0}.csv" -f $Project ) 
}

答案 1 :(得分:0)

我会做这样的事情:

我必须承认我还没能测试它,而且我个人通常使用c#来查询sql服务器。所以我可能在某处犯了一点错误。

$importProjectsCSV = e:\Projects.csv
$server = servername
$database = database

$Projects =  Import-Csv -Path $importProjectsCSV | % {$_.Project}
$DS_Projects = New-Object System.Data.DataSet
$DS_Clients = New-Object System.Data.DataSet

$query_pro = "Select ProjectLeader, ProjectTitle, Project FROM dbo.PROJECTS";
$query_Clients = "Select ProjectClient, Name, Project FROM dbo.CLIENTS";

$connectionTemplate = "Data Source={0};Integrated Security=SSPI;Initial Catalog={1};"
$connectionString = [string]::Format($connectionTemplate, $server, $database)
$connection = New-Object System.Data.SqlClient.SqlConnection
$connection.ConnectionString = $connectionString

$connection.Open()
$command = $connection.CreateCommand();
$command.CommandText = $query_pro;
$sqlAdap = New-Object System.Data.SqlClient.SqlDataAdapter($command)
$sqlAdap.Fill($DS_Projects)
$command2 = $connection.CreateCommand();
$command2.CommandText = $query_Clients;
$sqlAdap2 = New-Object System.Data.SqlClient.SqlDataAdapter($command2)
$sqlAdap2.Fill($DS_Clients)
$connection.Close();

foreach($project in $Projects)
{
    $DS_Projects.Tables[0].Select("Project=$project") | Export-Csv "E:\$project.csv"
    $DS_Clients.Tables[0].Select("Project=$project") | Export-Csv "E:\$project.csv"
}