So I have a database with 5 tables all with various data. Table 1 is called dbo.Business, table 2 is called dbo.Constraints, table 3 is called dbo.Features, table 4 is called dbo.Interfaces, and table 5 is called dbo.Modules. Using the data from these tables, I'm trying to create an XML doc with the format
<Lin key="####"> <----- comes from table 1
<G ID="####"> <----- comes from table 1
<Constraints>
<Security>2</Security> <----- more data like this, all comes from table 2
</Constraints>
<Modules>
<Module id="1" name="DV" registered="false"/> <----- more data like this, all comes from table 5
</Modules>
<Features>
<Feature id="0" name="DEC" registered="true" expiration=""/> <----- more data like this, all comes from table 3
</Features>
<Interfaces>
<Interface id="1" name="SI" registered="false" expiration="" rulename="HIS"/> <----- more data like this, all comes from table 4
</Interfaces>
</G>
</Lin>
I can't for the life of me figure out how to get an XML doc to come out like this (I just started learning SQL last week). Any and all help would be appreciated. Thank you for your time!
--------------- EDIT:
So with the help of you both I have successfully formatted MOST of my xml to look like what I want, but not quite. My current query is this:
SELECT OrgID as SomeID, Licensingkey as SomeKey INTO Business FROM TestDB.dbo.Business
SELECT ProductName as SecurityConstraint, Amount as Number INTO Constraints FROM TestDB.dbo.Constraints
SELECT FeatureID as id, FeatureName as name, Included as registered, Expiration as expiration INTO Features FROM TestDB.dbo.Features
SELECT ModuleID as id, ProductName as name, Included as registered INTO Modules FROM TestDB.dbo.Modules
SELECT InterfaceID as id, InterfaceName as name, Included as registered, Expiration as expiration, RuleName as rules INTO Interfaces FROM TestDB.dbo.Interfaces
SELECT SomeKey AS [@key]
,SomeID AS [G/@OrgID]
,(SELECT Constraints.Number
FROM Constraints
FOR XML PATH(''),TYPE
) AS [G/Constraints]
,(SELECT Modules.registered AS [@registered]
,Modules.name AS [@name]
,Modules.id AS [@id]
FROM Modules
FOR XML PATH('Module'),TYPE
) AS [G/Modules]
,(SELECT Features.registered AS [@registered]
,Features.name AS [@name]
,Features.id AS [@id]
,IsNull(cast(Features.expiration as varchar(10)), '' ) AS [@expiration]
FROM Features
FOR XML PATH('Feature'),TYPE
) AS [G/Features]
,(SELECT Interfaces.registered AS [@registered]
,Interfaces.name AS [@name]
,Interfaces.id AS [@id]
,IsNull(cast(Interfaces.expiration as varchar(10)), '' ) AS [@expiration]
,Interfaces.rules as [@rulename]
FROM Interfaces
FOR XML PATH('Interface'),TYPE
) AS [G/Interfaces]
FROM Business
FOR XML PATH('Lin')
DROP TABLE master.dbo.Business, master.dbo.Constraints, master.dbo.Features, master.dbo.Modules, master.dbo.Interfaces
GO
and the only problem that I have now is that my Constraints section currently looks like this
<Constraints>
<Number>4</Number>
<SecurityConstraint>Security</SecurityConstraint>
<Number>400</Number>
<SecurityConstraint>InSecurity</SecurityConstraint>
<Number>400</Number>
<SecurityConstraint>LoggedUsers</SecurityConstraint>
<Number>2</Number>
<SecurityConstraint>Lend</SecurityConstraint>
</Constraints>
When I need it to look like
<Constraints>
<Security>4</Security>
<InSecurity>400</InSecurity>
<LoggedUsers>400</LoggedUsers>
<Lend>2</Lend>
</Constraints>
How would I go about tackling this final problem? Thank you so much!