sql for xml

Getting error: xml document cannot contain multiple root level elements.  (I know why, just not sure how to fix it).

I'd like to generate this:
<root uid="me" pwd="123">
      <group gid="222">
            <transaction>
                  <trxid>1</trxid>
                  <trxdate>12/25/2010</trxdate>
                  <trxamt>10.00</trxamt>
            </transaction>
      </group>
      <group gid="222">
            <transaction>
                  <trxid>2</trxid>
                  <trxdate>12/31/2010</trxdate>
                  <trxamt>15.00</trxamt>
            </transaction>
      </group>
</root>

But I'm getting this:
<root uid="me" pwd="123">
  <group gid="222">
    <transaction>
      <trxid>1</trxid>
      <trxdate>12/25/2010</trxdate>
      <trxamt>10.00</trxamt>
    </transaction>
  </group>
</root>
<root uid="me" pwd="123">
  <group gid="222">
    <transaction>
      <trxid>2</trxid>
      <trxdate>12/31/2010</trxdate>
      <trxamt>15.00</trxamt>
    </transaction>
  </group>
</root>

How do I fix the query?  (see code for sample table)

SELECT 'me' AS '@uid', '123' AS '@pwd',
ISNULL(CONVERT(VARCHAR(25), transactiongroupid), '') AS 'group/@gid',
CONVERT(VARCHAR(25), transactionid) AS 'group/transaction/trxid',
CONVERT(VARCHAR(10), transactiondate, 101) AS 'group/transaction/trxdate',
ISNULL(CONVERT(VARCHAR(10), transactionamt), '') AS 'group/transaction/trxamt'
FROM tempdb.dbo.mh_group_transactions
WHERE filesentdate IS NULL
FOR XML PATH('root')
IF EXISTS (SELECT * FROM tempdb.dbo.sysobjects WHERE name = 'mh_group_transactions' AND type = 'U' AND UID = 1)
DROP TABLE tempdb.dbo.mh_group_transactions

CREATE TABLE tempdb.dbo.mh_group_transactions (
transactionid int not null,
transactiongroupid int null,
transactiondate smalldatetime null,
transactionamt money null,
filesentdate smalldatetime null)

INSERT INTO tempdb.dbo.mh_group_transactions VALUES (1, 222, '12/25/2010', 10.00, NULL)
INSERT INTO tempdb.dbo.mh_group_transactions VALUES (2, 222, '12/31/2010', 15.00, NULL)

Open in new window

MariaHaltAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kevin CrossChief Technology OfficerCommented:
Hi.

Are you sure there should be two <group gid="222"> elements or should the two transactions being under one?

Here is an example of one way to accomplish what you want. Another would be using FOR XML EXPLICIT where you have to build out the entire XML document using explicit placement and UNION operators.


/* Test data. */
DECLARE @mh_group_transactions TABLE (
   transactionid int not null,
   transactiongroupid int null,
   transactiondate smalldatetime null,
   transactionamt money null,
   filesentdate smalldatetime null
);

INSERT INTO @mh_group_transactions VALUES (1, 222, '12/25/2010', 10.00, NULL);
INSERT INTO @mh_group_transactions VALUES (2, 222, '12/31/2010', 15.00, NULL);

/*
<!-- desired output -->
<root uid="me" pwd="123">
      <group gid="222">
            <transaction>
                  <trxid>1</trxid>
                  <trxdate>12/25/2010</trxdate>
                  <trxamt>10.00</trxamt>
            </transaction>
      </group>
      <group gid="222">
            <transaction>
                  <trxid>2</trxid>
                  <trxdate>12/31/2010</trxdate>
                  <trxamt>15.00</trxamt>
            </transaction>
      </group>
</root>
*/
SELECT 'me' AS '@uid', '123' AS '@pwd'
     , (
       SELECT ISNULL(CONVERT(VARCHAR(25), transactiongroupid), '') AS "@gid"
            , (
              SELECT CONVERT(VARCHAR(25), transactionid) AS trxid
                   , CONVERT(VARCHAR(10), transactiondate, 101) AS trxdate
                   , ISNULL(CONVERT(VARCHAR(10), transactionamt), '') AS trxamt
			  FROM @mh_group_transactions t2
			  WHERE t1.transactiongroupid = t2.transactiongroupid
			  AND t2.filesentdate IS NULL
			  FOR XML PATH('transaction'), TYPE
              )
	   FROM @mh_group_transactions t1
	   WHERE filesentdate IS NULL
	   GROUP BY transactiongroupid
	   FOR XML PATH('group'), TYPE
       )
FOR XML PATH('root')
;

/*
<!-- actual output -->
<root uid="me" pwd="123">
  <group gid="222">
    <transaction>
      <trxid>1</trxid>
      <trxdate>12/25/2010</trxdate>
      <trxamt>10.00</trxamt>
    </transaction>
    <transaction>
      <trxid>2</trxid>
      <trxdate>12/31/2010</trxdate>
      <trxamt>15.00</trxamt>
    </transaction>
  </group>
</root>
*/

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kevin CrossChief Technology OfficerCommented:
If you truly want the desired output above, just make the modification shown below:
/*
<!-- desired output -->
<root uid="me" pwd="123">
      <group gid="222">
            <transaction>
                  <trxid>1</trxid>
                  <trxdate>12/25/2010</trxdate>
                  <trxamt>10.00</trxamt>
            </transaction>
      </group>
      <group gid="222">
            <transaction>
                  <trxid>2</trxid>
                  <trxdate>12/31/2010</trxdate>
                  <trxamt>15.00</trxamt>
            </transaction>
      </group>
</root>
*/
SELECT 'me' AS '@uid', '123' AS '@pwd'
     , (
       SELECT ISNULL(CONVERT(VARCHAR(25), transactiongroupid), '') AS "@gid"
            , (
              SELECT CONVERT(VARCHAR(25), transactionid) AS trxid
                   , CONVERT(VARCHAR(10), transactiondate, 101) AS trxdate
                   , ISNULL(CONVERT(VARCHAR(10), transactionamt), '') AS trxamt
			  FROM @mh_group_transactions t2
			  WHERE /*t1.transactiongroupid = t2.transactiongroupid
			  AND t2.filesentdate IS NULL*/ t1.transactionid = t2.transactionid
			  FOR XML PATH('transaction'), TYPE
              )
	   FROM @mh_group_transactions t1
	   WHERE filesentdate IS NULL
	   -- GROUP BY transactiongroupid
	   FOR XML PATH('group'), TYPE
       )
FOR XML PATH('root')

Open in new window

0
MariaHaltAuthor Commented:
Perfect!  Thanks.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.