Solved

SQL Server Valid XML

Posted on 2013-06-10
2
433 Views
Last Modified: 2013-06-10
My following schema works and throws an error if an invalid schema is passed in

So if I use this
<root0MwljpDk4TE</root>

Open in new window


This error is thrown
Msg 6909, Level 16, State 1, Line 2
XML Validation: Text node is not allowed at this location, the type was defined with element only content or with simple content. Location: /*:root[1]

But I can't seem to get error handling to work.
If I use womething like this I don;t get the @@Error part returned. Just the initial XML Validation error
DECLARE @x XML(videoSchema)
        
SELECT  @x = '<root>0MwljpDk4TE=</root>'

IF @@error <> 0
   PRINT '@err is ' + ltrim(str(@@error)) + '.'

Open in new window



Heres my schema

CREATE XML SCHEMA COLLECTION videoSchema
    AS'<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:element name="root" >
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="encString" />
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>'

DECLARE @x XML(videoSchema)
SELECT @x = '<root><encString>0MwljpDk4TE=</encString></root>'

SELECT @x

Open in new window

0
Comment
Question by:lrbrister
2 Comments
 
LVL 32

Accepted Solution

by:
bhess1 earned 500 total points
ID: 39235835
I believe you need to move to the TRY... CATCH error handling for XML syntax errors.  These errors come from the XML engine, which is not perfectly integrated in SQL 2005.

Since all of your code should be in the TRY block, and all error handling in the CATCH block - and the @@error is not always correctly set in XML errors (as you discovered), I tend to work like this with XML coding in 2005

DECLARE @x XML(videoSchema)
DECLARE @stepFlag int

BEGIN TRY
    SET @stepFlag=1
    SELECT  @x = '<root0MwljpDk4TE=</root>'

    SET @stepFlag = 2
....

END TRY

BEGIN CATCH
    IF @stepFlag=1
    BEGIN
        PRINT 'XML Error at Step 1 - ' + @x
        PRINT '@errorvalue: ' + ltrim(str(@@error)) + '.'
    END

    IF @stepFlag=2
    BEGIN
         .....

Open in new window

0
 

Author Closing Comment

by:lrbrister
ID: 39235943
Got me started.

Thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now