@@ -8,15 +8,23 @@ $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
9
$logPath = " C:\Script_Utility\log\"
10
10
11
- # Run the Schema.sql file againist the source database to get the metadata for the import
12
- $DS = Invoke-Sqlcmd - ServerInstance $ServerName - User $UserName - Password $Password - Database $SourceDatabase - InputFile $fileName -As DataSet
11
+ # Get Schema Data from the database
12
+ $DS = Invoke-Sqlcmd - MaxCharLength 150000 - ServerInstance $ServerName - User $UserName - Password $Password - Database $SourceDatabase - InputFile $fileName -As DataSet
13
13
14
14
# For each row
15
15
foreach ($sql in $DS.Tables [0 ].Rows){
16
16
Write-Output $sql.TableName
17
17
18
18
# Execute file on each database
19
- Invoke-Sqlcmd - ServerInstance $DestinationServer - Database $DestinationDB - MaxCharLength 8000 - Query $sql.SqlStatement
19
+ Invoke-Sqlcmd - ServerInstance $DestinationServer - Database $DestinationDB - Query $sql.SqlStatement - OutputSqlErrors $true - verbose
20
+
21
+ if ($error.count -gt 0 )
22
+ {
23
+ $error | Out-File - FilePath " $ ( $logPath ) $ ( $sql.TableName ) _sqlout.txt"
24
+ $sql.SqlStatement | Out-File - FilePath " $ ( $logPath ) $ ( $sql.TableName ) _sqlout.txt"
25
+
26
+ $error.Clear ()
27
+ }
20
28
Write-Output $sql.SqlStatement
21
29
22
30
# If we just created the table, let's import the data before applying an constraints or indexes
@@ -32,7 +40,7 @@ foreach($sql in $DS.Tables[0].Rows){
32
40
Write-Output $bcp
33
41
34
42
# add code to import in to use format file
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 ) _in_err.txt"
43
+ $bcp = " bcp $ ( $sql.SchemaName ) .$ ( $sql.TableName ) in $ ( $tempPath ) $ ( $sql.TableName ) .bcp -b 100 - S $ ( $DestinationServer ) -d $ ( $DestinationDB ) -T -t -E -f $ ( $tempPath ) $ ( $sql.TableName ) .fmt -e $ ( $logPath ) $ ( $sql.TableName ) _in_err.txt"
36
44
Invoke-Expression $bcp
37
45
Write-Output $bcp
38
46
0 commit comments