Solved

How do I convert an SQL stored XML document into discrete columns.

Posted on 2007-03-19
4
219 Views
Last Modified: 2007-04-22
You know when you say "Hey, that'll be easy" and it isn't?! ...

I've been given a shrinkwrapped app to work with. They want me to produce reports.

The original app's authors are not available and they will never be available and we cannot change the app and we have to just live with it.

So.

The app is a web app.

Instead of having a table with lots of columns, they have a table with 3 columns.

Col1 = UniqueID
Col2 = XML document
Col3 = A state (an int valued from 1 to 5).

I'm trying to get the data from the xml via an SQL command.

I can see that I can use OPENXML on MS SQL 2000, but I can't quite see how I use it.

The table is called WorkRequests and the column is called WR_SerializedObject.

What I want to do is a sort of

SELECT WR_ID, unserialized(WR_SerializedObject)  FROM WorkRequests

sort of thing.

WorkRequests is the table, WR_ID is the row's unique ID, WR_SerializedObject is the XML document.

Now I can see from the document, that they've made it include the same element 10 times ...

This is an edited doc (only the data has been edited).

<?xml version="1.0" encoding="utf-16"?>
<WorkRequest xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
      <State>BreakDownList</State>
      <SerializedObject />
      <LastEditedBy>Kevin M</LastEditedBy>
      <JobSheetNumber>039920</JobSheetNumber>
      <TimeOnScene>17:10</TimeOnScene>
      <CallOutAllowed>None</CallOutAllowed>
      <TyreMakeOrdered>RQuadling</TyreMakeOrdered>
      <Eta>13/07/2006 17:10</Eta>
      <FaxNumber>01555 123 345</FaxNumber>
      <TelephoneNumber>01555 555 987</TelephoneNumber>
      <TimeAllocated>15:39</TimeAllocated>
      <ServiceProvicerStaffName>Pete</ServiceProvicerStaffName>
      <ServiceProviderUsed>Bob Smith Quick Fitters</ServiceProviderUsed>
      <CallCentreReferenceNumber />
      <CallCenterUsed>None</CallCenterUsed>
      <Notes />
      <ConfirmDefectNumberRequired>No</ConfirmDefectNumberRequired>
      <VehicleLoaded>No</VehicleLoaded>
      <VehicleDueOutBy>13/07/2006 16:45</VehicleDueOutBy>
      <OtherVisibleDamage />
      <Mileage />
      <TrailerUnitRegistrationNumber>Free</TrailerUnitRegistrationNumber>
      <ColourOfVehicle>No</ColourOfVehicle>
      <Location>AX12 2FG</Location>
      <SpareOnBoard>No</SpareOnBoard>
      <TyreSize>10 22.5</TyreSize>
      <ConfirmRegistrationNumber>BGM39</ConfirmRegistrationNumber>
      <DamageDescription>N/A</DamageDescription>
      <TyreReason>Worn</TyreReason>
      <Position>NSR</Position>
      <DriversMobile>N/A</DriversMobile>
      <DriversName>N/A</DriversName>
      <ContactTelephoneNumber>01555 123 321</ContactTelephoneNumber>
      <ContactName>Jason</ContactName>
      <SpecificInstructions>Remoulds Acceptable</SpecificInstructions>
      <DoubleDeck>N/A</DoubleDeck>
      <Nofl>N/A</Nofl>
      <RegistrationNumber>BGM39</RegistrationNumber>
      <DefectNumberRequired>No</DefectNumberRequired>
      <HHCustomer>DHL</HHCustomer>
      <CallerLocation>FL</CallerLocation>
      <CallerName>Jason</CallerName>
      <Time>15:12</Time>
      <Date>13/07/2006</Date>
      <Contract>Bob Matton</Contract>
      <BTCOrThirdPartyJob>BTC</BTCOrThirdPartyJob>
      <StaffName>Michelle O</StaffName>
      <Comments />
      <Refusals>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
            <Refusal>
                  <RefusalReason />
                  <StaffNameSpokenTo />
                  <DepotLocation />
                  <ServiceProviderName />
            </Refusal>
      </Refusals>
</WorkRequest>

As I can't change the app, what I'm looking to do is add a trigger (insert/update) to update another table with the values (delete trigger to delete the values).

The app is low usage realtime, so even adding a second to the update/insert wouldn't be noticed. I don't think the app allow for the amending of MANY rows at the same time, so "inserted/deleted" should only ever be 1 row.

I think I would need 2 sets of results to insert/update the new table. The first would be of everything EXCEPT the refusals, the next would be the refusals per WR_ID, unless I can get RefusalReason_1, RefusalReason_2, etc.

I know I could use a whole ton of string manipulation, but hey, it IS XML already, so there should be some tricks I can use!

Pointers, suggestions, sympathies all accepted.

TIA.

Richard Quadling.
0
Comment
Question by:Richard Quadling
4 Comments
 
LVL 42

Expert Comment

by:EugeneZ
ID: 18757629
0
 
LVL 40

Author Comment

by:Richard Quadling
ID: 18762524
Thank you for that.

I've managed to get this to work as follows ...

declare @idoc int
declare @doc nvarchar(4000)
select top 1 @doc = convert(nvarchar(4000), wr_serializedobject) from workrequests where len(convert(nvarchar(4000), wr_serializedobject)) < 4000
exec sp_xml_preparedocument @idoc OUTPUT, @doc
select * from openxml(@idoc, '/WorkRequest',2)
exec sp_xml_removedocument @idoc


This is fine except that the majority of the documents are greater than 4000 characters (the XML data has a UTF-16 encoding! ARGH!).

As I want to operate on this data within T-SQL and you can't use text, ntext or varchar(8001+) or nvarchar(4001+), how do I process this XML?

0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18955671
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SSIS Conditional Split 7 31
get column names from table in vb.net 8 26
Get the latest status 8 30
Inserting oldest record into new table. 5 22
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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 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…

785 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