SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


RAISERROR in TRY/Catch block after XML Schema Collection Validation.


RAISERROR in TRY/Catch block after XML Schema Collection Validation.

Author
Message
bdba
bdba
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 338
Posted this earlier under SQL 7, 2000 T-SQL Section. Have deleted that one and reposting under SQL 2005.
I am trying to Raise an error using RAISERROR 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 RAISERROR 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
RAISERROR(' 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
Matt Miller (4)
Matt Miller (4)
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12735 Visits: 18584
The error handling and the error catching in SQL 2005 can be tricky. Depending on the erro you might be facing, it may be "batch" or "thread" terminating, meaning the inner context cannot catch anything, since it's been terminated.

The TRY...CATCH routines inside the stored proc can catch a lot of the minor non terminating errors. If you want to catch more errors than that - try wrapping your CALL to the stored proc in a TRY....CATCH. That way you can g etall the detail you need.

----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
bdba
bdba
SSC-Enthusiastic
SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)SSC-Enthusiastic (174 reputation)

Group: General Forum Members
Points: 174 Visits: 338
Hit Matt,

Thanks for you feedback.
Actually yes, I am already doing that in my Unit Test for the from as shown below, but still no luck

--Set the values for the Input variables
DECLARE @EmpXML XML = 'Test',
@ErrorMsg varchar(200)

BEGIN TRY

EXEC @rc = dbo.ImportEmployeeInfo
@EmployeeXML = @EmpXML

END TRY

BEGIN CATCH
SELECT @Error = ERROR_MESSAGE()
END CATCH

SELECT @Error AS Error
The Dixie Flatline
The Dixie Flatline
SSCertifiable
SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)SSCertifiable (5.6K reputation)

Group: General Forum Members
Points: 5632 Visits: 6900
How about this approach?


DECLARE @EmpXML AS XML(EmployeeSchema)
DECLARE @LocalError -- as what?

BEGIN TRY
SET @EmpXML = @EmployeeXML -- TRY to set the @EmpXML to @EmployeeXML
END Try
BEGIN CATCH
-- If the attempt fails, then
SET @LocalError = 50001 --user defined error
RAISERROR(' The input parameter @EmployeeXML is not valid', 16, 1)
END



__________________________________________________

Against stupidity the gods themselves contend in vain. -- Friedrich Schiller
Stop, children, what's that sound? Everybody look what's going down. -- Stephen Stills
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search