How to update data as json array and select data as json array in sql server

心不动则不痛 提交于 2020-03-22 09:05:20

问题


I am new in SQL server 2017 for JSON result. I am storing JSON array in one column in my table. I am saving id's array in that table, but i want to update its relative text from other table, so please help me in this.

create table #subjectList(subjectID int identity(1,1),subjectName varchar(50))
insert into #subjectList(subjectName)
select 'Math' union all
select 'English' union all
select 'Hindi' union all
select 'PC' union all
select 'Physics'

select * from #subjectList

Create table #studentList(studentID int identity(1,1), subjectName varchar(50), choseSubjectList varchar(max))
insert into #studentList(subjectName, choseSubjectList)
Select 'A','["1","2"]'

select * from #studentList

create table #studentWithSubject(studentID int,subjectName varchar(50),choseSubjectIDList varchar(max),choseSubjectNameList varchar(max))

insert into #studentWithSubject(studentID,subjectName,choseSubjectIDList)
Select a.studentID,a.studentID,a.choseSubjectList
from #studentList a

Update #studentWithSubject set choseSubjectNameList=''

select * from #studentWithSubject

Here is #studentWithSubject output

studentID   subjectName choseSubjectIDList  choseSubjectNameList
1              1         ["1","2"]          ''

Now I want to update subjectname from #subjectList and output should be like this:

studentID   subjectName choseSubjectIDList  choseSubjectNameList
1              1         ["1","2"]          ["Math","English"]

回答1:


One possible approach is to parse the JSON array with IDs using OPENJSON() and default schema and then generate the JSON array with names. The OPENJSON() with default schema returns a table with columns key, value and type and the key columns holds the index of each item. Note, that the important part here is to generate the names in the same order as they exists in the IDs JSON array. You need to use an apprach based on string aggregation, because I don't think, that you can generate a JSON array with scalar values using FOR JSON.

Tables:

create table #subjectList(subjectID int identity(1,1),subjectName varchar(50))
insert into #subjectList(subjectName)
select 'Math' union all
select 'English' union all
select 'Hindi' union all
select 'PC' union all
select 'Physics'

Create table #studentList(studentID int identity(1,1), subjectName varchar(50), choseSubjectList varchar(max))
insert into #studentList(subjectName, choseSubjectList)
Select 'A','["1","2"]' union all
Select 'B','["3","2","5"]' union all
Select 'C','["6","2"]'

create table #studentWithSubject(studentID int,subjectName varchar(50),choseSubjectIDList varchar(max),choseSubjectNameList varchar(max))
insert into #studentWithSubject(studentID,subjectName,choseSubjectIDList)
Select a.studentID,a.studentID,a.choseSubjectList
from #studentList a

Statement:

UPDATE #studentWithSubject
SET choseSubjectNameList = (
    CONCAT(
       '["',
       STUFF(
          (SELECT CONCAT('","', COALESCE(s.subjectName, ''))
          FROM OPENJSON(#studentWithSubject.choseSubjectIDList) j
          LEFT JOIN #subjectList s ON j.[value] = s.subjectID
          ORDER BY CONVERT(int, j.[key])
          FOR XML PATH('')), 1, 3, ''
       ),
       '"]'
    )   
)

Result:

studentID   subjectName choseSubjectIDList  choseSubjectNameList
1           1           ["1","2"]           ["Math","English"]
2           2           ["3","2","5"]       ["Hindi","English","Physics"]
3           3           ["6","2"]           ["","English"]


来源:https://stackoverflow.com/questions/60376495/how-to-update-data-as-json-array-and-select-data-as-json-array-in-sql-server

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!