Solved

Converting to XML-like format

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
unable to reinstall SCVMM 2012 R2 - SQL errors. 5 35
find SQL job run average duration 24 53
How come this XML node is not read? 3 26
access to sql migration 5 22
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

740 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