Solved

Converting to XML-like format

Posted on 2006-07-05
5
206 Views
Last Modified: 2006-11-18
I have three tables arranged in standard SQL relationships:  Policy_Record (ipr), Insured_Record (iir) and Vehicle_Record (ivr).  The relationship is that each ipr can have one and only one iir.  Each ipr can have 0 or more ivr’s.

I want to export the data in a hierarchy similar to XML.  That is, I want to export an ipr, an iir and 0 or more ivrs.  This sequence must be repeated once for each ipr.  The main difference I can see between this and “real” XML is that this particular export must not have any tags.  Also, the actual records to be exported are fixed length char strings based on a concatenation of elements in the base tables.  For example, there are multiple fields in ipr.  These fields would be combined into a fixed length, fixed format string of 300 bytes.  I actually built some user defined functions to perform formatting for each of the record types.  Note that each record type has a record type indicator embedded in the string (POL, INS, VEH).  This is how the records are decoded on the receiving end.

Do you have any idea how this can be accomplished?  It sounds suspiciously like “FOR XML” might be useful for this.

0
Comment
Question by:DrLechter
5 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 17044954
\
DrLechter,
>  It sounds suspiciously like “FOR XML” might be useful for this.

yes you are right

See this
http://www.sitepoint.com/article/data-as-xml-sql-server
0
 
LVL 12

Expert Comment

by:Einstine98
ID: 17045658
For XML will give you actual XML output... you need to do it through the select statement... using functions may help

Many times I'm asked for non-standard XML format and I end up building it in the SQL statement

SELECT '<xml tab>' + field + '</tag>'
FROM TABLE

if the field is of decimal or integer type then you have to convert it to character

CAST (field AS varchar (100))

0
 
LVL 4

Author Comment

by:DrLechter
ID: 17053086
As I mentioned in my original question, I wanted the XML hierachical structure with none of the tags presents.  That would seem to suggest that if one were to use the FOR XML approach, one would have to string the tags out later.  It might also be necessary to convert meta characters back to their original form (such as &gt being converted back to ">").  

I decided to take another approach:  I put the records from my three tables into a temp table along with an artifical key.  The key allows me to sort the records into the correct order as required by the hierachical structure.  This approach allows me to generate the hierachical structure without having to undo the undesirable stuff the FOR XML does for this application.  Here is what I came up with:

(If anyone sees a more direct approach, I would appreciate the input).

      -- fill temp temp with Ivans_Policy_Records and key
      insert into #TempIvansRecord (SortKey, IvansRecord)
      select
            right (replicate (' ', 8) + convert (nvarchar,  ipr.Ivans_Policy_Record_ID), 8) -- iprID part of key
            + replicate (' ', 8)  -- iirID part of key
            + replicate (' ', 8),  -- ivrID part of key
            ipr.Ivans_Policy_Record            
      from Ivans_Policy_Record ipr
      where ipr.Ivans_Export_ID = @IvansExportID
      
      -- fill temp temp with Ivans_Insured_Records and key
      insert into #TempIvansRecord (SortKey, IvansRecord)
      select
            right (replicate (' ', 8) + convert (nvarchar,  ipr.Ivans_Policy_Record_ID), 8) -- iprID part of key
            + right (replicate (' ', 8) + convert (nvarchar,  iir.Ivans_Insured_Record_ID), 8) -- iirID part of key
            + replicate (' ', 8),  -- ivrID part of key
            iir.Ivans_Insured_Record            
      from Ivans_Insured_Record iir
      inner join Ivans_Policy_Record ipr on ipr.Ivans_Policy_Record_ID = iir.Ivans_Policy_Record_ID
      where ipr.Ivans_Export_ID = @IvansExportID
      
      -- fill temp temp with Ivans_Vehicle_Records and key
      insert into #TempIvansRecord (SortKey, IvansRecord)
      select
            right (replicate (' ', 8) + convert (nvarchar,  ipr.Ivans_Policy_Record_ID), 8) -- iprID part of key
            + right (replicate (' ', 8) + convert (nvarchar,  iir.Ivans_Insured_Record_ID), 8) -- iirID part of key
            + right (replicate (' ', 8) + convert (nvarchar,  ivr.Ivans_Vehicle_Record_ID), 8), -- ivrID part of key
            iir.Ivans_Vehicle_Record            
      from Ivans_Vehicle_Record ivr
      inner join Ivans_Policy_Record ipr on ipr.Ivans_Policy_Record_ID = iir.Ivans_Policy_Record_ID
      inner join Ivans_Insured_Record iir on iir.Ivans_Policy_Record_ID = ipr.Ivans_Policy_Record_ID
      where ipr.Ivans_Export_ID = @IvansExportID      
      
      -- now insert data into the real Ivans_Record table in the correct order
      insert into Ivans_Record (Ivans_Export_ID, Ivans_Record)
      select
            @IvansExportID,
            tir.IvansRecord
      from #TempIvansRecord tir
      order by tir.SortKey
      

0
 

Accepted Solution

by:
ee_ai_construct earned 0 total points
ID: 17404150
PAQ / Refund
ee ai construct, community support moderator
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
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.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

911 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now