栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

在SQL Server中遍历XML变量

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

在SQL Server中遍历XML变量

像这样吗?

DECLARE @XmlVariable XML = '<parent_node>        <category>Low</category>        <category>Medium</category>        <category>High</category>      </parent_node>'INSERT INTO dbo.YourTargetTable(CategoryColumn)  SELECt      XTbl.Cats.value('.', 'varchar(50)')  FROM      @XmlVariable.nodes('/parent_node/category') AS XTbl(Cats)

更新: 如果您 必须
使用旧的旧式存储过程并且不能更改它(这是我的首选方式),那么您将不得不自己进行逐行行(RBAR)循环,例如通过使用一个表变量:

-- declare temporary work tableDECLARE @RbarTable TABLE (CategoryName VARCHAr(50))-- insert values into temporary work tableINSERT INTO @RbarTable(CategoryName)  SELECt      XTbl.Cats.value('.', 'varchar(50)')  FROM      @XmlVariable.nodes('/parent_node/category') AS XTbl(Cats)-- declare a single categoryDECLARE @CategoryNameToBeInserted VARCHAr(50)-- get the first categorySELECt TOP 1 @CategoryNameToBeInserted = CategoryName FROM @RbarTable-- as long as we have dataWHILE @CategoryNameToBeInserted IS NOT NULLBEGIN    -- execute your stored procedure here.....        EXEC sp_executesql N'dbo.YourStoredProcedure @CategoryName',  N'@CategoryName VARCHAr(50)',  @CategoryName = @CategoryNameToBeInserted    -- delete the category we just inserted from the temporary work table    DELETe FROM @RbarTable WHERe CategoryName = @CategoryNameToBeInserted    -- see if we still have more categories to insert        SET @CategoryNameToBeInserted = NULL    SELECt TOP 1 @CategoryNameToBeInserted = CategoryName FROM @RbarTable ORDER BY CategoryNameEND


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/456246.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号