开发者

Having Trouble Shredding XML to SQL Table

开发者 https://www.devze.com 2023-03-30 16:46 出处:网络
Using SQL Server 2008, I need to select this XML into a Table.I can\'t get it to return each row. Declare @CurrentStatusInfoList xml

Using SQL Server 2008, I need to select this XML into a Table. I can't get it to return each row.

Declare @CurrentStatusInfoList xml
Set @CurrentStatusInfoList = '<CurrentStatusInfoList>
  <CurrentStatusInfo Caption="Route" ItemID="568" eFolderID="0900000000000000000000000058170" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="615" eFolderID="0900000000000000000000000059580" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="1199" eFolderID="0900000000000000000000000066117" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="997" eFolderID="0900000000000000000000000063447" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="1208" eFolderID="0900000000000000000000000066265" eStageName="ustg_Route" /> 
  <CurrentStatus开发者_开发知识库Info Caption="Route" ItemID="1577" eFolderID="0900000000000000000000000069853" eStageName="ustg_Route" /> 
</CurrentStatusInfoList>'

 DECLARE @StatusTable 
    TABLE(           
             eFolderID varchar(100),
             ItemID bigint,
             eStageName varchar(100),
             Caption varchar(100)
         )

 INSERT INTO              
  @StatusTable (eFolderID, ItemID,eStageName,Caption)              
 SELECT               
  @CurrentStatusInfoList.value('(//@eFolderID)[1]', 'varchar(100)') as eFolderID,
  @CurrentStatusInfoList.value('(//@ItemID)[1]', 'bigint') as ItemID,
  @CurrentStatusInfoList.value('(//@eStageName)[1]', 'varchar(100)') as eStageName, 
  @CurrentStatusInfoList.value('(//@Caption)[1]', 'varchar(100)') as Caption 
FROM @CurrentStatusInfoList.nodes('//CurrentStatusInfoList/CurrentStatusInfo') 
    AS ParamValues(eFolderID)                 

 Select * from @StatusTable 


Try this:

Declare @CurrentStatusInfoList xml
Set @CurrentStatusInfoList = '<CurrentStatusInfoList>
  <CurrentStatusInfo Caption="Route" ItemID="568" eFolderID="0900000000000000000000000058170" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="615" eFolderID="0900000000000000000000000059580" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="1199" eFolderID="0900000000000000000000000066117" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="997" eFolderID="0900000000000000000000000063447" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="1208" eFolderID="0900000000000000000000000066265" eStageName="ustg_Route" /> 
  <CurrentStatusInfo Caption="Route" ItemID="1577" eFolderID="0900000000000000000000000069853" eStageName="ustg_Route" /> 
</CurrentStatusInfoList>'

 DECLARE @StatusTable 
    TABLE(           
             eFolderID varchar(100),
             ItemID bigint,
             eStageName varchar(100),
             Caption varchar(100)
         )

 INSERT INTO              
  @StatusTable (eFolderID, ItemID,eStageName,Caption)              
 SELECT               
  CurrentStatusInfo.value('@eFolderID', 'varchar(100)') as eFolderID,
  CurrentStatusInfo.value('@ItemID', 'bigint') as ItemID,
  CurrentStatusInfo.value('@eStageName', 'varchar(100)') as eStageName, 
  CurrentStatusInfo.value('@Caption', 'varchar(100)') as Caption 
FROM @CurrentStatusInfoList.nodes('//CurrentStatusInfoList/CurrentStatusInfo') as CurrentStatusInfoList(CurrentStatusInfo)

 Select * from @StatusTable 
0

精彩评论

暂无评论...
验证码 换一张
取 消