You are on page 1of 3

JSON1:

Dado el siguiente JSON:

{
"Id": 135,
"IdQuestion": 173,
"Time": "2015-12-21T09:13:49.463",
"Author": 337,
"IdWork": 367,
"ComplexAnswerInfo": [
{
"Id": 279,
"IdAnswer": 135,
"Name": "Quidem fugiat magnam ut sit veniam debitis sunt expedita.",
"DocumentNumber": "1635793725",
"Company": "Hintz, Hintz and Runte",
"JobTitle": "warren",
"Signature": null,
"AllowedDatetimeStart": "2015-12-21T09:13:49.463"
},
{
"Id": 280,
"IdAnswer": 135,
"Name": "Earum vel nihil nam doloribus voluptatem quia.",
"DocumentNumber": "1283620302",
"Company": "Wiegand, Spinka and Hyatt",
"JobTitle": "corbin.morissette",
"Signature": null,
"AllowedDatetimeStart": "2015-12-21T09:13:49.463"
},
{
"Id": 281,
"IdAnswer": 135,
"Name": "Est cum doloremque quo quasi quis.",
"DocumentNumber": "1447220193",
"Company": "Lemke Group",
"JobTitle": "maudie_daugherty",
"Signature": null,
"AllowedDatetimeStart": "2015-12-21T09:13:49.463"
}
],
"EasyAnswerInfo": [
{
"Id": 135,
"IdAnswer": 135,
"InputTime": "2015-12-21T09:13:49.463",
"Signature": "Wilbert Zieme",
"Text": "Minima est nihil sint."
}
]
}

Tabla Working$Answer, en campo Value, con campo Key = 135

Para recuperar el array "ComplexAnswerInfo", se lanzar�a la siguiente consulta:

"SELECT json_extract(value, '$.ComplexAnswerInfo') FROM Working$Answer WHERE Key


= 135;"
/*Para la respuesta con Key = 135 seleccionar dentro del array
"ComplexAnswerInfo" el objeto con Id = 280 */

SELECT json_each.value FROM working$answer a, json_each(a.value,


'$.ComplexAnswerInfo') where a.Key = 135 AND json_extract(json_each.value, '$.Id')
= 280

/* Para actualizar el campo Name del objeto recuperado anteriormente: */

//1. Recuperamos su �ndice en el Array (ser� el campo Id de la tabla devuelta


por la funci�n json_each)
select json_each.key from working$answer a, json_each(a.Value,
'$.ComplexAnswerInfo') where a.Key = 135 AND json_extract(json_each.value, '$.Id')
= 280;
(devuelve un 1)
//2. Actualizamos el elemento correspondiente
UPDATE working$answer SET Value = (SELECT json_set(json(Value),
'$.ComplexAnswerInfo[1].Name', 'Pepito P�rez') FROM working$answer WHERE Key = 135)

WHERE Key = 135;

//Actualizamos IdQuestion = 666 en los Values con IdQuestion = 39


SELECT Key FROM Working$Answer WHERE json_extract(Working$Answer.Value,
'$.IdQuestion') = 39;

UPDATE Working$Answer SET Value = (SELECT json_set(json(Working$Answer.Value),


'$.IdQuestion', 666) FROM Working$Answer ) WHERE json_extract(Working$Answer.Value,
'$.IdQuestion') = 39;

//O en la nueva notaci�n para AnswerProcessing


SELECT Key FROM WK_AnswerProcessing WHERE
json_extract(WK_AnswerProcessing.Value, '$.IdQuestion') = -39;

UPDATE WK_AnswerProcessing SET Value = (SELECT


json_replace(json(WK_AnswerProcessing.Value), '$.IdAnswer', 525) FROM
WK_AnswerProcessing) WHERE Key = 1;

//O en 1 sola (no funciona)

UPDATE WK_AnswerProcessing SET Value = (SELECT


json_set(json(WK_AnswerProcessing.Value), '$.IdAnswer', 525) FROM
WK_AnswerProcessing)
WHERE json_extract(WK_AnswerProcessing.Value, '$.IdAnswer') = -39;

//MULTIPLE UPDATE

WITH Tmp(Key, Value, State) AS (VALUES(5,'kk1', 2) UNION ALL VALUES(6,'kk2',


2))
UPDATE Working$Answer SET
Value = (SELECT Value FROM Tmp WHERE Working$Answer.Key = Tmp.Key),
State = (SELECT State FROM tmp WHERE Working$Answer.Key = Tmp.Key)
WHERE Key IN (SELECT Key FROM Tmp);