@@ -3835,6 +3835,7 @@ pub enum Statement {
3835
3835
/// ```
3836
3836
/// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/merge)
3837
3837
/// [BigQuery](https://cloud.google.com/bigquery/docs/reference/standard-sql/dml-syntax#merge_statement)
3838
+ /// [MSSQL](https://learn.microsoft.com/en-us/sql/t-sql/statements/merge-transact-sql?view=sql-server-ver16)
3838
3839
Merge {
3839
3840
/// optional INTO keyword
3840
3841
into : bool ,
@@ -3846,6 +3847,8 @@ pub enum Statement {
3846
3847
on : Box < Expr > ,
3847
3848
/// Specifies the actions to perform when values match or do not match.
3848
3849
clauses : Vec < MergeClause > ,
3850
+ // Specifies the output to save changes in MSSQL
3851
+ output : Option < OutputClause > ,
3849
3852
} ,
3850
3853
/// ```sql
3851
3854
/// CACHE [ FLAG ] TABLE <table_name> [ OPTIONS('K1' = 'V1', 'K2' = V2) ] [ AS ] [ <query> ]
@@ -5425,14 +5428,19 @@ impl fmt::Display for Statement {
5425
5428
source,
5426
5429
on,
5427
5430
clauses,
5431
+ output,
5428
5432
} => {
5429
5433
write ! (
5430
5434
f,
5431
5435
"MERGE{int} {table} USING {source} " ,
5432
5436
int = if * into { " INTO" } else { "" }
5433
5437
) ?;
5434
5438
write ! ( f, "ON {on} " ) ?;
5435
- write ! ( f, "{}" , display_separated( clauses, " " ) )
5439
+ write ! ( f, "{}" , display_separated( clauses, " " ) ) ?;
5440
+ if let Some ( output) = output {
5441
+ write ! ( f, " {output}" ) ?;
5442
+ }
5443
+ Ok ( ( ) )
5436
5444
}
5437
5445
Statement :: Cache {
5438
5446
table_name,
@@ -7963,6 +7971,35 @@ impl Display for MergeClause {
7963
7971
}
7964
7972
}
7965
7973
7974
+ /// A Output Clause in the end of a 'MERGE' Statement
7975
+ ///
7976
+ /// Example:
7977
+ /// OUTPUT $action, deleted.* INTO dbo.temp_products;
7978
+ /// [mssql](https://learn.microsoft.com/en-us/sql/t-sql/queries/output-clause-transact-sql)
7979
+ #[ derive( Debug , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
7980
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
7981
+ #[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
7982
+ pub struct OutputClause {
7983
+ pub select_items : Vec < SelectItem > ,
7984
+ pub into_table : SelectInto ,
7985
+ }
7986
+
7987
+ impl fmt:: Display for OutputClause {
7988
+ fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
7989
+ let OutputClause {
7990
+ select_items,
7991
+ into_table,
7992
+ } = self ;
7993
+
7994
+ write ! (
7995
+ f,
7996
+ "OUTPUT {} {}" ,
7997
+ display_comma_separated( select_items) ,
7998
+ into_table
7999
+ )
8000
+ }
8001
+ }
8002
+
7966
8003
#[ derive( Debug , Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
7967
8004
#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
7968
8005
#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
0 commit comments