I have a similar requirement for a 3rd party app I need to interface with. The most effective way I know of to do this is to append the XML components to each other in a final output. Here's an example of one of my procs:
CREATE PROCEDURE <procname>
WITH EXECUTE AS CALLER
AS
SET NOCOUNT ON;
SET FMTONLY OFF;
WHILE 1=0
BEGIN
SELECT CONVERT( VARCHAR(MAX), NULL) AS Result
END
DECLARE @ShellAccounts_Standard_i XML
DECLARE @ShellAccounts_WTFSC_i XML
DECLARE @ShellAccounts_Standard_u XML
DECLARE @ShellAccounts_WTFSC_u XML
-- We will build out the Portfolio Shell Accounts for
-- new client entries here.
SELECT @ShellAccounts_standard_i =
(
--Query1
FOR XML PATH ('Account')
)
SELECT @ShellAccounts_WTFSC_i =
(
--query2
FOR XML PATH ('Account')
)
SELECT @ShellAccounts_standard_u =
(
--query3
FOR XML PATH ('Account')
)
SELECT @ShellAccounts_WTFSC_u =
(
--query4
FOR XML PATH ('Account')
)
SELECT
--Header
'<?xml version="1.0" encoding="Windows-1252" ?>'
+ '<ROOT>'
+ '<Header FileDate="' + CONVERT( VARCHAR(20), GETDATE(), 101) + '" />'
+ '<Body DataType="Account">'
-- Body
+ CONVERT( VARCHAR(MAX), ISNULL( @ShellAccounts_Standard_i, ''))
+ CONVERT( VARCHAR(MAX), ISNULL( @ShellAccounts_WTFSC_i, ''))
+ CONVERT( VARCHAR(MAX), ISNULL( @ShellAccounts_Standard_u, ''))
+ CONVERT( VARCHAR(MAX), ISNULL( @ShellAccounts_WTFSC_u, ''))
-- Footer
+ '</Body>'
+ CONVERT( VARCHAR(MAX) ,
(SELECT 'Account' AS [@DataType], SUM(cnt) AS [@RecordCount]
FROM
(SELECT COUNT(*) AS cnt
FROM@ShellAccounts_Standard_i.nodes('Account') cnt(nd)
UNION ALL
SELECT COUNT(*) AS cnt
FROM@ShellAccounts_WTFSC_i.nodes('Account') cnt(nd)
UNION ALL
SELECT COUNT(*) AS cnt
FROM@ShellAccounts_Standard_u.nodes('Account') cnt(nd)
UNION ALL
SELECT COUNT(*) AS cnt
FROM@ShellAccounts_WTFSC_u.nodes('Account') cnt(nd)
) AS drv
FOR XML PATH ( 'Trailer')
)
)
+ '</ROOT>'
AS Result
SET NOCOUNT OFF;
GO
Obviously some of this is obfuscated, but if you change your PATH('') between queries and then append it like I have (instead of everything being 'Account'), you should get to your results.
This is the simplest method I've found to deal with XML creation when you need footers and the like, or need to change node structure between different components.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA