How to export data as CSV format from SQL Server using sqlcmd?

后端 未结 11 1683
天涯浪人
天涯浪人 2020-11-22 13:06

I can quite easily dump data into a text file such as:

sqlcmd -S myServer -d myDB -E -Q \"select col1, col2, col3 from SomeTable\" 
     -o \"MyData.txt\"
         


        
11条回答
  •  死守一世寂寞
    2020-11-22 13:34

    This answer builds on the solution from @iain-elder, which works well except for the large database case (as pointed out in his solution). The entire table needs to fit in your system's memory, and for me this was not an option. I suspect the best solution would use the System.Data.SqlClient.SqlDataReader and a custom CSV serializer (see here for an example) or another language with an MS SQL driver and CSV serialization. In the spirit of the original question which was probably looking for a no dependency solution, the PowerShell code below worked for me. It is very slow and inefficient especially in instantiating the $data array and calling Export-Csv in append mode for every $chunk_size lines.

    $chunk_size = 10000
    $command = New-Object System.Data.SqlClient.SqlCommand
    $command.CommandText = "SELECT * FROM "
    $command.Connection = $connection
    $connection.open()
    $reader = $command.ExecuteReader()
    
    $read = $TRUE
    while($read){
        $counter=0
        $DataTable = New-Object System.Data.DataTable
        $first=$TRUE;
        try {
            while($read = $reader.Read()){
    
                $count = $reader.FieldCount
                if ($first){
                    for($i=0; $i -lt $count; $i++){
                        $col = New-Object System.Data.DataColumn $reader.GetName($i)
                        $DataTable.Columns.Add($col)
                    }
                    $first=$FALSE;
                }
    
                # Better way to do this?
                $data=@()
                $emptyObj = New-Object System.Object
                for($i=1; $i -le $count; $i++){
                    $data +=  $emptyObj
                }
    
                $reader.GetValues($data) | out-null
                $DataRow = $DataTable.NewRow()
                $DataRow.ItemArray = $data
                $DataTable.Rows.Add($DataRow)
                $counter += 1
                if ($counter -eq $chunk_size){
                    break
                }
            }
            $DataTable | Export-Csv "output.csv" -NoTypeInformation -Append
        }catch{
            $ErrorMessage = $_.Exception.Message
            Write-Output $ErrorMessage
            $read=$FALSE
            $connection.Close()
            exit
        }
    }
    $connection.close()
    

提交回复
热议问题