Avatar of COHFL
COHFL
Flag for United States of America asked on

SQL for XML format header and groups

Hi there,
I'm creating a store procedure that returns simple data pertaining accounts.
What I need to is to format the xml output the following way:

<AccountNum>
    <AccountNum>000117622</AccountNum>
    <Name>                        </Name>
    <Address1>123 Man street      </Address1>
    <MTU>123</MTU>
    	<Port>1</Port>
    	<WaterConsumption4>30.00</WaterConsumption4>
    	<RunDate>2014-11-11T00:00:00</RunDate>
    </MTU>
    <MTU>345</MTU>
    	<Port>2</Port>
    	<WaterConsumption4>30.00</WaterConsumption4>
     	<RunDate>2014-11-11T00:00:00</RunDate>
    </MTU>
  </AccountNum>

Open in new window


here is the store procedure:
alter procedure usp_Summation360Consumption
as 
begin 
SELECT top 100 [AccountNum]
      ,[MTU]
      ,[Port]
      ,[WaterConsumption4]
      ,[Name]
      ,[Address1]
      ,[CutOffdt]
      ,[RunDate]
  FROM Consumption360
--  where AccountNum = '000112182'
for xml path ('AccountNum'), root('Account'), type
  end

Open in new window


How can I modify the procedure to get the output I need?
Thanks,
COHFL
Microsoft SQL ServerMicrosoft SQL Server 2008SQL

Avatar of undefined
Last Comment
Phillip Burton

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Phillip Burton

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
COHFL

ASKER
not quite there yet!

maybe syntax wise was not accurate but here is what im trying:
SELECT [AccountNum]
      ,[Name]
      ,[Address1]
	  (SELECT [MTU]
			  ,[Port]
			  ,[WaterConsumption4]
			  ,[RunDate]
		  FROM Consumption360
		  where AccountNum = '000100226'
		  order by [MTU], [PORT]
		for xml path ('MTU'), root('METER'), type
		)
  FROM Consumption360
  where AccountNum = '000100226'
  for xml path ('AccountNum'), root('ACCOUNT'), type

Open in new window


the idea is that the account info is wrap around on the top and each MTU after but under the same Account
COHFL

ASKER
but yet im getting and error that reads

Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'order'.

Just comment that out to see if it work then complaints on the next line =(
Phillip Burton

You are missing a comma at the end of line 3.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Phillip Burton

If you can't get it quite right, I suggest posting the structure of the table and some sample data. But how close are you now?
COHFL

ASKER
yeah I just notice that =( thanks to point it out.
After I fix that little error on my part I get another error:
Unable to show XML. The following error happened:
Unexpected end of file has occurred. The following elements are not closed: RunDate, MTU, METER, AccountNum, ACCOUNT. Line 1, position 2097154

One solution is to increase the number of characters retrieved from the server for XML data. To change this setting, on the Tools menu, click Options.

I did that and pick the unlimited characters for XML and still does not work =(
Phillip Burton

Have you tried my version? How close is that to what you want?
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
COHFL

ASKER
Phil, I have try your version but it does not break on every MTU and repeats the account number on each entry. Im trying to format the output to break on each account but at the same time break on each MTU
Phillip Burton

I suggest posting the structure of the table and some sample data. Any other experts want to help?