2 回答
TA贡献1815条经验 获得超6个赞
您需要单独使用CROSS APPLY和获取枚举<OptionalItemIds>- 尝试这样的事情:
INSERT INTO Products(Size, ShortName, Id, Description, OptionalItemIds, Calories, Name)
SELECT
tbl.Stores.value('Size[1]', 'nvarchar(20)') AS Size,
tbl.Stores.value('ShortName[1]', 'nvarchar(20)') AS ShortName,
tbl.Stores.value('Id[1]', 'nvarchar(250)') AS Id,
tbl.Stores.value('Description[1]', 'nvarchar(20)') AS Description,
-- tbl.Stores.value('OptionalItemIds.[1]', 'nvarchar(250)') AS OptionalItemIds,
tbl2.ItemIds.value('.', 'int') AS OptionalItemId,
tbl.Stores.value('Calories[1]', 'nvarchar(20)') AS Calories,
tbl.Stores.value('Name[1]', 'nvarchar(20)') AS Name
FROM
@XmlProduct.nodes('/Stores/Products') AS tbl(Stores)
-- get the list of "OptionalItemIds" separately, cross apply to produce rows to be inserted
CROSS APPLY
@XmlProduct.nodes('/Stores/Products/OptionalItemIds') AS tbl2(ItemIds)
;
TA贡献1841条经验 获得超3个赞
这段代码对我有用,
INSERT INTO Products(Size, ShortName, Id, Description, OptionalItemIds, Calories, Name)
SELECT
tbl.Stores.value('Size[1]', 'nvarchar(20)') AS Size,
tbl.Stores.value('ShortName[1]', 'nvarchar(20)') AS ShortName,
tbl.Stores.value('Id[1]', 'nvarchar(250)') AS Id,
tbl.Stores.value('Description[1]', 'nvarchar(20)') AS Description,
tbl2.ItemIds.value('.', 'nvarchar(20)') AS OptionalItemIds,
tbl.Stores.value('Calories[1]', 'nvarchar(20)') AS Calories,
tbl.Stores.value('Name[1]', 'nvarchar(20)') AS Name
FROM
@XmlProduct.nodes('/Stores/Products') AS tbl(Stores)
CROSS APPLY tbl.Stores.nodes('OptionalItemIds') AS tbl2(ItemIds)
- 2 回答
- 0 关注
- 122 浏览
添加回答
举报