【问题标题】:How to update data as json array and select data as json array in sql server如何在sql server中将数据更新为json数组并选择数据作为json数组
【发布时间】:2020-02-24 13:01:41
【问题描述】:

我是 SQL Server 2017 中 JSON 结果的新手。我将 JSON 数组存储在表的一列中。我将 id 的数组保存在该表中,但我想从其他表中更新它的相关文本,所以请帮助我。

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

这里是#studentWithSubject 输出

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

现在我想从#subjectList 更新主题名,输出应该是这样的:

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

【问题讨论】:

    标签: sql json tsql sql-server-2017


    【解决方案1】:

    一种可能的方法是使用 OPENJSON() 和默认架构解析带有 ID 的 JSON 数组,然后生成带有名称的 JSON 数组。带有默认架构的OPENJSON() 返回一个包含keyvaluetype 列的表,key 列保存每个项目的索引。请注意,这里的重要部分是以与 IDs JSON 数组中存在的顺序相同的顺序生成名称。您需要使用基于字符串聚合的方法,因为我不认为您可以使用FOR JSON 生成具有标量值的 JSON 数组。

    表格:

    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
    

    声明:

    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, ''
           ),
           '"]'
        )   
    )
    

    结果:

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

    【讨论】:

    • 谢谢@Zhorov,我认为有任何简单的方法可以做到这一点,我使用它也有点困难顺便说一句,非常感谢您的快速帮助先生。
    猜你喜欢
    • 2022-01-23
    • 2023-02-08
    • 1970-01-01
    • 2020-01-15
    • 2020-04-14
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    相关资源
    最近更新 更多