将XML文件作为存储过程中的输入参数传递

时间:2014-11-07 16:50:38

标签: sql-server xml

是否可以将XML文件作为参数传递给SQL Server中的存储过程? 我试过这个,但它没有做到它应该做的事情:

DECLARE @XmlString2 as XML <--I have a feeling this is the problem
SET @XmlString2 = 'cast(x as XML)
from openrowset (bulk '''+@FileName+''', single_blob) as T(x)'

DECLARE cur CURSOR LOCAL for
SELECT 
    PA_ID = XTbl.value('(Answer_ID)[1]', 'varchar(400)'),
    Question_ID = XTbl.value('(../../ID)[1]', 'bigint'),
    QuestionText = XTbl.value('(../../QuestionText)[1]', 'varchar(200)'),
    QuestionType = XTbl.value('(../../QuestionType)[1]','bigint'),
    Questionaire_ID = XTbl.value('(../../QuestionaireID)[1]','bigint'),
    Filter = XTbl.value('(../../Filter)[1]', 'bigint'),
    Value = XTbl.value('(Value)[1]','varchar(400)'),
    RequiresExplanation = XTbl.value('(RequiresExplanation)[1]','int'),
    ReviewRequired = XTbl.value('(ReviewRequire)[1]','char(1)')

    from @XmlString2.nodes('/Questions/Question/PossibleAnswers/PossibleAnswer') as XD(XTbl)

我对文件的硬编码地址有同样的意义并且它有效,所以我想知道这是否可行,以便我可以使用不同的文件执行存储过程,如果我需要

1 个答案:

答案 0 :(得分:1)

这是一个示例过程,它接受XML作为字符串,并将其碎化为表。如果您根据表定义编写某种类型的生成器,那么手动编写它是非常粗糙的。

输入的xml非常简单,基于属性

<xml>
<agent addr1='123 Main Street' city='Toronto' />
<agent addr1='123 Main Street' city='Toronto' />
</xml>

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[sp_INSERT_agents]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure [dbo].[sp_INSERT_agents]
GO

CREATE PROCEDURE sp_INSERT_agents
     @strXML ntext
AS
BEGIN
    -- NOTE: This procedure was generated by WCG:ITX DB/XML mapping utility
    -- Please do not update this code by hand.
    DECLARE @RC int
    DECLARE @iDoc int
    DECLARE @dtcurrenttime datetime
    SET @dtcurrenttime = CURRENT_TIMESTAMP

    -- Field variables

    DECLARE @addr1 varchar(50)
    DECLARE @addr2 varchar(50)
    DECLARE @agentid char(13)
    DECLARE @city varchar(50)
    DECLARE @email varchar(50)
    DECLARE @fax varchar(25)
    DECLARE @mobile varchar(25)
    DECLARE @muli char(1)
    DECLARE @name varchar(50)
    DECLARE @notes varchar(500)
    DECLARE @phone varchar(25)
    DECLARE @state char(2)
    DECLARE @zip varchar(10)
    DECLARE @part char(1)
    EXECUTE sp_xml_preparedocument @iDoc OUTPUT, @strXML

    -- Create a temporary return table
    create table #return
    ( err varchar(50), agentid char(13))

    -- Set NOCOUNT ON, to allow data to be returned from the temporary table.
    SET NOCOUNT ON 


    DECLARE @ElementCursor  CURSOR
    SET @ElementCursor = CURSOR SCROLL DYNAMIC FOR 
    SELECT addr1, addr2, agentid, city, email, fax, mobile, muli, name, notes, phone, state, zip, part FROM OPENXML( @iDoc, "//agent", 2 ) 
        WITH( addr1 varchar(50) '@addr1', addr2 varchar(50) '@addr2', agentid char(13) '@dt', city varchar(50) '@city', email varchar(50) '@email', fax varchar(25) '@fax', mobile varchar(25) '@mobile', muli char(1) '@muli', name varchar(50) '@name', notes varchar(500) '@notes', phone varchar(25) '@phone', state char(2) '@state', zip varchar(10) '@zip', part char(1) '@part')

    OPEN @ElementCursor
    FETCH NEXT FROM @ElementCursor INTO @addr1, @addr2, @agentid, @city, @email, @fax, @mobile, @muli, @name, @notes, @phone, @state, @zip, @part

    BEGIN TRANSACTION

    WHILE @@FETCH_STATUS = 0
        BEGIN
        -- Convert any temp values to real date time values

        BEGIN
            INSERT into dbo.agents
            (addr1, addr2, agentid, city, email, fax, mobile, muli, name, notes, phone, state, zip, part, dtmodified)
            values
            (@addr1, @addr2, @agentid, @city, @email, @fax, @mobile, @muli, @name, @notes, @phone, @state, @zip, @part, @dtcurrenttime)
        END

        -- Check for any errors on the insert / update
        IF @@error <> 0
        BEGIN
            INSERT into #return
            (err)
      values
      (@@error)
        END

        FETCH NEXT FROM @ElementCursor INTO @addr1, @addr2, @agentid, @city, @email, @fax, @mobile, @muli, @name, @notes, @phone, @state, @zip, @part


    END



    COMMIT TRANSACTION


    CLOSE @ElementCursor
    DEALLOCATE @ElementCursor
    EXECUTE sp_xml_removedocument @iDoc

    -- Return the temporary data, containing any errors
    SELECT * from #return
END
GO