Solved

Converting to XML-like format

Posted on 2006-07-05
5
195 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

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…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

706 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

16 Experts available now in Live!

Get 1:1 Help Now