Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 SQL SERVER 2005/2008 TRY CATCH & RAISERROR

Author  Topic 

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2009-07-30 : 10:03:24
i have tried to use try/catch/raiserror like in this example :
http://msdn.microsoft.com/en-us/library/ms178592.aspx
u used it beacuse i didnt want from my vb6 application to get errors on duplicate keys, but i wanted to get any other error.
the Problem is that the RaisError dosen't have the effect that if the exception that the sql server throws on "duplickate key" error for example.
if i dont use try/catch in the vb6 i will get the full error "Primary Key violation....."
but when i rap it with try/catch and make another error, and use the RaiseError i don't get the error back from the SQL SERVER
any idea why?


Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:)

jeremygiaco
Starting Member

14 Posts

Posted - 2009-07-30 : 10:12:30
What severity are you using? You can try something like this (I made a stored procedure, but you can just type in the code everywhere you want to use it..)


/*
CREATE PROCEDURE dbo.usp_PrimaryKeyErrorHandler
AS
BEGIN
DECLARE @Error_Number int, @Error_Severity int, @Error_State int,
@Error_Procedure varchar(1000), @Error_Line int, @Error_Message varchar(8000)
SELECT
@Error_Number = ERROR_NUMBER(),
@Error_Severity = ERROR_SEVERITY(),
@Error_State = ERROR_STATE(),
@Error_Procedure = ERROR_PROCEDURE(),
@Error_Line = ERROR_LINE(),
@Error_Message = ERROR_MESSAGE()

--Handle errors
IF @Error_Number = 2627
--Primary key violation, return error to client
BEGIN
RAISERROR(@Error_Message,@Error_Severity, @Error_State)
END
ELSE
--just return error to the app nicely if you want to do something with it
BEGIN
SELECT
ERROR_NUMBER() AS ErrorNumber,
ERROR_SEVERITY() AS ErrorSeverity,
ERROR_STATE() AS ErrorState,
ERROR_PROCEDURE() AS ErrorProcedure,
ERROR_LINE() AS ErrorLine,
ERROR_MESSAGE() AS ErrorMessage;
END
END
*/
SET NOCOUNT ON
BEGIN TRY
SELECT 1/0
END TRY
BEGIN CATCH
Exec usp_PrimaryKeyErrorHandler
END CATCH

BEGIN TRY
Create Table #Temp (id int primary key)
Insert Into #Temp (id) Values(1)
Insert Into #Temp (id) Values(1)
END TRY
BEGIN CATCH
Exec usp_PrimaryKeyErrorHandler
Drop Table #Temp
END CATCH
Go to Top of Page

pelegk2
Aged Yak Warrior

723 Posts

Posted - 2009-07-30 : 10:27:19
this is the code, an i do raise error 16

DECLARE @Err int ,@msg_id bigint
DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;



BEGIN TRY
INSERT vw_Billing WITH(ROWLOCK)......
VALUES .....
END TRY
BEGIN CATCH
SET @Err = @@ERROR
SELECT @ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
SET @msg_id = 50000+@Err
END CATCH
IF(@Err=0)
BEGIN
RETURN 1
END
ELSE
BEGIN
IF @Err = 2767 RETURN 0 --duplicate Key Error
print @ErrorSeverity --=>16
print @ErrorState--=>1
RAISERROR(@msg_id,@ErrorSeverity,@ErrorState)
--SET @@ERROR =
RETURN 0
END


Israel -the best place to live in aftr heaven 9but no one wan't to go there so fast -:)
Go to Top of Page
   

- Advertisement -