rc
asked on
help with Update query -- t-sql
Hello Experts!
I am trying to capture an error message if the below transaction fails. In the CATCH block, I am setting the error message to @errormessage variable, but this will not work.
How can I capture that particular record for which the update failed?
Thanks in advance!!!!
I am trying to capture an error message if the below transaction fails. In the CATCH block, I am setting the error message to @errormessage variable, but this will not work.
How can I capture that particular record for which the update failed?
declare @ErrorMessage varchar(4000) = ''
BEGIN TRAN trn1
BEGIN TRY
update p
set p.Professional_Id = s.Destination_Professional_Id
from tblPhys p, dbo.Prof s
where p.Professional_Id = s.Source_Professional_Id
and s.Source_Professional_Id IS NOT NULL
and EXISTS (SELECT * FROM dbo.tblPhys AS tp WHERE tp.Professional_Id = s.Destination_Professional_Id AND tp.PhysicianId = p.PhysicianId)
COMMIT TRAN trn1
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
ROLLBACK TRAN trn1
Set @ErrorMessage = @ErrorMessage + ' Update Failed for ProfesstionalId from S: ' + convert(varchar(20), s.Destination_Professional_Id) + Error_message() + ' failed at trn1'
END CATCH
Thanks in advance!!!!
ASKER
So, you mean I have to do 2 updates instead of 1. one for capturing the value and the other for performing the actual update ??
Inside the catch block use this
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;
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;
ASKER
Aneesh,
But this will not give me the record that made the update fail, right?
But this will not give me the record that made the update fail, right?
>So, you mean I have to do 2 updates instead of 1. one for capturing the value and the other for performing the actual update ??
One select to capture the PK, then your update.
That way the PK is in a variable if needed in the CATCH block to display in a custom error message.
Unless one of my peers here can demonstrate this is possible in 1 statement, afraid so.
One select to capture the PK, then your update.
That way the PK is in a variable if needed in the CATCH block to display in a custom error message.
Unless one of my peers here can demonstrate this is possible in 1 statement, afraid so.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Jim,
I never worked on a cursor before, could you give me a sample for the update script I have attached above?
I never worked on a cursor before, could you give me a sample for the update script I have attached above?
>could you give me a sample for the update script I have attached above?
See http://msdn.microsoft.com/ en-us/libr ary/ms1801 69.aspx
Warning: Not easy to learn.
See http://msdn.microsoft.com/
Warning: Not easy to learn.
> But this will not give me the record that made the update fail, right?
thats correct, but you will get the error ...
thats correct, but you will get the error ...
I am trying to capture an error message if the below transaction fails
So why don't we start with what error are you are trying to capture caused by bad data? And please don't say any error. Is is a Primary or Foreign Key or Check CONSTRAINT error? What other errors are you trying to capture?
So why don't we start with what error are you are trying to capture caused by bad data? And please don't say any error. Is is a Primary or Foreign Key or Check CONSTRAINT error? What other errors are you trying to capture?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
>How can I capture that particular record for which the update failed?
afaik the only way to pull this off would be to select the ID of the record, save it in a variable, then perform your UPDATE.