Export Double Quoted CSV File Through Powershell



Import-Module -Name 'SQLPS' -DisableNameChecking  
$dt = Get-Date -Format yyyyMMddHHmmss
$workdir='C:\temp'

$svrname = "MC047012\MSSQL2014"
 $db ='test1'

 
#Change default timeout time from 600 to unlimited
$svr =  new-object ('Microsoft.SqlServer.Management.Smo.Server') $svrname 
$svr.ConnectionContext.StatementTimeout = 0

 
#$Path = "$env:temp\myfile$($dt).txt"
$Path = "$($workdir)\myfile.txt"
 
 
$q = @"
select * from  
dbo.myFiles
"@
Invoke-SQLCmd -ServerInstance $svr.Name  -Database $db -Query $q |  Export-Csv -NoTypeInformation -Path $Path

Invoke-Item -Path $Path  
 
 
 --bcp import  the double quoted csv file
 ---https://jingyangli.wordpress.com/2014/10/27/how-to-import-csv-file-with-double-quotes-text-qualifiers-through-bcp/



 
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s