'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:

DECLARE @properties nvarchar(max) = N'[{"property": "A", "value": "1"},
                                       {"property": "B", "value": "2"},
                                       {"property": "C", "value": "3"}]';
INSERT INTO dbo.property
SELECT *
FROM OPENJSON(@properties)
WITH ( property varchar(50) '$.property',
       value varchar(50) '$.value');

And want to change the statement to something like:

DECLARE @properties nvarchar(max) = N'[{"property": "A", "value": "1"},
                                           {"property": "B", "value": "2"},
                                           {"property": "C", "value": "3"}]';
    INSERT INTO dbo.property (objectId)
    VALUES (IDENT_CURRENT('nodes')) 
    SELECT *
    FROM OPENJSON(@properties)
    WITH ( property varchar(50) '$.property',
           value varchar(50) '$.value');
sql


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source