Error: "Index was outside the bounds of the array." in Sql Mng Studio


  • Sql Server 2008 Standard:

    To isolate the problem I'm having, I've created a simple Dbs with just one
    table, having an "int" field and an "xml" field.

    The xml field is assigned to a SchemaCollection, so it is typed.
    Updating the xml field with XML data works 99.99 of the times, but I have 2 specific XML fragments that systematically give me problems. Updating the xml field in the table with this data works fine without errors, however when I do a SELECT * from the table in the Management Studio, I get the error:

    "An error occurred while executing batch. Error message is: Index was outside the bounds of the array."

    Changing "SELECT xmlField FROM tab" to "SELECT CAST(xmlField AS nvarchar(MAX)) FROM tab" appears to give me the correct xml string, so the data is there!

    Querying the data from .Net code gives the same error. Here's part of the stack trace:

    [IndexOutOfRangeException: Index was outside the bounds of the array.]
    System.Xml.XmlSqlBinaryReader.GetXsdKatmaiTokenLength(BinXmlToken token) +1207761
    System.Xml.XmlSqlBinaryReader.ScanOverAnyValue(BinXmlToken token, Boolean attr, Boolean checkChars) +524
    System.Xml.XmlSqlBinaryReader.ScanOverValue(BinXmlToken token, Boolean attr, Boolean checkChars) +159
    System.Xml.XmlSqlBinaryReader.ImplReadData(BinXmlToken tokenType) +119
    System.Xml.XmlSqlBinaryReader.ReadDoc() +589
    System.Xml.XmlSqlBinaryReader.Read() +201

    I get this exact problem on different machines: both the production Server 2003 and my Vista development PC.

    The funny thing is that if I remove a few things from the XML, and re-update, the error goes away, but there's no structure in what I have to remove. As if the XML content is triggering some rare bug in the Sql server code?

    I've been successfully using XML fields in SQL2005 for 3 years now, and have recently upgraded to SQL2008.

    This has me completely puzzled.
    Is there any known problem that might be related or anyone has a clue how to fix this? Would be warmly appreciated.

    Thursday, October 16, 2008 5:41 PM


All replies

  • OK, I have managed to create a "repro" script.

    Just download the following:

    Contains only 1 file: SqlRepro.sql
    Run this script on any SQL Server 2008 (I use the Standard version).

    What the script does:
    1. Create a new database: "FdmSqlRepro" (drops it first if already exists)
    2. Create a schema collection: "MySchemaCollection"
    3. Create a table: "TestTable", with a numeric "idx" column and a typed xml column "xData". The xml column is bound to the created "MySchemaCollection"
    4. Insert 1 record in the table
    5. Query the table (SELECT * FROM TestTable)

    That's all!

    The last step (query *) gives the error:
    "Index was outside the bounds of the array."

    Can anyone run this script and confirm they get the same error?
    And then of course: why ... ?

    Many thanks,
    Hopefully my question is a little more documented now.

    Monday, October 20, 2008 3:22 PM
  • Hi:


        I do a testing,it worked fine on sql server 2005 Std,and failed on sql server 2008 Std.

    Monday, October 20, 2008 3:39 PM
  • Consider logging a bug on SQL Connect:




    Although check if something similar hasn't already been posted.

    Monday, October 20, 2008 4:31 PM
  • Thanks, I have done that. It's now 2 weeks ago but it seems that will not exactly be the fast track to a solution. Seems like the report will be sitting there with no feedback from Microsoft.

    Any other actions I could take to get at least some acknowledgment from Microsoft?

    This bug (?) is a bit like a time-bomb for our application: any newly added record could induce the error again and prevent running queries on the table until the record is removed.

    Monday, November 03, 2008 3:24 PM
  • I have installed service pack 1 of SQL Server 2008 and still i got this error. Does someone know the status of this thing/ bug?
    Sunday, November 08, 2009 8:18 PM