欢迎来到天天文库
浏览记录
ID:38208361
大小:23.93 KB
页数:5页
时间:2019-06-07
《SQL 解析XML 解决方案》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、Sql解析XML解决方案1.1、@XML为数据传入的XML格式2.root为根目录3.为对应需要插入的表,详见一对多或者多对多的xml格式4.多对多是为该条数据对应的从表的唯一标识,可以为其他名称的字段。如:115.注意案例存储过程只定义了2个参数,可以根据自己的具体需求增加参数。一、对单表(单条或者多条)的操作。declare@XMLxmlset@XML=N'1
2、ID>test12test2'InsertintoTemp(ID,Name)selectS.value('(ID)[1]','int')asID,S.value('(name)[1]','nvarchar')asname,from@XML.nodes('/root/A')T(S)注意:A为表名ID,Name为此表对应的字段名二、对多表(一对多)的操作declare@CustomerIDintdeclare@XMLxmlset@XML=N'3、ot>深圳大展1162626'InsertintoCustomer(CustomerNam4、e,CustomerTypeID)selectS.value('(CustomerName)[1]','nvarchar')asCustomerName,S.value('(CustomerTypeID)[1]','int')asCustomerTypeIDfrom@XML.nodes('/root/Customer')T(S)set@CustomerID=@@IDENTITYinsertintoCustomerBranch(CustomerID,AreaID,BranchID)select@CustomerID,S.value('(AreaID)5、[1]','int')asAreaID,S.value('(BranchID)[1]','int')asBranchIDfrom@XML.nodes('/root/CustomerBranch')T(S)一、对多表(多对多)的操作declare@CustomerIDintdeclare@Countintdeclare@Errorintdeclare@XMLxmlset@XML=N'1深圳大展1166、merTypeID>2艾默生1161262467、erBranch>223'select@Count=MAX(RowNumber)from(selectROW_NUMBER()OVER(ORDERBYS.value('(ID)[1]','int'))ASRowNumberfrom@XML.nodes('/root/Customer')T(S))asmwhile(@Count>0)begininsertintoCustomer(8、CustomerName,CustomerTypeID)selectCustomerName,CustomerTypeID
3、ot>深圳大展1162626'InsertintoCustomer(CustomerNam
4、e,CustomerTypeID)selectS.value('(CustomerName)[1]','nvarchar')asCustomerName,S.value('(CustomerTypeID)[1]','int')asCustomerTypeIDfrom@XML.nodes('/root/Customer')T(S)set@CustomerID=@@IDENTITYinsertintoCustomerBranch(CustomerID,AreaID,BranchID)select@CustomerID,S.value('(AreaID)
5、[1]','int')asAreaID,S.value('(BranchID)[1]','int')asBranchIDfrom@XML.nodes('/root/CustomerBranch')T(S)一、对多表(多对多)的操作declare@CustomerIDintdeclare@Countintdeclare@Errorintdeclare@XMLxmlset@XML=N'1深圳大展1166、merTypeID>2艾默生1161262467、erBranch>223'select@Count=MAX(RowNumber)from(selectROW_NUMBER()OVER(ORDERBYS.value('(ID)[1]','int'))ASRowNumberfrom@XML.nodes('/root/Customer')T(S))asmwhile(@Count>0)begininsertintoCustomer(
6、merTypeID>
7、erBranch>223
8、CustomerName,CustomerTypeID)selectCustomerName,CustomerTypeID
此文档下载收益归作者所有