@@ -49,13 +49,12 @@ public override string BuildMoveDataSql<T>(
4949 throw new InvalidOperationException ( "Table has no primary key that can be used for conflict detection." ) ;
5050 }
5151
52- q . AppendLine ( $ "MERGE INTO { target . QuotedTableName } AS { PseudoTableInserted } ") ;
52+ q . AppendLine ( $ "MERGE INTO { target . QuotedTableName } { PseudoTableInserted } ") ;
5353
5454 q . Append ( "USING (SELECT " ) ;
5555 q . AppendColumns ( insertedColumns ) ;
56- q . Append ( $ " FROM { source } ) AS { PseudoTableExcluded } (") ;
57- q . AppendColumns ( insertedColumns ) ;
58- q . AppendLine ( ")" ) ;
56+ q . Append ( $ " FROM { source } ) { PseudoTableExcluded } ") ;
57+ q . AppendLine ( ) ;
5958
6059 q . Append ( "ON " ) ;
6160 q . AppendJoin ( " AND " , matchColumns , ( b , col ) => b . Append ( $ "{ PseudoTableInserted } .{ col } = { PseudoTableExcluded } .{ col } ") ) ;
@@ -65,7 +64,20 @@ public override string BuildMoveDataSql<T>(
6564 {
6665 var columns = target . GetColumns ( false ) ;
6766
68- q . AppendLine ( "WHEN MATCHED THEN UPDATE SET " ) ;
67+ q . Append ( "WHEN MATCHED" ) ;
68+
69+ if ( onConflictTyped . RawWhere != null || onConflictTyped . Where != null )
70+ {
71+ if ( onConflictTyped is { RawWhere : not null , Where : not null } )
72+ {
73+ throw new ArgumentException ( "Cannot specify both RawWhere and Where in OnConflictOptions." ) ;
74+ }
75+
76+ q . Append ( " AND " ) ;
77+ AppendConflictCondition ( q , target , context , onConflictTyped ) ;
78+ }
79+
80+ q . AppendLine ( " THEN UPDATE SET " ) ;
6981 q . AppendJoin ( ", " , GetUpdates ( context , target , columns , onConflictTyped . Update ) ) ;
7082 q . AppendLine ( ) ;
7183 }
@@ -77,13 +89,6 @@ public override string BuildMoveDataSql<T>(
7789 q . Append ( "VALUES (" ) ;
7890 q . AppendJoin ( ", " , insertedColumns , ( b , col ) => b . Append ( $ "{ PseudoTableExcluded } .{ col . QuotedColumName } ") ) ;
7991 q . AppendLine ( ")" ) ;
80-
81- if ( returnedColumns . Count != 0 )
82- {
83- q . Append ( "OUTPUT " ) ;
84- q . AppendJoin ( ", " , returnedColumns , ( b , col ) => b . Append ( $ "{ PseudoTableInserted } .{ col . QuotedColumName } AS { col . QuotedColumName } ") ) ;
85- q . AppendLine ( ) ;
86- }
8792 }
8893
8994 // No conflict handling
0 commit comments