2017-09-18 3 views
0

Dans Microsoft SQL Server, j'ai une ligne qui utilise JSON. Comme cetteDonnées JSS MSSQL dans le tableau

[ 
 
{"id":"_Diagnose","value": 
 
\t {"$type":"System.Collections.Generic.List`1[[x2.Data.DataAccess.Entity.Helper.XmlHelper.MedDokDiagnose, Data.DataAccess.Entity]], mscorlib","$values": 
 
\t \t [{"$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}, 
 
\t \t {"$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}]} 
 
} 
 
]

Je veux que les deux valeurs de la CIM.

J'ai une instruction SQL comme ce qui fonctionne très bien:

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')) 

Mais le problème est que son possible qu'il puisse y avoir 3 ou plus CIM-clé à valeur paires. Et je les veux tous. J'ai essayé tant de façons mais rien n'a fonctionné.

Répondre

0

ne pas avoir un sql2016. Mais fait le code pour travailler sur 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);