MSSQL JSON 数组中的数据

MSSQL JSON Data in Array

在 Microsoft SQL 服务器中,我有一行使用 JSON。像这样

[
{"id":"_Diagnose","value":
 {"$type":"System.Collections.Generic.List`1[[x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity]], mscorlib","$values":
  [{"$type":"x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity","Icd":"G12.8","IcdBeschreibung":"Sonstige spinale Muskelatrophien und verwandte Syndrome","IcdNotationskennzeichen":null,"Erlaeuterung":null,"Ausnahmetatbestand":null,"Sicherheit":"G","Seitenlokalisation":null,"AbgesetztAm":null,"Warnings":[],"IdKategorieBevorAbgesetzt":null,"TnmStatus":null},
  {"$type":"x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity","Icd":"B15.9","IcdBeschreibung":"Virushepatitis A ohne Coma hepaticum","IcdNotationskennzeichen":null,"Erlaeuterung":null,"Ausnahmetatbestand":null,"Sicherheit":"G","Seitenlokalisation":"","AbgesetztAm":null,"Warnings":[],"IdKategorieBevorAbgesetzt":null,"TnmStatus":null}]}
}
]

我想要ICD的两个值。

我有一个像这样的 SQL 语句,它工作正常:

SELECT
DetailXml.value('shortinfo[1]', 'nvarchar(100)') as Text,
(select test_value from openjson(formularVariablen) 
with (test_id nvarchar(MAX) '$.id', test_value nvarchar(MAX) '$.value."$values"[0].Icd') where test_id = '_Diagnose') as ICD1,
(select test_value from openjson(formularVariablen) 
with (test_id nvarchar(MAX) '$.id', test_value nvarchar(MAX) '$.value."$values"[1].Icd') where test_id = '_Diagnose') as ICD2
from MedDok CROSS APPLY Detail.nodes('meddokformular') as SqlXml(DetailXml)
LEFT JOIN MedDokFormularVariable ON MedDok.Id=MedDokFormularVariable.Id_MedDok
where exists
(select * from openjson((select formularVariablen from MedDokFormularVariable where id_meddok = MedDok.ID)) 
with (test_id varchar(100) '$.id', test_value varchar(100) '$.value'))

但问题是可能有3个或更多的ICD键值对。我想要他们全部。试了很多方法都没用。

没有 sql 2016。但是使代码可以在 XML 上运行。

DECLARE @myDoc XML;
SET @myDoc = '<id>_Diagnose</id>        
            <value>
            <type>System.Collections.Generic.List`1[[x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity]], mscorlib</type>
            <values>
                <type>x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity</type>
                <Icd>G12.8</Icd>
                <IcdBeschreibung>Sonstige spinale Muskelatrophien und verwandte Syndrome</IcdBeschreibung>
                <IcdNotationskennzeichen />
                <Erlaeuterung />
                <Ausnahmetatbestand />
                <Sicherheit>G</Sicherheit>
                <Seitenlokalisation />
                <AbgesetztAm />
                <IdKategorieBevorAbgesetzt />
                <TnmStatus />
            </values>
            <values>
                <type>x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity</type>
                <Icd>B15.9</Icd>
                <IcdBeschreibung>Virushepatitis A ohne Coma hepaticum</IcdBeschreibung>
                <IcdNotationskennzeichen />
                <Erlaeuterung />
                <Ausnahmetatbestand />
                <Sicherheit>G</Sicherheit>
                <Seitenlokalisation></Seitenlokalisation>
                <AbgesetztAm />
                <IdKategorieBevorAbgesetzt />
                <TnmStatus />
            </values>
        </value>
    ';
SELECT @myDoc; 

-- BUILD XML
DECLARE @x XML;
SELECT @x =
(
    SELECT @myDoc.value('(/id)[1]', 'varchar(50)') AS ID,
           @myDoc.query('
            for $a in //values
            return <address 
                Icd="{$a/Icd}" 
                IcdBeschreibung="{$a/IcdBeschreibung}" 

            />
        ') FOR XML RAW
);
SELECT [Name] = T.Item.value('../@ID', 'varchar(20)'),
       street = T.Item.value('@Icd', 'varchar(20)'),
       city = T.Item.value('@IcdBeschreibung', 'varchar(20)')
FROM @x.nodes('//row/address') AS T(Item);    

对值列表使用交叉应用。像这样:

select
    *
from OPENJSON(
'
[
{"id":"_Diagnose","value":
    {"$type":"System.Collections.Generic.List`1[[x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity]], mscorlib","$values":
        [{"$type":"x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity","Icd":"G12.8","IcdBeschreibung":"Sonstige spinale Muskelatrophien und verwandte Syndrome","IcdNotationskennzeichen":null,"Erlaeuterung":null,"Ausnahmetatbestand":null,"Sicherheit":"G","Seitenlokalisation":null,"AbgesetztAm":null,"Warnings":[],"IdKategorieBevorAbgesetzt":null,"TnmStatus":null},
        {"$type":"x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity","Icd":"B15.9","IcdBeschreibung":"Virushepatitis A ohne Coma hepaticum","IcdNotationskennzeichen":null,"Erlaeuterung":null,"Ausnahmetatbestand":null,"Sicherheit":"G","Seitenlokalisation":"","AbgesetztAm":null,"Warnings":[],"IdKategorieBevorAbgesetzt":null,"TnmStatus":null}]}
}
]
')
with
(
    [id] nvarchar(max), 
    [values] nvarchar(max) '$.value."$values"' AS JSON
) as a
CROSS APPLY OPENJSON([values])
WITH(
    [Icd] nvarchar(max), 
    [IcdBeschreibung] nvarchar(max)
    /* etc... */
) as b