@@ -44,7 +44,7 @@ function Write-InsertOrUpdate {
44
44
File name: Write-InsertOrUpdate.ps1
45
45
Author: Florian Carrier
46
46
Creation date: 15/10/2019
47
- Last modified: 10 /02/2020
47
+ Last modified: 26 /02/2020
48
48
#>
49
49
[CmdletBinding (
50
50
SupportsShouldProcess = $true
@@ -111,6 +111,36 @@ function Write-InsertOrUpdate {
111
111
Process {
112
112
switch ($Vendor ) {
113
113
" Oracle" {
114
+ # Define existence check
115
+ foreach ($Key in $PrimaryKey ) {
116
+ if ($PrimaryKeyCheck -eq $null ) { $PrimaryKeyCheck = " $Key = $ ( $Fields .$Key ) " }
117
+ else { $PrimaryKeyCheck += " AND $Key = $ ( $Fields .$Key ) " }
118
+ }
119
+ $Check = [System.String ]::Concat(" MERGE INTO $Table USING dual ON (" , $PrimaryKeyCheck , " )" )
120
+
121
+ # Loop through fields
122
+ foreach ($Field in $Fields.GetEnumerator ()) {
123
+ # Select update values
124
+ if ($Field.Key -NotIn $PrimaryKey ) {
125
+ if ($UpdateValues -eq $null ) { $UpdateValues = " $ ( $Field.Key ) = $ ( $Field.Value ) " }
126
+ else { $UpdateValues += " , $ ( $Field.Key ) = $ ( $Field.Value ) " }
127
+ }
128
+ # Set insert fields
129
+ if ($InsertFields -eq $null ) { $InsertFields = " $ ( $Field.Key ) " }
130
+ else { $InsertFields += " , $ ( $Field.Key ) " }
131
+ # Set insert values
132
+ if ($InsertValues -eq $null ) { $InsertValues = " $ ( $Field.Value ) " }
133
+ else { $InsertValues += " , $ ( $Field.Value ) " }
134
+ }
135
+
136
+ # Construct update query
137
+ $Update = [System.String ]::Concat(" WHEN MATCHED THEN UPDATE SET " , $UpdateValues )
138
+ # Construct insert query
139
+ $Insert = [System.String ]::Concat(" WHEN NOT MATCHED THEN INSERT (" , $InsertFields , " ) VALUES (" , $InsertValues , " )" )
140
+
141
+ # Construct whole SQL query
142
+ $Query = [System.String ]::Concat($Check , " `n " , $Update , " `n " , $Insert )
143
+
114
144
# TODO
115
145
}
116
146
" SQLServer" {
0 commit comments