zoukankan      html  css  js  c++  java
  • SqlServer中以xml作为参数创建表格的存储过程

    用到的工具:

    • SqlServer
    • java
    • mybatis

    第一步:创建function,用于获取xml中的数据

     CREATE function create_table (@str xml)
     returns @tb table(sourceId varchar(20))
     as
     begin
          insert into @tb 
             SELECT
             v.value('@sourceId[1]','VARCHAR(20)') AS sourceId
            FROM @str.nodes('/RSSsources/rssSource') x(v)
        return   
     end

    第二步:用存储过程调用上面的函数创建表格

    CREATE PROCEDURE T_PRO(@doc xml)
     AS
     BEGIN
        
         CREATE TABLE tbl(sourceId varchar(20))
         INSERT INTO tbl
         select b.sourceId from 
         create_table(@doc) b
     END

    第三步:在数据库中调用存储过程

     DECLARE @doc xml 
     SET @doc = '<RSSsources><rssSource sourceId="1"/><rssSource sourceId="2"/><rssSource sourceId="3"/><rssSource sourceId="4"/></RSSsources>'
    
     EXEC T_PRO @doc

    第四步:mybatis调用存储过程

    <select id="createTable" statementType="CALLABLE" resultType="string">
            {call T_PRO(#{xml})}
    </select>

    第五步:java程序中调用存储过程

    SqlSessionFactory factory = GetSqlSessionFactory.getInstance();
    SqlSession sqlSession = factory.openSession();
    NewsInfoMapper mapper = sqlSession.getMapper(NewsInfoMapper.class);
    String sourceXml = "<RSSsources><rssSource sourceId="1"/><rssSource sourceId="2"/><rssSource sourceId="3"/><rssSource sourceId="4"/></RSSsources>";
    mapper.createTable(sourceXml);
    sqlSession.commit();
    sqlSession.close();
  • 相关阅读:
    idea2020 安装
    739. 每日温度
    图像翻转
    257. 二叉树的所有路径
    1466. 重新规划路线
    面试题 04.05. 合法二叉搜索树
    671. 二叉树中第二小的节点
    965. 单值二叉树
    648. 单词替换
    137. 只出现一次的数字 II
  • 原文地址:https://www.cnblogs.com/hzwl-2015/p/4228914.html
Copyright © 2011-2022 走看看