SQL Server字符串或二进制数据将被截断varchar(max)

时间:2015-07-20 19:45:39

标签: sql-server xml

我已经多次看过这个问题了,但似乎都没有解决我的问题变种。

  

Msg 8152,Level 16,State 14,Line 150
  字符串或二进制数据将被截断。

从XML变量中提取数据时出现的错误。

XML结构(简化)如下所示:

<View>
    <view_config>
        <gl_type>Box</gl_type>
        <data_access>
            <access_as></access_as>
            <access_id></access_id>
            ...
        </data_access>
        <data>
            <dimension x="1" y="2" z="3" />
            <is_position_relative>false</is_position_relative>
        </data>
    </view_config>
    <view_config>
    ...
    </view_config>
</View>

在我的数据集中,唯一提供问题的XML有168个节点。该错误发生了27次,并且仅在使用以下代码解析该特定XML数据时:

declare @graphics table(
    rowNum INT IDENTITY(0,1)PRIMARY KEY CLUSTERED,
    graphicName VARCHAR(MAX),
    vcXML XML,
    numComponents int
)
declare @viewConfigs table(
    rowNum int identity(0,1)primary key clustered,
    graphicName varchar(max),
    viewIndex int,
    gl_type varchar(max),
    access_as varchar(10),
    ...
    dimX float, dimY float, dimZ float,
    posRelative varchar(max)
)

insert into @graphics(graphicName,vcXML)
(select graphic_name,viewcenter_config from ProjAuto_Graphics) order by graphic_name

declare @m int, @n int
declare @xml XML
set @m = 0
select @n = COUNT(*) from @graphics
while @m < @n
begin
    update @graphics
    set numComponents = (select max(vcXml.value('count(//view_config)','int'))
    from @graphics where rowNum = @m
    set @m = @m + 1
end
set @m = 0

select @xml = vcXML FROM @graphics where rowNum = @m

declare @graphicName varchar(30)
declare @glType varchar(30) --Increased these values while trying to debug.
declare @dimX float, @dimY float, @dimZ float
declare @rltv varchar(max)
--other variables needed to fill in the table

declare @data table(
    rowNum int identity(0,1)primary key clustered,
    dX float, dY float, dZ float,...,rltv varchar(max)
)--I had to make this table due to the nested structure of the XML

while @m < @n --This loops over each graphic in the table
begin
    select @xml = vcXML from @graphics where rowNum = @m
    INSERT INTO @data
    SELECT
        Tbl.Col.value('(dimension/@x)[1]','varchar(max)'),
        Tbl.Col.value('(dimension/@y)[1]','varchar(max)'),
        Tbl.Col.value('(dimension/@z)[1]','varchar(max)'),
        Tbl.Col.value('(is_position_relative)[1]','varchar(max)')
    FROM @xml.nodes('//data') Tbl(Col)

    declare @i int, @j int
    set @i = 0
    set @j = numComponents from @graphics where rowNum = @m
    while @i < @j
    begin
        --store values from @data into temp variables to make an insert statement
        select @dimX = dX from @data where rowNum = @i
        select @dimY = dY from @data where rowNum = @i
        select @dimZ = dZ from @data where rowNum = @i
    print @graphicName -- Debugging
        select @rltv = rltv from @data where rowNum = @i
        insert into @viewConfigs(graphicName,gl_type,...,rltv)
        values(@graphicName,@glType,...,@rltv)
        set @i = @i + 1
    end
    set @m = @m + 1
end

所以我已经浏览了我用于该节点的每个变量,并确保它们都设置为VARCHAR(MAX),甚至是xquery语法中使用的变量。我还测试了一个查询,以查看is_position_relativefalse以外是否有任何值,除了手动检查导致错误发生的特定图形的XML之外,还没有。{ / p>

最后,当我在SQL Server中查看结果时,我的表中没有NULL个值,更不用说任何非false值了。

什么可能导致此错误,以及错误发生的结果是什么(因为它似乎没有弄乱我的结果)?

0 个答案:

没有答案