麻烦切碎XML到SQL表

时间:2011-08-25 22:50:23

标签: sql xml sql-server-2008 xpath

使用SQL Server 2008,我需要在表中选择此XML。我不能让它返回每一行。

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               
  @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 

1 个答案:

答案 0 :(得分:3)

试试这个:

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