Je me bats comment formater ma procédure stockée pour créer une chaîne de données JSONprocédure stockée sélectionner des données JSON
J'ai cet exemple de requête où, en i besoin de formater la chaîne JSON comme celui-ci
{"Résumé": "Nom d'utilisateur du changement d'employé", "DateModified": "2017/09/06", "Modifications": [{"Propriété": "test", "ancien": "10", "nouveau": "1"}, {"Property": "test", "old": "10", "new": "2"}, {"Propriété": "test", "old": "10", "new" ":" 3 "}, {" Propriété ":" test "," ancien ":" 10 "," nouveau ":" 4 "}, {" Propriété ":" test "," ancien ":" 10 ", "nouveau": "5"}]}
DECLARE @membersJSON NVARCHAR(MAX) = '[1,2,3,4,5]';
DECLARE @commiteeID INT = 10;
DECLARE @jsonData NVARCHAR(MAX);
DECLARE @jsonTable TABLE(
Property nvarchar(max),
old nvarchar(max),
new nvarchar(max));
INSERT INTO @jsonTable SELECT 'test' as Property,@commiteeID AS old,m.value AS new
FROM OPENJSON(@membersJSON) as m;
SELECT * FROM @jsonTable FOR JSON PATH, ROOT ('CommitteeMembers')
Actuellement ma sortie est toujours comme ce
{ "CommitteeMembers": [{ "Propriété": "test", "vieux": "10", "nouveau": "1"}, {"Propriété": "test", "ancien": "10", "nouveau": "2"}, {"Propriété": "test", "ancien": "10", "nouveau": "3" }, {"Propriété": "test", "ancien": "10", "nouveau": "4"}, {"Propriété": "test", "ancien": "10", "nouveau": " 5" }]}
Heres le dbfiddle link
CURSEUR n'est pas autorisée dans l'instruction FOR JSON - des thats l'erreur –
peut vous montrer votre table JSON defination –
voir le lien dbdiffle ici http://dbfiddle.uk/?rdbms=sqlserver_2016&fiddle=db2669c3863b41db46391a0f55e8bc60 –