Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

RaiseError in TRY/Catch block after XML Schema Collection Validation. Expand / Collapse
Author
Message
Posted Friday, April 3, 2009 9:40 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 9:15 PM
Points: 112, Visits: 319
I am trying to Raise an error using RAISEERROR function in sql when the XML schema validation fails.
I am doing this inside a store proc. I am declaring the XML doc binding it to it's XSD schema in the TRY block and if validation fails, raise a user define error number by using RAISEERROR function in the catch block. However, somehow i am not able to do this and sql return it's own error the moment it executes that line of code. So using Employee example here, I am doing something like this.

CREATE PROC ImportEmployeeInfo
@EmployeeXML as XML
AS

BEGIN

BEGIN TRY

DECLARE @EmpXML AS XML(EmployeeSchema) --declaring a local XML variable and binding it to its XSD Schema Collection
DECLARE @LocalError

SET @EmpXML = @EmployeeXML -- here I am setting the @EmpXML to @EmployeeXML variable passed in
IF @@error<>0 --if the @EmployeeXML failed the XSD validation, i beileve there would be an error right however this line never gets executed
BEGIN
SET @LocalError = 50001 --user defined error
RaiseError(' The input parameter @EmployeeXML is not valid', 16, 1)
END

--The XML shredding and import into table goes here

END TRY

BEGIN CATCH

--I have another another user defined errorhandler proc here which will take the above @LocalError
--as input parameter and raise a detailed error

END CATCH

END --End of Proc
Post #690049
Posted Friday, April 3, 2009 10:20 AM
Mr or Mrs. 500

Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500Mr or Mrs. 500

Group: General Forum Members
Last Login: Today @ 7:44 AM
Points: 522, Visits: 2,879
Are you spelling the "raise error" correctly? It's "Raiserror", not "RaiseError".


Post #690071
Posted Friday, April 3, 2009 4:57 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 9:15 PM
Points: 112, Visits: 319
Matt,

I tried by putting the XML schema validation within a nested Try/Catch in the proc and now it's able to capture the LocalError passsed in.
Thanks for you feedback.
Post #690304
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse