@@ -3817,6 +3817,7 @@ pub enum Statement {
38173817 /// ```
38183818 /// [Snowflake](https://docs.snowflake.com/en/sql-reference/sql/merge)
38193819 /// [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)
38203821 Merge {
38213822 /// optional INTO keyword
38223823 into : bool ,
@@ -3828,6 +3829,8 @@ pub enum Statement {
38283829 on : Box < Expr > ,
38293830 /// Specifies the actions to perform when values match or do not match.
38303831 clauses : Vec < MergeClause > ,
3832+ // Specifies the output to save changes in MSSQL
3833+ output : Option < OutputClause > ,
38313834 } ,
38323835 /// ```sql
38333836 /// CACHE [ FLAG ] TABLE <table_name> [ OPTIONS('K1' = 'V1', 'K2' = V2) ] [ AS ] [ <query> ]
@@ -5407,14 +5410,19 @@ impl fmt::Display for Statement {
54075410 source,
54085411 on,
54095412 clauses,
5413+ output,
54105414 } => {
54115415 write ! (
54125416 f,
54135417 "MERGE{int} {table} USING {source} " ,
54145418 int = if * into { " INTO" } else { "" }
54155419 ) ?;
54165420 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 ( ( ) )
54185426 }
54195427 Statement :: Cache {
54205428 table_name,
@@ -7945,6 +7953,35 @@ impl Display for MergeClause {
79457953 }
79467954}
79477955
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+
79487985#[ derive( Debug , Copy , Clone , PartialEq , PartialOrd , Eq , Ord , Hash ) ]
79497986#[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
79507987#[ cfg_attr( feature = "visitor" , derive( Visit , VisitMut ) ) ]
0 commit comments