jplauri opened a new pull request #19596: URL: https://github.com/apache/airflow/pull/19596
Related: https://github.com/apache/airflow/discussions/19583 For prepared SQL statements, MSSQL uses `?` rather than `%s` for parameter markers. Currently, [MsSqlHook](https://airflow.apache.org/docs/apache-airflow-providers-microsoft-mssql/stable/_api/airflow/providers/microsoft/mssql/hooks/mssql/index.html) does not provide an implementation of `_generate_insert_sql` used by `insert_rows` making it impossible to use for instance [GenericTransfer](https://airflow.apache.org/docs/apache-airflow/stable/_api/airflow/operators/generic_transfer/index.html) with the said hooks. As written, the PR implements `_generate_insert_sql` for MsSqlHook but in a limited way. That is, it will only allow us to do INSERTs (so when `replace=False`) but not UPSERTs (when `replace=True`). I would love to get some feedback and ideas for implementing the UPSERT even though it is not useful for my current use-case. Note that MSSQL does not support anything like "REPLACE INTO" for UPSERT. Instead, there seem to be two alternatives: - Try to do an update first and check `@@ROWCOUNT`: in case nothing was updated, do an insert. This is explained [here by Aaron Bertrand](https://sqlperformance.com/2020/09/locking/upsert-anti-pattern). - Use `MERGE`, many hits on StackOverflow such [this one](https://stackoverflow.com/q/2479488/551375) explaining the syntax. It seems that Aaron B. argues for the pattern in the first link because of several issues with `MERGE` (see [list of issues](https://www.mssqltips.com/sqlservertip/3074/use-caution-with-sql-servers-merge-statement/)). I spent a bit of time trying to implement either approach here, but got stuck after realizing it's not that straightforward to re-use parameter markers `?` from `values` from pyodbc `cursor.execute(sql, values)`. In any case, I was attempting to draw some inspiration from `_generate_insert_sql` of [PostgresHook](https://airflow.apache.org/docs/apache-airflow-providers-postgres/stable/_modules/airflow/providers/postgres/hooks/postgres.html#PostgresHook). Thus, this might need some work beyond just implementing `_generate_insert_sql` unless I'm missing something. <!-- Thank you for contributing! Please make sure that your code changes are covered with tests. And in case of new features or big changes remember to adjust the documentation. Feel free to ping committers for the review! In case of existing issue, reference it using one of the following: closes: #ISSUE related: #ISSUE How to write a good git commit message: http://chris.beams.io/posts/git-commit/ --> --- **^ Add meaningful description above** Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/CONTRIBUTING.rst#pull-request-guidelines)** for more information. In case of fundamental code change, Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvements+Proposals)) is needed. In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x). In case of backwards incompatible changes please leave a note in [UPDATING.md](https://github.com/apache/airflow/blob/main/UPDATING.md). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
