Solved

SQL for XML format header and groups

Posted on 2014-12-05
9
192 Views
Last Modified: 2014-12-17
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
0
Comment
Question by:COHFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40482927
Firstly, you can't. What you have given is not correct XML code.

For instance:

    <MTU>123</MTU>
    	<Port>1</Port>
    	<WaterConsumption4>30.00</WaterConsumption4>
    	<RunDate>2014-11-11T00:00:00</RunDate>
    </MTU>

Open in new window


has got two </MTU> but only one </MTU>

Guessing what you actually mean, I would suggest:

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

Open in new window

0
 

Author Comment

by:COHFL
ID: 40482947
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
0
 

Author Comment

by:COHFL
ID: 40482959
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 =(
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482966
You are missing a comma at the end of line 3.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482979
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?
0
 

Author Comment

by:COHFL
ID: 40482983
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 =(
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40482992
Have you tried my version? How close is that to what you want?
0
 

Author Comment

by:COHFL
ID: 40483001
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
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40483007
I suggest posting the structure of the table and some sample data. Any other experts want to help?
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question