@@ -3835,6 +3835,7 @@ pub enum Statement {
38353835 /// ```
38363836 /// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/merge)
38373837 /// [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)
38383839 Merge {
38393840 /// optional INTO keyword
38403841 into : bool ,
@@ -3846,6 +3847,8 @@ pub enum Statement {
38463847 on : Box < Expr > ,
38473848 /// Specifies the actions to perform when values match or do not match.
38483849 clauses : Vec < MergeClause > ,
3850+ // Specifies the output to save changes in MSSQL
3851+ output : Option < OutputClause > ,
38493852 } ,
38503853 /// ```sql
38513854 /// CACHE [ FLAG ] TABLE <table_name> [ OPTIONS('K1' = 'V1', 'K2' = V2) ] [ AS ] [ <query> ]
@@ -5425,14 +5428,19 @@ impl fmt::Display for Statement {
54255428 source,
54265429 on,
54275430 clauses,
5431+ output,
54285432 } => {
54295433 write ! (
54305434 f,
54315435 "MERGE{int} {table} USING {source} " ,
54325436 int = if * into { " INTO" } else { "" }
54335437 ) ?;
54345438 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 ( ( ) )
54365444 }
54375445 Statement :: Cache {
54385446 table_name,
@@ -7963,6 +7971,35 @@ impl Display for MergeClause {
79637971 }
79647972}
79657973
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+
79668003#[ derive( Debug , Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
79678004#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
79688005#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
0 commit comments