0

I am reading XML data using OPENXML in a stored procedure in SQL Server 2012.

My XML:

<name>
    <name>Temp1</name>
    <data>
        <mealID>5</mealID>
        <food>
            <foodID>11</foodID>
        </food>
        <food>
            <foodID>12</foodID>
        </food>
    </data>
    <data>
        <mealID>6</mealID>
        <food>
           <foodID>13</foodID>
        </food>
    </data>
</name>

My stored procedure:

ALTER PROCEDURE [dbo].[sp_Insert_EatingProgramTemplate] 
    @Template xml,
    @intOutSuccess nvarchar(10) OUTPUT,
    @ErrorSeverity nvarchar(MAX) OUTPUT,
    @ErrorMesg nvarchar(MAX) OUTPUT
AS
BEGIN
    DECLARE @idoc int

    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SET @ErrorSeverity = ''
    SET @ErrorMesg = ''
    SET @intOutSuccess = ''

    BEGIN TRY
        SELECT *
         FROM OPENXML (@idoc, '/ROOT/Customer',1)
         WITH (CustomerID  varchar(10),
               ContactName varchar(20))
        SET @intOutSuccess = 1
    END TRY
    BEGIN CATCH
        SET @ErrorSeverity = ERROR_SEVERITY()  
        SET @ErrorMesg = ERROR_MESSAGE() 
        SET @intOutSuccess = -1
        GOTO A
    END CATCH
A:
    END
GO

While executing this procedure I am getting this error:

Could not find prepared statement with handle 0

Any help, why I am getting this error?

5
  • Well, did you do any debugging? Like do you expect a certain value for @idoc? Did you output it to see if it contained the value you expected? Also, did you consider just querying the XML directly instead of using OPENXML, sp_xml_preparedocument, etc.? Commented Apr 30, 2015 at 13:00
  • yes i tried it with XML directly but getting error. I did not do any debugging for this Commented Apr 30, 2015 at 13:02
  • @Twix your OPENXML query completely unrelated to the XML structure you have. What values do you actually want to get from that XML sample? Anyway, I'd suggest to look into nodes() XML function instead Commented Apr 30, 2015 at 13:41
  • 3
    Just curious, what is the point of putting a goto in there? Also, you shouldn't use the sp_ prefix for procedure names. Here is a link to an article by @AaronBertrand sqlperformance.com/2012/10/t-sql-queries/sp_prefix Commented Apr 30, 2015 at 13:42
  • @har07 I know I have provided different select statement but that is what I tried on different XML and I could not relate it with my XML and don't know how to use it Commented May 1, 2015 at 4:44

1 Answer 1

2

You missed to set @idoc by calling sp_xml_preparedocument, for example :

declare @template xml = '<name>
    <name>Temp1</name>
    <data>
        <mealID>5</mealID>
        <food>
            <foodID>11</foodID>
        </food>
        <food>
            <foodID>12</foodID>
        </food>
    </data>
    <data>
        <mealID>6</mealID>
        <food>
           <foodID>13</foodID>
        </food>
    </data>
</name>'
DECLARE @idoc int
EXEC sp_xml_preparedocument @idoc OUTPUT, @template; 

SELECT *
     FROM OPENXML (@idoc, '/name/data/food/foodID',1)
     WITH (FoodID  INT '.',
           MealID INT '../../mealID')

--clear parsed XML document from SQL Server cache
EXEC sp_xml_removedocument @idoc; 

You can also achieve the same output by querying the xml variable directly :

SELECT 
    x.value('.', 'int') as FoodID
    , x.value('../../mealID[1]', 'int') as MealID
FROM @template.nodes('/name/data/food/foodID') as T(X)
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't you also apply sp_xml_removedocument if you use sp_xml_preparedocument?
@AndriyM thanks for the hints, I didn't know initially, but MSDN suggests that I should

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.