Build json with variable key / json_modify variable 2nd parameter

时间:2016-04-21 22:47:58

标签: sql-server sql-server-2016

suppose you have

create schema tmp
go

create table tmp.Properties
(
  ParentId uniqueidentifier not null,
  PropertyName nvarchar(20) not null,
  PropertyValue nvarchar(100) null,

  primary key (ParentId,PropertyName)
)
go

create table tmp.FullData
(
  ParentId uniqueidentifier not null,
  Properties nvarchar(max) null,

  primary key (ParentId)
)
go

declare @id1 uniqueidentifier = 'F1935D6A-D5A6-4FA1-ACF4-BA3858804CEC',
        @id2 uniqueidentifier = 'F1935D6B-D5A6-4FA1-ACF4-BA3858804CEC'

insert into tmp.Properties
values
(@id1, 'FirstName', 'Luke'),
(@id1, 'LastName', 'Skywalker'),
(@id2, 'FirstName', 'Han'),
(@id2, 'LastName', 'Solo')

please consider that:

  • properties are dynamically created and I cannot know in advance PropertyNames
  • at the moment parents table contains 1M and properties table contains 23M records

how can I fill tmp.FullData with:

ParentId                             Properties
------------------------------------ ------------------------------------------------
F1935D6A-D5A6-4FA1-ACF4-BA3858804CEC { "FirstName": "Luke", "LastName": "Skywalker" }
F1935D6B-D5A6-4FA1-ACF4-BA3858804CEC { "FirstName": "Han", "Test1": "Solo" }

I tried

insert into tmp.FullData (ParentId, Properties)
select distinct ParentId, '{}' from tmp.Properties

update f
set Properties = json_modify(Properties, 'append $.' + p.PropertyName, p.PropertyValue)
from tmp.FullData f
cross join tmp.Properties p

but as you know/imagine

Msg 13610, Level 16, State 2, Line 39
The argument 2 of the "JSON_MODIFY" must be a string literal.

any other option? thanks in advance

1 个答案:

答案 0 :(得分:0)

略过更新并直接插入tmp.FullData:

INSERT INTO tmp.FullData (ParentId, Properties)
SELECT
    ParentId
    ,'{'
     + STUFF((
                 SELECT ',' + '"' + PropertyName + '":"' + PropertyValue + '"'
                 FROM tmp.Properties a
                 WHERE a.ParentId = p.ParentId
                 FOR XML PATH(''), TYPE
             ).value('.', 'VARCHAR(MAX)'), 1, 1, ''
            ) + '}' AS Properties
FROM tmp.Properties p
GROUP BY ParentId;