我有一个存储过程,其中我有一个表格如下:
CREATE TEMPORARY TABLE tt_ASSET_DETAILS
(
SiteGroup VARCHAR(30),
SiteNumber VARCHAR(50),
AssetCategory VARCHAR(30),
Address VARCHAR(50),
AssetName VARCHAR(50)
);
我想从以下XML中将数据插入此表:
<ManageAsset>
<Asset>
<SiteGroup>Chicago</SiteGroup>
<SiteNumber>4524</SiteNumber>
<AssetCategory>Passenger Cars</AssetCategory>
<Address>Address 1</Address>
<AssetNumber>Tango</AssetNumber>
</Asset>
<Asset>
<SiteGroup>Cincinnati</SiteGroup>
<SiteNumber>5360</SiteNumber>
<AssetCategory>Passenger Cars</AssetCategory>
<Address>Address 2</Address>
<AssetNumber>Mango</AssetNumber>
</Asset>
</ManageAsset>
现在,我希望将节点集<Asset> </Asset>
之间的数据作为第一行插入,将另一组<Asset> </Asset>
作为第二行插入。
为此我使用了以下代码,它将<Asset> </Asset>
的两个集合插入到表的一行中,
INSERT INTO tt_ASSET_DETAILS(SiteGroup,SiteNumber,AssetCategory,Address,AssetName)
SELECT SiteGroup,SiteNumber,AssetCategory,Address,AssetNumber
FROM
(select ExtractValue(AssetsXML,'//ManageAsset/Asset/SiteGroup') as SiteGroup,
ExtractValue(AssetsXML,'//ManageAsset/Asset/SiteNumber') as SiteNumber,
ExtractValue(AssetsXML,'//ManageAsset/Asset/AssetCategory') as AssetCategory,
ExtractValue(AssetsXML,'//ManageAsset/Asset/Address') as Address,
ExtractValue(AssetsXML,'//ManageAsset/Asset/AssetNumber') as AssetNumber) XM;
有人,请帮我纠正我的代码,以便达到我所需的结果!!!
答案 0 :(得分:1)
我已经得到了我的问题的答案。使用WHILE循环满足需要!
SET @COUNT = (SELECT EXTRACTVALUE(AssetsXML,'COUNT(/ManageAsset/Asset)'));
SET @I = 1;
WHILE (@I <= @COUNT) DO
INSERT INTO tt_ASSET_DETAILS(SiteGroup,SiteNumber,AssetCategory,Address,AssetName)
SELECT ExtractValue(AssetsXML,CONCAT('/ManageAsset/Asset[',@I,']/SiteGroup')),
ExtractValue(AssetsXML,CONCAT('/ManageAsset/Asset[',@I,']/SiteNumber')),
ExtractValue(AssetsXML,CONCAT('/ManageAsset/Asset[',@I,']/AssetCategory')),
ExtractValue(AssetsXML,CONCAT('/ManageAsset/Asset[',@I,']/Address')),
ExtractValue(AssetsXML,CONCAT('/ManageAsset/Asset[',@I,']/AssetNumber'));
SET @I = @I + 1;
END WHILE;
现在,如果我将表中的数据作为SELECT * FROM tt_ASSET_DETAILS;
查询,我将把数据插入到不同行的表中!