PowerShell脚本将SQL导出为CSV添加定界符

时间:2018-12-12 11:20:07

标签: powershell

我正在做一个脚本,以从SQL Server数据库导出数据。我想输出带有定界符“;”的.csv文件。

这是我的剧本:

#Variable to hold variable  
$SQLServer = "SERVEUR"  
$SQLDBName = "TOTO"  
$uid ="PS"  
$pwd = "password123"   
$delimiter = ";"

#SQL Query  
$SqlQuery = "SELECT * from $SQLDBName.dbo.SAGE_TO_PRESTASHOP;"  
$SqlConnection = New-Object System.Data.SqlClient.SqlConnection  
$SqlConnection.ConnectionString = "Server = $SQLServer; Database = $SQLDBName; Integrated Security = True;"  
$SqlCmd = New-Object System.Data.SqlClient.SqlCommand  
$SqlCmd.CommandText = $SqlQuery  
$SqlCmd.Connection = $SqlConnection  
$SqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter  
$SqlAdapter.SelectCommand = $SqlCmd   
#Creating Dataset  
$DataSet = New-Object System.Data.DataSet  
$SqlAdapter.Fill($DataSet)  
$DataSet.Tables[0] | Out-File "E:\EXPORTS\export.csv"  

1 个答案:

答案 0 :(得分:1)

用以下内容替换您的底线:

$DataSet.Tables[0] | export-csv -Delimiter $delimiter -Path "E:\EXPORTS\export.csv" -NoTypeInformation