@@ -3817,6 +3817,7 @@ pub enum Statement {
3817
3817
/// ```
3818
3818
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/merge)
3819
3819
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#merge_statement)
3820
+ /// [MSSQL](https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16)
3820
3821
Merge {
3821
3822
/// optional INTO keyword
3822
3823
into : bool ,
@@ -3828,6 +3829,8 @@ pub enum Statement {
3828
3829
on : Box < Expr > ,
3829
3830
/// Specifies the actions to perform when values match or do not match.
3830
3831
clauses : Vec < MergeClause > ,
3832
+ // Specifies the output to save changes in MSSQL
3833
+ output : Option < OutputClause > ,
3831
3834
} ,
3832
3835
/// ```sql
3833
3836
/// CACHE [ FLAG ] TABLE <table_name> [ OPTIONS('K1' = 'V1', 'K2' = V2) ] [ AS ] [ <query> ]
@@ -5407,14 +5410,19 @@ impl fmt::Display for Statement {
5407
5410
source,
5408
5411
on,
5409
5412
clauses,
5413
+ output,
5410
5414
} => {
5411
5415
write ! (
5412
5416
f,
5413
5417
"MERGE{int} {table} USING {source} " ,
5414
5418
int = if * into { " INTO" } else { "" }
5415
5419
) ?;
5416
5420
write ! ( f, "ON {on} " ) ?;
5417
- write ! ( f, "{}" , display_separated( clauses, " " ) )
5421
+ write ! ( f, "{}" , display_separated( clauses, " " ) ) ?;
5422
+ if let Some ( output) = output {
5423
+ write ! ( f, " {output}" ) ?;
5424
+ }
5425
+ Ok ( ( ) )
5418
5426
}
5419
5427
Statement :: Cache {
5420
5428
table_name,
@@ -7945,6 +7953,35 @@ impl Display for MergeClause {
7945
7953
}
7946
7954
}
7947
7955
7956
+ /// A Output Clause in the end of a 'MERGE' Statement
7957
+ ///
7958
+ /// Example:
7959
+ /// OUTPUT $action, deleted.* INTO dbo.temp_products;
7960
+ /// [mssql](https://learn.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql)
7961
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
7962
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
7963
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
7964
+ pub struct OutputClause {
7965
+ pub select_items : Vec < SelectItem > ,
7966
+ pub into_table : SelectInto ,
7967
+ }
7968
+
7969
+ impl fmt:: Display for OutputClause {
7970
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
7971
+ let OutputClause {
7972
+ select_items,
7973
+ into_table,
7974
+ } = self ;
7975
+
7976
+ write ! (
7977
+ f,
7978
+ "OUTPUT {} {}" ,
7979
+ display_comma_separated( select_items) ,
7980
+ into_table
7981
+ )
7982
+ }
7983
+ }
7984
+
7948
7985
#[ derive( Debug , Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
7949
7986
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
7950
7987
#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
0 commit comments