Skip to content

Instantly share code, notes, and snippets.

@andreasbotsikas
Created December 15, 2016 01:21
Show Gist options
  • Save andreasbotsikas/8e23146f23166cf422d24291efc5f89a to your computer and use it in GitHub Desktop.
Save andreasbotsikas/8e23146f23166cf422d24291efc5f89a to your computer and use it in GitHub Desktop.
Export tables to csv via powershell
# These databases contain a dbo.VfiTagNumHistory and a dbo.VfiTagStrHistory table
$Database = @("VfiTag","VfiTag_0_1427961614083","VfiTag_1427961614084_1397948399843","VfiTag_1427961614084_1403650799757","VfiTag_1427961614084_1409957999664","VfiTag_1427961614084_1415401199875","VfiTag_1427961614084_1420239599988","VfiTag_1427961614084_1423609199863","VfiTag_1427961614084_1425941999989")
# For each database
$Database | foreach {
# export num history to csv using the bcp tool
$command = "cmd.exe /C ""C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\bcp.exe"" $_.dbo.VfiTagNumHistory out C:\Work\Personal\Optimus\$($_)_Num.csv /S. /c /t, -T"
Invoke-Expression -Command:$command
#export the string history to csv using the bcp tool
$command = "cmd.exe /C ""C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\bcp.exe"" $_.dbo.VfiTagStrHistory out C:\Work\Personal\Optimus\$($_)_Str.csv /S. /c /t, -T"
Invoke-Expression -Command:$command
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment