Tuesday, May 7, 2024

How to join two tables without join and output as XML - SQL Server

 SELECT

    (SELECT d.DtFinalJudOrder FROM CourtApplicationDisposals d FOR XML PATH('CourtApplicationDisposalsList'), TYPE) AS CourtApplicationDisposals,

    (SELECT s.DtOfStay FROM CourtApplicationStayDetails s FOR XML PATH('CourtApplicationStayDetailsList'), TYPE) AS CourtApplicationStayDetails

FOR XML PATH(''), ROOT('root')



For single table with root

===========

SELECT  Doc.DocumentName, Doc.Subject, Doc.DocumentFilePath

FROM Document Doc

where Doc.ObjectType = 337 AND Doc.IsActive = 1

FOR XML RAW('DocumentList'),ELEMENTS,ROOT('Document')


For single table without root

SELECT  Doc.DocumentName, Doc.Subject, Doc.DocumentFilePath

FROM Document Doc

where Doc.ObjectType = 337 AND Doc.IsActive = 1

FOR XML RAW('DocumentList'),ELEMENTS


For single table with cast and root

select cast ((SELECT  Doc.DocumentName, Doc.Subject, Doc.DocumentFilePath

FROM Document Doc

where Doc.ObjectType = 337 AND Doc.IsActive = 1

FOR XML RAW('DocumentList'),ELEMENTS,ROOT('Document')) as xml)


For more than two tables :-

DECLARE @ApplStayDetails xml

DECLARE @ApplDisposals xml


SET @ApplStayDetails = (SELECT  S.DtOfStay, S.BranchEfforts, S.DtStayVacation, (Select Name from Court Where CourtId = S.CourtId) AS CourtName

FROM CourtApplicationStayDetails S

where S.ApplicationId = @ApplicationId AND S.IsActive = 1

FOR XML RAW('CourtApplicationStayDetailsList'),ELEMENTS,ROOT('CourtApplicationStayDetails'))


--Court Application Disposals

SET @ApplDisposals = (SELECT  D.DtFinalJudOrder, (Select Name from Court Where CourtId = D.CourtId) AS CourtName, dbo.fn_LookupValueById(D.CourtResult) AS CourtResultName

FROM CourtApplicationDisposals D

where D.ApplicationId = @ApplicationId

FOR XML RAW('CourtApplicationDisposals'),ELEMENTS)


select 


@ApplStayDetails,@ApplDisposals FOR XML RAW('CourtApplicationProfile'),ELEMENTS


No comments:

Post a Comment