views:

53

answers:

1

I have an issue where I must shred multiple xml datasets from multiple providers into a single set of tables by mapping various elements in the sources to my own tables. I can do this just fine when I am gathering the top level information, but some of my information is in a collection, such as the categories. My first step is just to pull the data out of XML and put it into a few tables and then the next step is to merge the data into my real tables.

My XML Data looks like this:

<ArrayOfApiNewItemDetailInfo>
  <ApiNewItemDetailInfo>
    <categories>
      <ApiNavCategoryInfo>
        <count></count>
        <id></id>
        <name></name>
        <selected></selected>
      </ApiNavCategoryInfo>
      <ApiNavCategoryInfo>
        <count></count>
        <id></id>
        <name></name>
        <selected></selected>
      </ApiNavCategoryInfo>
    </categories>
    <item_id></item_id>
    <product_id></product_id>
   </ApiNewItemDetailInfo>
<ArrayOfApiNewItemDetailInfo>

I want to shred this data into a table with product id, item id, and category id, when multiple categories can be assigned to a single product

Firstly, I have my DataDictionary table

CREATE TABLE [dbo].[DataDictionary](
 [Id] [int] IDENTITY(1,1) NOT NULL,
 [Provider] [varchar](50) NOT NULL,
 [XmlColumn] [varchar](100) NOT NULL,
 [Datatype] [varchar](100) NOT NULL,
 [DestinationColumn] [varchar](100) NOT NULL
)

In this table I have these records:

88 Categories product_id int ProviderProductId
89 Categories item_id int ItemId
93 Categories categories/ApiNavCategoryInfo/id int CategoryId

Now, I have a sql statement that pulls in the data dictionary for my provider and shreds the data into a table.

CREATE TABLE #SQLWork (Row_Id INT IDENTITY,SQL varchar(500)) 

 INSERT #SQLWork (SQL) VALUES ('DROP TABLE TempCategories')
 INSERT #SQLWork (SQL) VALUES ('SELECT ')
 INSERT #SQLWork (SQL) SELECT 'Categories.value(''' + XmlColumn + '[1]''' + ',''' + Datatype + ''') as ' + DestinationColumn + ', '
     FROM DataDictionary WHERE UPPER(Provider) = 'CATEGORIES' 

 UPDATE #SQLWork SET SQL = REPLACE(SQL,', ','') WHERE row_id = @@IDENTITY

 INSERT #SQLWork (SQL) VALUES ('  INTO TempCategories ')
 INSERT #SQLWork (SQL) VALUES (' FROM RawDetails CROSS APPLY Data.nodes(''' + '//ArrayOfApiNewItemDetailInfo/ApiNewItemDetailInfo' + ''') as NewTable(Categories)')

 DECLARE @SQL nvarchar(max)

 select @SQL = coalesce(@SQL + ' ',' ') + Sql from #SQLWork Order By Row_Id
 --SELECT @SQL
 exec sp_executesql @sql

 drop table #Sqlwork

The sql statement that is generated looks like this:

 DROP TABLE TempCategories 
 SELECT  
    Categories.value('product_id[1]','int') as ProviderProductId,  
    Categories.value('item_id[1]','int') as ItemId,  
    Categories.value('categories/ApiNavCategoryInfo/id[1]','int') as CategoryId   
 INTO 
    TempCategories   
FROM 
    RawDetails 
CROSS APPLY 
    Data.nodes('//ArrayOfApiNewItemDetailInfo/ApiNewItemDetailInfo') as NewTable(Categories)

When I run my statement, I get the error:

XQuery [RawDetails.Data.value()]: 'value()' requires a singleton (or empty sequence), found operand of type 'xdt:untypedAtomic *'

Any ideas?

Thanks.

A: 

I found out that my paths were wrong.

I changed

Data.nodes('//ArrayOfApiNewItemDetailInfo/ApiNewItemDetailInfo') as NewTable

to

Data.nodes('//ArrayOfApiNewItemDetailInfo/ApiNewItemDetailInfo/categories/ApiNavCategoryInfo') as NewTable

And I changed my records with this

88 Categories ../../product_id int ProviderProductId 
89 Categories ../../item_id int ItemId 
93 Categories id int CategoryId 

And that worked.

Josh