Skip to content
Advertisement

utilize ident_current in JSON insert

Is it possible to merge / mix / join the SQL command IDENT_CURRENT(‘table_name’) with a OPENJSON insert statement?

For example a table dbo.properties contains:

  • id as int (primary key, auto increment)
  • objectId as int
  • property as varchar(50)
  • value as varchar (50)

I fill the property and value based on a JSON and are in need to use IDENT_CURRENT(‘objects’) to fill the objectId column. Currently my statement looks like:

And want to change the statement to something like:

Advertisement

Answer

Cannot say I’d necessarily recommend your approach. Generally I’d recommend inserting into both tables together and using SCOPE_IDENTITY() instead. With your approach, if another process inserts a row at the same time, it could cause data issues since your script might grab the wrong identity value

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement