2

I have a task to save the results of a SQL Server query into a .csv file. After some googling I decided to use PowerShell. I found a script, modified it a bit, it works and almost all is ok.

$server = "server"
$database = "database"
$query = "SELECT * from et_thanks"

$tod = Get-Date;
$file = "{0:yyyyMMdd}_go.csv" -f $tod;
$extractFile = @"
\\info\export_files\$file
"@

$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.Tables[0] | Export-Csv -Force -Delimiter ";" $extractFile

But I have 2 problems which I can't solve:

  1. When I open the .csv file I see columns headers and commented string on first line:

    #TYPE System.Data.DataRow
    "ob_no","c_name","c_visible","c_fp","e_from","e_to"
    "436439","09.09.2013 11:29:08","0","","10937","260153"
    

    How can I get rid of it?

  2. All values are surrounded with quotes. Is it possible to modify script not to use it while exporting? Autoreplace isn't good idea, cause there is a possibility that quote symbol can be found in sql data.

I tried to find answers in documentation (http://ss64.com/ps/export-csv.html) but with no success.

3 Answers 3

3

You might run in to trouble removing the quotes, but if that's what you really want then the following should achieve it.

-NoTypeInformation will remove the additional type information you are seeing.

($DataSet.Tables[0] | ConvertTo-Csv -Delimiter ";" -NoTypeInformation) -replace "`"", "" | `
Out-File -Force $extractFile

This uses convertto-csv to convert to a string representation of the csv followed by replacing all instances of " with nothing and the final string is piped to Out-File.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks David, -1 problem. It seems to me that it's impossible to get read of quotes and column name, cause export goes via csv-standarts. Export-Csv cmdlet not the very best idea for my task.
1

...and, to get rid of the header record, if you first convert the data to csv (Convert-Csv), then pipe those results to Select to skip the 1st record:

($DataSet.Tables[0] | ConvertTo-Csv -Delimiter "`t" -NoTypeInformation ) -Replace "`"","" | Select -skip 1 | Out-File blahblahblah...

Comments

0

Agreed export-csv isn't the best tool for the job. I would use sqlcmd.exe or bcp.exe provided SQL Server command-lines tools are installed. You could also build a simple routine to create a CSV from a datatable:

$result = new-Object text.stringbuilder
$dt = $DataSet.Tables[0]

foreach ($dr in $dt.Rows) {
           for ($i = 0; $i -lt $dt.Columns.Count; $i++) {

                $null = $result.Append($($dr[$i]).ToString())
                $null = $result.Append($(if ($i -eq $dt.Columns.Count - 1) {"`n" } else { ","} ))
            }
}

$result.ToString()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.