How to generate nested JSON with SQL -


can assist me following query:

i have 2 tables

[sdocumentmanagement].[tdocuments]

[sdocumentmanagement].[tdocumentattachments]

every entry in documents table different document , pk documentid. in table documentattachments can have 1 or more attachments fk - documentid.

what want achieve following.

[   {     documentid: 1,     something: 'blb',     attachments: [       {         attachmentid: 1,         somethingelse: '23232'       }     ]   },   {     documentid: 2,     something: 'blb',     attachments: [       {         attachmentid: 12,         somethingelse: '23232'       },       {         attachmentid: 13,         somethingelse: '23232'       }     ]   } ] 

i want take information both tables.

i tried following queries

select @documentsjson = (         select *         [sdocumentmanagement].[tdocuments]         left join [sdocumentmanagement].[tdocumentattachments] on [tdocuments].documentid = [tdocumentattachments].documentid     json path     )        select @documentsjson = (     select     (         select *         [sdocumentmanagement].[tdocumentattachments]         [tdocumentattachments].documentid = [tdocuments].documentid         json path     ) attachments     , *     [sdocumentmanagement].[tdocuments]     json path     ) 

ok, found solution:

select @documentsjson = (        select *,        (        select * [sdocumentmanagement].[tdocumentattachments] attachments        attachments.documentid = docs.documentid        json auto        ) documentattachments        [sdocumentmanagement].[tdocuments] docs        docs.organizationid = @organizationid        json path, root('documents') ) 

but there problem.

no matter declare @documentsjson nvarchar(max) output, sql returning 4500characters documentsjson ? idea problem ?


Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -