如何在 sql 服务器中将数据更新为 json 数组和将 select 数据更新为 json 数组
How to update data as json array and select data as json array in sql server
我是 SQL 服务器 2017 的新人,结果是 JSON。我将 JSON 数组存储在 table 的一列中。我在 table 中保存了 id 的数组,但我想从其他 table 更新它的相关文本,所以请帮助我。
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"]
一种可能的方法是使用 OPENJSON()
和默认模式解析带有 ID 的 JSON 数组,然后生成带有名称的 JSON 数组。具有默认架构 returns 的 OPENJSON()
具有列 key
、value
和 type
的 table 以及 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"]
我是 SQL 服务器 2017 的新人,结果是 JSON。我将 JSON 数组存储在 table 的一列中。我在 table 中保存了 id 的数组,但我想从其他 table 更新它的相关文本,所以请帮助我。
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"]
一种可能的方法是使用 OPENJSON()
和默认模式解析带有 ID 的 JSON 数组,然后生成带有名称的 JSON 数组。具有默认架构 returns 的 OPENJSON()
具有列 key
、value
和 type
的 table 以及 key
列包含索引每个项目。请注意,这里的重要部分是按照 IDs JSON 数组中存在的相同顺序生成名称。您需要使用基于字符串聚合的方法,因为我不认为您可以使用 FOR 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"]