Skip to content

Commit 70fe957

Browse files
committed
Add Export-SQLTableToCSV.ps1 script
1 parent 951d2d8 commit 70fe957

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#requires -version 4
2+
#requires -modules PSLogging
3+
<#
4+
.SYNOPSIS
5+
Export in parallel sql server tables into csv files using bcp utility
6+
.DESCRIPTION
7+
Export in parallel sql server tables into csv files using bcp utility
8+
bcp docs: https://docs.microsoft.com/en-us/sql/tools/bcp-utility
9+
.PARAMETER ServerName <string>
10+
Specifies SQL Server
11+
.PARAMETER DatabaseName <string>
12+
Specifies database name
13+
.OUTPUTS Log File
14+
The script log file stored in C:\Windows\Temp\<name>.log
15+
.NOTES
16+
Version:
17+
Author: Konstantin Taranov k@taranov.pro
18+
Author Modified: Alexander Titenko aleks.titenko@gmail.com
19+
Creation Date: 2017-09-26
20+
Last modified: 2017-09-29
21+
Purpose/Change: Add PSLogging module
22+
.EXAMPLE
23+
Export-SQLTableToCSV -CSVPath D:\12\ -ServerName NL-04 -DatabaseName NIIGAZ -MinRowCount 1 -MaxRowCount 3;
24+
#>
25+
26+
#---------------------------------------------------------[Initialisations]--------------------------------------------------------
27+
28+
#Set Error Action to Silently Continue
29+
$ErrorActionPreference = 'SilentlyContinue'
30+
31+
#Import Modules & Snap-ins
32+
Import-Module PSLogging
33+
34+
#----------------------------------------------------------[Declarations]----------------------------------------------------------
35+
36+
#Script Version
37+
$sScriptVersion = '1.1'
38+
39+
#Log File Info
40+
$sLogPath = 'D:\1\'
41+
$sLogName = 'Export-SQLTableToCSV.log'
42+
$sLogFile = Join-Path -Path $sLogPath -ChildPath $sLogName
43+
44+
#-----------------------------------------------------------[Functions]------------------------------------------------------------
45+
46+
Function Export-SQLTableToCSV {
47+
Param (
48+
[Parameter(Mandatory=$true)]
49+
[string]$CSVPath,
50+
[Parameter(Mandatory=$false)]
51+
[string]$ServerName,
52+
[Parameter(Mandatory=$false)]
53+
[string]$DatabaseName,
54+
[Parameter(Mandatory=$false)]
55+
[string]$CodePage = '-C65001',
56+
[Parameter(Mandatory=$false)]
57+
[string]$MinRowCount,
58+
[Parameter(Mandatory=$false)]
59+
[string]$MaxRowCount
60+
61+
)
62+
Begin {
63+
Write-LogInfo -LogPath $sLogFile -Message "Srart export SQL Tables from $DatabaseName in $CSVPath with bcp utility"
64+
if(-not(Test-Path $CSVPath)){Write-Host "Path $CSVPath doesn't exist!" -foreground:red;
65+
Write-LogInfo -LogPath $sLogFile -Message "Path $CSVPath doesn't exist!"
66+
break;}
67+
68+
}
69+
Process {
70+
Try {
71+
72+
$wd = $CSVPath;
73+
$ProcessName = "Bulk Insert txt files";
74+
75+
$TableDependecy = Invoke-Sqlcmd -ServerInstance $ServerName -Database $DatabaseName -Verbose -Query "
76+
SELECT QUOTENAME(SCHEMA_NAME(t.schema_id)) AS SchemaName
77+
, QUOTENAME(t.name) AS TableName
78+
, SUM(ps.row_count) AS TableRowCount
79+
FROM sys.tables t
80+
INNER JOIN sys.dm_db_partition_stats ps
81+
ON ps.object_id = t.object_id
82+
WHERE index_id < 2
83+
GROUP BY t.name
84+
, t.schema_id
85+
HAVING SUM(ps.row_count) >= $MinRowCount AND SUM(ps.row_count) <= $MaxRowCount
86+
ORDER BY SUM(ps.row_count) ASC
87+
OPTION (RECOMPILE);
88+
";
89+
90+
$bcpStatement = 'bcp "SELECT * FROM ['+ $ServerName + '].[' + $DatabaseName +'].__SCHEMANAME__.__TABLENAME__" queryout "' + $wd + '__SCHEMANAME__.__TABLENAME__.txt" -T -S ' + $ServerName +' -c ' + $CodePage + ' -t"|" -r"\n"';
91+
92+
$ScriptBlock = {
93+
param($bcpRunStatement)
94+
Invoke-Expression $bcpRunStatement
95+
};
96+
97+
98+
$TD = $TableDependecy; # | Where TableLevel -eq $level;
99+
Foreach($t in $TD){
100+
$bcpRunStatement = $bcpStatement.Replace("__SCHEMANAME__", $t[0]).Replace("__TABLENAME__", $t[1]);
101+
Write-Host $bcpRunStatement
102+
Start-Job -ScriptBlock $ScriptBlock -ArgumentList @($bcpRunStatement);
103+
104+
while(@(Get-Job -State Running).Count -gt 6){
105+
Get-Job | Wait-Job -Any | Out-Null
106+
}
107+
}
108+
}
109+
Catch {
110+
Write-LogError -LogPath $sLogFile -Message $_.Exception -ExitGracefully
111+
Break
112+
}
113+
}
114+
End {
115+
If ($?) {
116+
Write-LogInfo -LogPath $sLogFile -Message 'Completed Successfully.'
117+
Write-LogInfo -LogPath $sLogFile -Message ' '
118+
}
119+
}
120+
}
121+
122+
#-----------------------------------------------------------[Execution]------------------------------------------------------------
123+
124+
Start-Log -LogPath $sLogPath -LogName $sLogName -ScriptVersion $sScriptVersion
125+
126+
Export-SQLTableToCSV -CSVPath D:\1\ -ServerName NL-04 -DatabaseName NIIGAZ -MinRowCount 1 -MaxRowCount 3;
127+
128+
Stop-Log -LogPath $sLogFile
129+

0 commit comments

Comments
 (0)