XML转换问题

时间:2016-04-11 22:40:33

标签: sql sql-server xml

获得以下错误; “将数据类型varchar转换为数字时出错”。

尝试从XML输入中选择数据时会发生这种情况。以下是代码:

DECLARE @XmlIn XML = '<rec>
<targetId>10</targetId>
<categoryId>4</categoryId>
<percent>2</percent>
<AgreementDurationMin></AgreementDurationMin>
<AgreementDurationMax></AgreementDurationMax>
</rec>'

INSERT  INTO WizzTable
        ( CommissionTargetId ,
          CommissionPercentageCategoryId ,
          Percentage ,
          AgreementDurationMin ,
          AgreementDurationMax
        )
        SELECT  targetId = NULLIF(cb.n.value('(targetId)[1]', 'INT'),
                                  '') ,
                categoryId = NULLIF(cb.n.value('(categoryId)[1]',
                                               'SMALLINT'), '') ,
                percentage = NULLIF(cb.n.value('(percent)[1]',
                                               'DECIMAL(17,2)'), '') ,
                AgreementDurationMin = ISNULL(NULLIF(cb.n.value('(AgreementDurationMin)[1]',
                                                          'INT'), ''), 0) ,
                AgreementDurationMax = NULLIF(cb.n.value('(AgreementDurationMax)[1]',
                                                         'INT'), '')
        FROM    @XmlIn.nodes('rec') cb ( n )
        WHERE   cb.n.value('(percent/text())[1]', 'INT') > 0

有什么想法吗?谢谢!

2 个答案:

答案 0 :(得分:1)

您在此处设置空字符串

... = NULLIF(something,'')

空字符串在数字上下文中无效。 expression1和expression2的类型必须相同:http://i.imgur.com/v0sISnP.png

试试这样:

SELECT  targetId = NULLIF(cb.n.value('(targetId)[1]', 'INT'),
                            0) ,
        categoryId = NULLIF(cb.n.value('(categoryId)[1]',
                                        'SMALLINT'), 0) ,
        percentage = NULLIF(cb.n.value('(percent)[1]',
                                        'DECIMAL(17,2)'), 0) ,
        AgreementDurationMin = ISNULL(NULLIF(cb.n.value('(AgreementDurationMin)[1]',
                                                    'INT'), 0), 0) ,
        AgreementDurationMax = NULLIF(cb.n.value('(AgreementDurationMax)[1]',
                                                    'INT'), 0)
FROM    @XmlIn.nodes('rec') cb ( n )
WHERE   cb.n.value('(percent/text())[1]', 'INT') > 0

答案 1 :(得分:1)

元素类型的百分比是多少?你尝试在你的选择中将它转换为DECIMAL(17,2);但在你的where子句中,它是INT。

错误会引发您的Nullif默认值。 Sql无法转换为&#39;&#39;到十进制。

修正:

DECLARE @XmlIn XML = '<rec>
<targetId>10</targetId>
<categoryId>4</categoryId>
<percent>2</percent>
<AgreementDurationMin></AgreementDurationMin>
<AgreementDurationMax></AgreementDurationMax>
</rec>'

INSERT  INTO WizzTable
        ( CommissionTargetId ,
          CommissionPercentageCategoryId ,
          Percentage ,
          AgreementDurationMin ,
          AgreementDurationMax
        )
        SELECT  targetId = NULLIF(cb.n.value('(targetId)[1]', 'INT'),
                                  '') ,
                categoryId = NULLIF(cb.n.value('(categoryId)[1]',
                                               'SMALLINT'), '') ,
                percentage = NULLIF(cb.n.value('(percent)[1]',
                                               'DECIMAL(17,2)'), '0') ,
                AgreementDurationMin = ISNULL(NULLIF(cb.n.value('(AgreementDurationMin)[1]',
                                                          'INT'), ''), 0) ,
                AgreementDurationMax = NULLIF(cb.n.value('(AgreementDurationMax)[1]',
                                                         'INT'), '')
        FROM    @XmlIn.nodes('rec') cb ( n )
        WHERE   cb.n.value('(percent/text())[1]', 'DECIMAL(17,2)') > 0