简体   繁体   中英

Azure Synapse - retrieve inserted row identity value

We have an ETL job, which would need to insert a row into Table X , and then retrieve the IDENTITY value for the row inserted into Table X for use in later transformations. IDENTITY is used as a surrogate key, eg like here .

In SQL Server it is possible to use SCOPE_IDENTITY but this is not available in Azure Synapse (formerly Azure SQL Data warehouse).

Simple example

╔═══════════════╦═══════╗
║ Id (IDENTITY) ║ Value ║
╠═══════════════╬═══════╣
║             1 ║ abc   ║
║             2 ║ def   ║
╚═══════════════╩═══════╝

The job will insert value "hij".

╔═══════════════╦═══════╗
║ Id (IDENTITY) ║ Value ║
╠═══════════════╬═══════╣
║             1 ║ abc   ║
║             2 ║ def   ║
║             3 ║ hij   ║
╚═══════════════╩═══════╝

The job should know the Id value = 3.

Note: doing MAX(Id) would not work, as the IDENTITY column does not guarantee incremental values, and other inserts could go into the table in the meanwhile.

Does anyone have a suggested solution for this?

I found the same "issue" when joining a project in my current job, we currently use a top 1 query ordered by (desc) an inserted datetime column:

SELECT TOP 1 identity_col
FROM schema.table
ORDER BY datetime_col DESC

I know it is prone to errors, I wanted to use SCOPE_IDENTITY() but this is as good as it gets... maybe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM