存储过程值不插入XML节点

时间:2016-10-25 12:42:14

标签: sql sql-server xml

存储过程:

ALTER proc [dbo].[Equip_SaveTeamTaskAssigineeDetails] (
@saveDataXML xml)
as 
begin

    Declare @claimid int,
            @createdate datetime,
            @duedate datetime,
            @startdate datetime,
            @assignmentid int,
            @assignedto varchar(50),
            @piority varchar(50),
            @comment varchar(4000)



    select  @claimid = node.value('claimid[1]','int'),
            @createdate = node.value('createdate[1]','datetime'),
            @duedate = node.value('duedate[1]','datetime'),
            @startdate = node.value('startdate[1]','datetime'),
            @assignmentid = node.value('assignmentid[1]','int'),
            @assignedto = node.value('assignedto[1]','varchar(50)'),
            @piority= node.value('piority[1]','varchar(50)'),
            @comment = node.value('comment[1]','varchar(4000)')


        from @saveDataXML.nodes('/TeamTaskDetails/Task') nodes(node)  

IF (@claimid = 0)
        BEGIN
        INSERT INTO  [dbo].[TeamTaskAssiginment]
               ([claimid],
                [createdate],
                [duedate],
                [startdate],
                [assignedid],
                [assiginedto],
                [priority],
                [comment])




               VALUES
                (@claimid,
            @createdate,
            @duedate,
            @startdate ,
            @assignmentid ,
            @assignedto ,
            @piority ,
            @comment
                )

        --SET @claimid = SCOPE_IDENTITY()
        END
    ELSE
        BEGIN
            Update [dbo].[TeamTaskAssiginment] set


            claimid=@claimid ,
                createdate=@createdate,
                duedate=@duedate,
                startdate=@startdate,
                assignedid=@assignedto,
                assiginedto=@assignedto,
                priority=@piority,
                comment=@comment

        END

Execute Stored Procedure 

Exec Equip_SaveTeamTaskAssigineeDetails @saveDataXML =
<TeamTaskDetails>
<Task claimid="1" createdate="2000-12-16" duedate="2000-12-16" startdate="2000-12-16" assignmentid="1" assignedto="test"  piority="HIGH" comment="test"  />
</TeamTaskDetails>

如果我执行存储过程,则显示0行受影响的值不插入

请帮助解决问题。

2 个答案:

答案 0 :(得分:1)

您查询xml错误,获取您需要查询的属性值,替换claimid[1]

SELECT  @claimid = node.value('@claimid', 'int') ,....

完整样本:

DECLARE @saveDataXML AS XML = 
    '<TeamTaskDetails>
        <Task claimid="1" createdate="2000-12-16" duedate="2000-12-16" 
              startdate="2000-12-16" assignmentid="1" assignedto="test"  
              piority="HIGH" comment="test"  />
    </TeamTaskDetails>'

DECLARE @claimid INT ,
    @createdate DATETIME ,
    @duedate DATETIME ,
    @startdate DATETIME ,
    @assignmentid INT ,
    @assignedto VARCHAR(50) ,
    @piority VARCHAR(50) ,
    @comment VARCHAR(4000)



SELECT  @claimid = node.value('@claimid', 'int') ,
        @createdate = node.value('@createdate', 'datetime') ,
        @duedate = node.value('@duedate', 'datetime') ,
        @startdate = node.value('@startdate', 'datetime') ,
        @assignmentid = node.value('@assignmentid', 'int') ,
        @assignedto = node.value('@assignedto', 'varchar(50)') ,
        @piority = node.value('@piority', 'varchar(50)') ,
        @comment = node.value('@comment', 'varchar(4000)')
FROM    @saveDataXML.nodes('/TeamTaskDetails/Task') nodes ( node )  


SELECT  claimid = @claimid ,
        createdate = @createdate ,
        duedate = @duedate ,
        startdate = @startdate ,
        assignedid = @assignedto ,
        assiginedto = @assignedto ,
        priority = @piority ,
        comment = @comment

输出:

claimid createdate  duedate     startdate   assignedid....
1       2000-12-16  2000-12-16  2000-12-16  test...

另外,正如我评论的那样,插入没有被命中,因为当前的选择返回空值,因此IF (@claimid = 0)不会返回true。除非您指定where子句,否则您的更新查询将更新每一行。

答案 1 :(得分:0)

BEGIN声明没有END。 请尝试此代码

ALTER proc [dbo].[Equip_SaveTeamTaskAssigineeDetails] (
@saveDataXML xml)
AS
BEGIN

    Declare @claimid int,
            @createdate datetime,
            @duedate datetime,
            @startdate datetime,
            @assignmentid int,
            @assignedto varchar(50),
            @piority varchar(50),
            @comment varchar(4000)



    select  @claimid = node.value('claimid[1]','int'),
            @createdate = node.value('createdate[1]','datetime'),
            @duedate = node.value('duedate[1]','datetime'),
            @startdate = node.value('startdate[1]','datetime'),
            @assignmentid = node.value('assignmentid[1]','int'),
            @assignedto = node.value('assignedto[1]','varchar(50)'),
            @piority= node.value('piority[1]','varchar(50)'),
            @comment = node.value('comment[1]','varchar(4000)')


        from @saveDataXML.nodes('/TeamTaskDetails/Task') nodes(node)  

    IF (@claimid = 0) BEGIN
        INSERT INTO  [dbo].[TeamTaskAssiginment]
               ([claimid],
                [createdate],
                [duedate],
                [startdate],
                [assignedid],
                [assiginedto],
                [priority],
                [comment])




               VALUES
                (@claimid,
            @createdate,
            @duedate,
            @startdate ,
            @assignmentid ,
            @assignedto ,
            @piority ,
            @comment
                )

        --SET @claimid = SCOPE_IDENTITY()
     END ELSE BEGIN
            Update [dbo].[TeamTaskAssiginment] set


            claimid=@claimid ,
                createdate=@createdate,
                duedate=@duedate,
                startdate=@startdate,
                assignedid=@assignedto,
                assiginedto=@assignedto,
                priority=@piority,
                comment=@comment

      END
END


GO

同时检查XML代码。它应该是一个倒置的逗号:

--  Execute Stored Procedure    
Exec Equip_SaveTeamTaskAssigineeDetails @saveDataXML =
'<TeamTaskDetails>
<Task claimid="1" createdate="2000-12-16" duedate="2000-12-16" startdate="2000-12-16" assignmentid="1" assignedto="test"  piority="HIGH" comment="test"  />
</TeamTaskDetails>'