PRB: Access Violation Occurs with the Use of OpenXML in a User-Defined Function (279078)



The information in this article applies to:

  • Microsoft SQL Server 2000 (all editions)

This article was previously published under Q279078

SYMPTOMS

SQL Server 2000 reports an Access Violation if a user-defined function (UDF) contains a call to the OpenXML function. The following error message is reported:
ODBC: Msg 0, Level 19, State 1
SqlDumpExceptionHandler: Process 51 generated fatal exception c0000005
EXCEPTION_ACCESS_VIOLATION. SQL Server is terminating this process.

CAUSE

This error message is the result of a current limitation in SQL Server 2000 that prevents the intermediate rowset from OpenXML from being streamed as input to another SQL command.

RESOLUTION

To work around the problem, you can insert the intermediate rowset from the OpenXML call into a temporary table.

This resolution is illustrated in more detail in the "More Information" section of this article.

MORE INFORMATION

Steps to Reproduce Behavior

  1. Create a user-defined function, as follows:
    CREATE FUNCTION dbo.udfTestOPENXML
       (@iDocID INT, @sXPath NVARCHAR(100))
    RETURNS TABLE
    AS
    RETURN 
      SELECT *
      FROM OPENXML (@iDocID, @sXPath)
        WITH (
          Value1  VARCHAR(10),
          Value2  VARCHAR(10)
        )
    GO
    					
    NOTE: The following steps 2 through 4 must be executed in a single batch rather than individually.

  2. Declare an XML document variable and a text variable to hold the XML text:
    DECLARE @hXMLInsertDoc int, @hXMLInsertDocText varchar(1000)
    
    SET @hXMLInsertDocText ='<Root><test Value1="test1" Value2="test2">This is a test</test></Root>'
    					
  3. Prepare the XML document from the text:
    EXEC sp_xml_preparedocument @hXMLInsertDoc OUTPUT, @hXMLInsertDocText
    					
  4. Execute the user-defined function with the prepared document:
    SELECT * FROM dbo.udfTestOPENXML(@hXMLInsertDoc,'/Root/test')
    						
    NOTE: This is where the error occurs.

Workaround

You must define the user-defined function to insert the intermediate rowset into a temporary table, as follows:
CREATE FUNCTION dbo.udfTestOPENXML
   (@iDocID INT, @sXPath VARCHAR(8000))
RETURNS @tbl
    TABLE (
           Value1 VARCHAR(10),
           Value2 VARCHAR(10)
           )
AS
BEGIN
 
   INSERT @tbl
   SELECT *
   FROM OPENXML (@iDocID, @sXPath)
     WITH (
       Value1  VARCHAR(10),
       Value2  VARCHAR(10)
     )

   RETURN
END
				

Modification Type:MajorLast Reviewed:10/3/2003
Keywords:kbprb KB279078