Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Inserting XML Into Table

Author  Topic 

mapidea
Posting Yak Master

124 Posts

Posted - 2010-02-16 : 12:44:55
I have a XML which I want to insert into table. It has a attribute value which has to be checked to insert into the particular column.

<OrderList StoreAccountName="gameSdirect">
<Order currency="USD" id="gameSdirect-128093">
<Time>Tue Feb 9 16:41:35 2010 GMT</Time>
<IPAddress>11.11.11..11</IPAddress>
<Shipping>USPS Economy Mail</Shipping>

<Total>
<Line type="Subtotal" name="Subtotal">29.99</Line>
<Line type="Shipping" name="Shipping">0.00</Line>
<Line type="Tax" name="Tax">0.00</Line>
<Line type="Total" name="Total">29.99</Line>
</Total>
<Space-Id></Space-Id>
</Order>
</OrderList>


How can I modify the SQL to get the appropriate values for subtotal, shipping and tax

INSERT INTO [Yahoo_Customer_Order]
([Login_User_Id]
,[Order_Id]
,[Currency]
,[IPAddress]
,[Shipping_Type]
,[Subtotal]
,[Shipping]
,[Tax]
,[Total]
,[Order_Time]
,[Inserted_DateTime])

SELECT

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-16 : 13:12:42
do you mean this?


declare @x xml
set @x='<OrderList StoreAccountName="gameSdirect">
<Order currency="USD" id="gameSdirect-128093">
<Time>Tue Feb 9 16:41:35 2010 GMT</Time>
<IPAddress>11.11.11..11</IPAddress>
<Shipping>USPS Economy Mail</Shipping>

<Total>
<Line type="Subtotal" name="Subtotal">29.99</Line>
<Line type="Shipping" name="Shipping">0.00</Line>
<Line type="Tax" name="Tax">0.00</Line>
<Line type="Total" name="Total">29.99</Line>
</Total>
<Space-Id></Space-Id>
</Order>
</OrderList>'


select Login_User_Id,OrderID,Currency,IPAddress,ShippingType,
MAX(case when typeval='Subtotal' then value else null end) as Subtotal,
MAX(case when typeval='Shipping' then value else null end) as Shipping,
MAX(case when typeval='Tax' then value else null end) as Tax,
MAX(case when typeval='Total' then value else null end) as Total,
OrderTime,
GETDATE() AS Inserted_DateTime
FROM
(
select t.u.value('../@StoreAccountName','varchar(100)') as Login_User_Id,
t.u.value('@id','varchar(100)') as OrderID,
t.u.value('@currency','varchar(100)') as Currency,
t.u.value('./IPAddress[1]','varchar(100)') as IPAddress,
t.u.value('./Shipping[1]','varchar(100)') as ShippingType,
t.u.value('./Time[1]','varchar(100)') as OrderTime,
a.b.value('.','decimal(10,2)') as value,
a.b.value('@type','varchar(100)') as typeval
from @x.nodes ('/OrderList/Order') t (u)
cross apply u.nodes('./Total/Line')a(b)
)t
GROUP BY Login_User_Id,OrderID,Currency,IPAddress,ShippingType,OrderTime


Login_User_Id OrderID Currency IPAddress ShippingType Subtotal Shipping Tax Total OrderTime Inserted_DateTime
gameSdirect gameSdirect-128093 USD 11.11.11..11 USPS Economy Mail 29.99 0.00 0.00 29.99 Tue Feb 9 16:41:35 2010 GMT 2010-02-16 23:40:03.957



------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

mapidea
Posting Yak Master

124 Posts

Posted - 2010-02-17 : 13:24:32
Thanks a lot Visakh.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-18 : 01:11:51
welcome

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -