У меня есть два отдельных XML-файла, которые находятся в таблице dbo.XMLwithOpenXML
в SQL Server, с Id
в качестве первичного ключа.
Я пытаюсь извлечь каждый XML-документ в таблицы, которые я могу успешно сделать,однако я хотел бы пройтись по моим двум XML и извлечь данные таким образом.Мои текущие попытки только позволяют мне извлекать второй XML-документ и не извлекают ничего из первого.
declare
@XMLData XML,
@TableName Varchar(255) = 'XML_Tables',
@ServerName varchar(255) = 'Census',
@Id int,
@locationiterator int = 1
Select @XMLData = XMLData from XMLwithOpenXML;
Declare ApplicationCursor cursor for
select Id
from [dbo].[XMLwithOpenXML]
where LoadedDateTime > '2019-02-20 16:07:57.227'
open ApplicationCursor
Truncate Table [dbo].[DeliveryDetails]
Truncate Table [dbo].[Items]
fetch next from ApplicationCursor into @Id
while @@FETCH_STATUS = 0
begin
select @XMLData
Output
----------------------------------------------------------------------------------------------------------
--Delivery Details--
insert into [dbo].[DeliveryDetails]
(
[PurchaseOrder]
,[ShippingType]
,[Street]
,[City]
,[State]
,[Zip]
,[Country]
,Comments
)
SELECT pd.value('(@PurchaseOrderNumber)[1]','nvarchar(max)') As PurchaseOrder
,pd.value('(@OrderDate)[1]','nvarchar(max)') As OrderDate
,pd.value('(Address/@Type)[1]','nvarchar(max)') as DeliveryType
,pd.value('(Address/Name/text())[1]','nvarchar(max)') as test
,pd.value('(Address/Street/text())[1]','nvarchar(max)') AS Street
,pd.value('(Address/City/text())[1]','nvarchar(max)') as City
,pd.value('(Items//Item/@PartNumber)[1]','nvarchar(max)') as PartNumber
,pd.value('(DeliveryNotes/text())[1]','nvarchar(max)') as DeliveryNotes
from @xmlData.nodes('/PurchaseOrders/PurchaseOrder') as i(pd);
SET @locationiterator = @locationiterator + 1
END
----------------------------------------------------------------------------------
--Item Details--
insert into [dbo].[Items]
(
PurchaseOrder,Item, ProductName, Quantity, USPrice
)
Select
Rec.value('../../@PurchaseOrderNumber[1]','nvarchar(max)') as PurchaseOrderNumber
,Rec.value('@PartNumber[1]','nvarchar(max)') As ItemNumber
,Rec.value('ProductName[1]','nvarchar(max)') as ProductName
,Rec.value('Quantity[1]','nvarchar(max)') as Quantity
,Rec.value('USPrice[1]','nvarchar(max)') as USPrice
from @xmlData.nodes('//PurchaseOrders/PurchaseOrder/Items/Item') as x(Rec);
--------------------------------------------------------------------------
fetch next from ApplicationCursor
into @Id
End
Close ApplicationCursor
Deallocate ApplicationCursor