应用XML交叉应用从XML文件中获取数据

时间:2015-01-11 14:08:25

标签: sql-server xml stored-procedures store cross-apply

我正在应用XML交叉应用程序,通过使用存储过程从具有多个节点的XML项目文件中提取数据 存储过程如下

INSERT INTO UCDetails
SELECT 
Usecase.value('@UserID','VARCHAR(100)') AS UCId, --ATTRIBUTE 
Usecase.value('@Name','VARCHAR(100)') AS UCName, --ATTRIBUTE 
Usecase.value('@PmAuthor','VARCHAR(100)') AS UCActor, --ATTRIBUTE 
UCPrecon.value('@Value','VARCHAR(MAX)') AS UCPre, --ATTRIBUTE 
UCPostcon.value('@Value','VARCHAR(MAX)') AS UCPost, --ATTRIBUTE 


FROM
@xml.nodes('/Project/Models/UseCase')AS TAB(Usecase) OUTER  
apply @xml.nodes('/Project/Models/UseCase/TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue[5] ')AS TAB1(UCPrecon)OUTER  
apply @xml.nodes('/Project/Models/UseCase/TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue[6]')AS TAB2(UCPostcon)

END

我没有获得单行数据,而是获得交叉乘法行,如下所示

UCId            UCName           UCActor         UCPre                  UCPost        
UC01         Login            Bilal Haider    User must be Registerd User is Loggined sucessfully      

UC01            Login            Bilal Haider    User must be Registerd User is added   

UC01            Login            Bilal Haider    User is Loggined sucessfully   User is Loggined sucessfully    

UC01            Login            Bilal Haider    User is Loggined sucessfully   User is added   

UC02            Add User         Bilal Haider    User must be Registerd  User is Loggined sucessfully   

UC02            Add User         Bilal Haider    User must be Registerd  User is added  

UC02            Add User         Bilal Haider    User is Loggined sucessfully   User is Loggined sucessfully    

UC02         Add User         Bilal Haider    User is Loggined sucessfully   User is added

很抱歉发布这样的结果 正确的行以粗体突出显示,但为什么我要获取所有其他行?

XML文件链接: XML FILE

1 个答案:

答案 0 :(得分:1)

请勿对TaggedValues使用交叉申请。请改为在values子句中指定xPath。

select T.X.value('@UserID', 'varchar(100)'),
       T.X.value('@Name', 'varchar(100)'),
       T.X.value('@PmAuthor', 'varchar(100)'),
       T.X.value('(TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue)[5]/@Value', 'varchar(max)'),
       T.X.value('(TaggedValues/TaggedValueContainer/ModelChildren/TaggedValue)[6]/@Value', 'varchar(max)')
from @xml.nodes('/Project/Models/UseCase') as T(X)

结果

------ ---------- ------------- ------------------------------ ------------------------------
UC01   Login      Bilal Haider  User must be Registerd         User is Loggined sucessfully
UC02   Add User   Bilal Haider  User is Loggined sucessfully   User is added