@@ -6,6 +6,7 @@ $SourceDatabase = "Database to copy from"
6
6
$DestinationDB = " Database to copy to"
7
7
$fileName = " ..\Schema.sql" # Path to the Schema.sql file included in this repo
8
8
$tempPath = " ..\BCP\" # A temp directory on a drive with enough free space to save the bcp files during the export and import operations
9
+ $logPath = " C:\Script_Utility\log\"
9
10
10
11
# Run the Schema.sql file againist the source database to get the metadata for the import
11
12
$DS = Invoke-Sqlcmd - ServerInstance $ServerName - User $UserName - Password $Password - Database $SourceDatabase - InputFile $fileName -As DataSet
@@ -18,24 +19,38 @@ foreach($sql in $DS.Tables[0].Rows){
18
19
Invoke-Sqlcmd - ServerInstance $DestinationServer - Database $DestinationDB - MaxCharLength 8000 - Query $sql.SqlStatement
19
20
Write-Output $sql.SqlStatement
20
21
21
- if ($sql.TableName -ne " Schema" -and $sql.TableName -ne " Alter" )
22
+ # If we just created the table, let's import the data before applying an constraints or indexes
23
+ if ($sql.ScriptType -eq " Table" )
22
24
{
23
25
# add code to create format file
24
- $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) format nul -n -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt -S $ ( $ServerName ) -d $ ( $SourceDatabase ) -U $ ( $UserName ) -P $ ( $Password ) "
26
+ $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) format nul -n -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt -S $ ( $ServerName ) -d $ ( $SourceDatabase ) -U $ ( $UserName ) -P $ ( $Password ) -e $ ( $logPath ) $ ( $sql .TableName ) _fmt_err.txt "
25
27
Invoke-Expression $bcp
26
28
Write-Output $bcp
27
29
28
- $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) out $ ( $tempPath ) $ ( $sql.TableName ) .bcp -S $ ( $ServerName ) -d $ ( $SourceDatabase ) -U $ ( $UserName ) -P $ ( $Password ) -E -n -t"
30
+ $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) out $ ( $tempPath ) $ ( $sql.TableName ) .bcp -S $ ( $ServerName ) -d $ ( $SourceDatabase ) -U $ ( $UserName ) -P $ ( $Password ) -E -n -t -e $ ( $logPath ) $ ( $sql .TableName ) _in_err.txt "
29
31
Invoke-Expression $bcp
30
32
Write-Output $bcp
31
33
32
34
# add code to import in to use format file
33
- $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) in $ ( $tempPath ) $ ( $sql.TableName ) .bcp -S $ ( $DestinationServer ) -d $ ( $DestinationDB ) -T -t -E -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt"
35
+ $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) in $ ( $tempPath ) $ ( $sql.TableName ) .bcp -S $ ( $DestinationServer ) -d $ ( $DestinationDB ) -T -t -E -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt -e $ ( $logPath ) $ ( $sql .TableName ) _out_err.txt "
34
36
Invoke-Expression $bcp
35
37
Write-Output $bcp
36
38
37
39
# Remove temp data file before moving on
38
40
remove-item - path " $ ( $tempPath ) $ ( $sql.TableName ) .bcp"
39
41
remove-item - path " $ ( $tempPath ) $ ( $sql.TableName ) .fmt"
42
+
43
+ # Clean up Logs
44
+ if ((Get-Item " $ ( $logPath ) $ ( $sql.TableName ) _out_err.txt" ).Length -eq 0 ){
45
+ Remove-Item " $ ( $logPath ) $ ( $sql.TableName ) _out_err.txt"
46
+ }
47
+
48
+ if ((Get-Item " $ ( $logPath ) $ ( $sql.TableName ) _in_err.txt" ).Length -eq 0 ){
49
+ Remove-Item " $ ( $logPath ) $ ( $sql.TableName ) _in_err.txt"
50
+ }
51
+
52
+ if ((Get-Item " $ ( $logPath ) $ ( $sql.TableName ) _fmt_err.txt" ).Length -eq 0 ){
53
+ Remove-Item " $ ( $logPath ) $ ( $sql.TableName ) _fmt_err.txt"
54
+ }
40
55
}
41
56
}
0 commit comments