Solved

Converting to XML-like format

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

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