A stored procedure in SQL Server that creates a table as an XML parameter

Source: Internet
Author: User

Tools to use:

    • Sql server
    • Java
    • MyBatis

First step: Create a function to get the data in the XML

CREATE function create_table (@str XML) returns @tb table (SourceID varchar) asbegin      INSERT INTO @tb          SELECT         v.value (' @sourceId [1] ', ' VARCHAR ') as SourceIDfrom        @str. Nodes ('/ Rsssources/rsssource ') x (v)    return    end

Step two: Create a table with a stored procedure call to the function above

CREATE PROCEDURE t_pro (@doc xml) as BEGIN         CREATE TABLE tbl (sourceid varchar)INSERT into     tbl     select B.sourceid from      create_table (@doc) b END

Step three: Call the stored procedure in the database

DECLARE @doc XML   = ' <rsssources><rsssource sourceid= ' 1 "/><rsssource sourceid=" 2 "/><rsssource sourceId=" 3 "/> <rsssource sourceid= "4"/></rsssources> ' EXEC t_pro @doc

Fourth step: MyBatis call the stored procedure

<select id= "CreateTable" statementtype= "callable" resulttype= "string" >        {call T_pro (#{xml})} </select>

Fifth step: Calling a stored procedure in a Java program

Sqlsessionfactory factory == = Sqlsession.getmapper (newsinfomapper.  Class= "<rsssources><rsssource sourceid=\" 1\ "/><rsssource sourceid=\" 2\ "/>< Rsssource sourceid=\ "3\"/><rsssource sourceid= "4"/></rsssources> "; Mapper.createtable ( SourceXML); Sqlsession.commit (); Sqlsession.close () ;

A stored procedure in SQL Server that creates a table as an XML parameter

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.