how catch error message etc "Error converting data type varchar to datetime"
and this error put in Select output
BEGIN
SET NOCOUNT ON;
begin try
UPDATE projects
SET
projectUser = #projectUser,
projectStartDate = #projectStartDate
where projectId = #projectId
SELECT 'OK'
end try
begin catch
SELECT 'ERROR - msg error'
end catch
COMMIT
END
I don't need ==> https://postimg.org/image/u8mohtl9b/
Any solution?
The issue you are having is that the error is not occuring in the procedure, it is occuring when calling the procedure. In a simple example, create the following procedure:
IF OBJECT_ID(N'dbo.ErrorCatchTest', 'P') IS NOT NULL
DROP PROCEDURE dbo.ErrorCatchTest;
GO
CREATE PROCEDURE dbo.ErrorCatchTest #int INT
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
DECLARE #T TABLE (I INT);
INSERT #T (I) VALUES (5.0 / #int);
SELECT 'OK';
END TRY
BEGIN CATCH
SELECT CONCAT('Error_Message: ', ERROR_MESSAGE());
END CATCH
END;
If I pass a valid INT to the procedure:
EXECUTE dbo.ErrorCatchTest #int = 1;
The I get OK as required.
If I a pass 0 to force an error:
EXECUTE dbo.ErrorCatchTest #int = 0;
You get the error message as required, but if I try an pass an invalid integer:
EXECUTE dbo.ErrorCatchTest #int = 'Not a number';
Then I get the error message, because the error is not within the proceudure, but while doing an implicit convert on the parameters.
The way around this is to call the procedure within a try/catch block:
BEGIN TRY
EXECUTE dbo.ErrorCatchTest #int = 'Not a number';
END TRY
BEGIN CATCH
SELECT CONCAT('Error_Message: ', ERROR_MESSAGE());
END CATCH
You can select the error message like below...
Begin Catch
SELECT 'Error_Message: ' + ERROR_MESSAGE() + ' Error_Line: ' + ERROR_LINE() AS ErrorMessage
End Catch
If you need only error message,use the below script.
BEGIN CATCH
SELECT 'Error_Message :'+ERROR_MESSAGE()
END CATCH
sample output :
Related
I am using Microsoft Sql I have the following code snippet of a much larger stored procedure.
if #ErrorValue = 0
begin
begin try
INSERT INTO MyItems (GroupID, OrderID, MajorNum)
VALUES (#GroupID, #OrderID,#MajorNum)
SELECT #RecordsAffected = ##RowCount, #ErrorValue = ##Error
if #GroupID is null
set #ResultMessage = 'New In Cell Record'
else
set #ResultMessage = 'New GroupID:' + convert(varchar,#GroupID)
end try
begin catch
set #ResultMessage = ERROR_MESSAGE ()
set #ErrorValue = ERROR_NUMBER()
end catch
end
else
begin
...
end
In keeping the lines of code down I experemented with removing the begin and end from the first block as I was using a try catch. It works like the first version.
Is there any problem with doing this?
if #ErrorValue = 0
begin try
INSERT INTO MyItems (GroupID, OrderID, MajorNum)
VALUES (#GroupID, #OrderID,#MajorNum)
SELECT #RecordsAffected = ##RowCount, #ErrorValue = ##Error
if #GroupID is null
set #ResultMessage = 'New In Cell Record'
else
set #ResultMessage = 'New GroupID:' + convert(varchar,#GroupID)
end try
begin catch
set #ResultMessage = ERROR_MESSAGE ()
set #ErrorValue = ERROR_NUMBER()
end catch
else
begin
...
end
Thanks.
The BEGIN in the first block is for the IF statement. If you don't have a BEGIN in IF block, only the first statement following IF will be executed inside IF block. Next set of statements will execute outside IF. Since
BEGIN TRY
...
END TRY
BEGIN CATCH
...
END CATCH
is inside IF and is considered as one BLOCK, you didn't see any difference in execution
we have a below procedure to update/insert a table. I have added exception handling in stored procedure as below.
CREATE PROCEDURE USP_UPDATESP
#Workstationlist worktable READONLY
AS
BEGIN
SET NOCOUNT ON
DECLARE #rerror As int
SET #rerror = 0
BEGIN TRY
BEGIN TRAN
MERGE [dbo].WORKTABLE AS [ofc]
USING #Workstationlist AS [Source] ON [ofc].officeid = [Source].id
WHEN MATCHED THEN
UPDATE
SET NumWorkStations = [Source].wsno,
ModifiedBy = [Source].modifiedby,
ModifiedByUsername = [Source].modifieduser,
ModifiedDate = GETDATE()
WHEN NOT MATCHED THEN
INSERT ( officeid, NumWorkStations, ModifiedBy, ModifiedByUsername, ModifiedDate )
VALUES ([Source].ID,[Source].wsno, [Source].modifiedby, [Source].modifieduser,GETDATE() );
SET #rerror = #rerror + ##error
If #rerror = 0
BEGIN
COMMIT TRAN
END
END TRY
BEGIN CATCH
SELECT #rerror AS ErrNum
ROLLBACK TRAN
End Catch
SET NOCOUNT off
END
GO
When I execute the procedure with an exception (passing null to id column) as below
declare #Workstationlist worktable
insert into #Workstationlist VALUES ( NULL,500,106720,106720)
EXEC USP_UPDATESP #Workstationlist
I got #error as 0 Always . Is there any problem this way of error handling?
to me it looks like you are mixing TRY...CATCH error handling with old style error handling.
the code:
SET #rerror = #rerror + ##error
cannot be reached because when an exception occurs the control is passed to the catch block so the #rerror variable will always be 0, the value initially set.
in the catch block you should leverage the proper structures/objects to access error information and drop all the old way completely.
something like this:
BEGIN CATCH;
DECLARE #ErrSev INT,
#ErrMsg NVARCHAR(MAX),
#ErrState INT;
SELECT #ErrSev = ERROR_SEVERITY(),
#ErrState = ERROR_STATE(),
#ErrMsg = isnull(ERROR_PROCEDURE(), '(unknown procedure)') + ': ' + isnull(ERROR_MESSAGE(), '(unknown message)');
RAISERROR(#ErrMsg, #ErrSev, #ErrState);
END CATCH;
Catch the error in proper way.
BEGIN TRY
BEGIN TRANSACTION;
COMMIT TRANSACTION;
SELECT 'Success' AS Result
END TRY
BEGIN CATCH
ROLLBACK TRANSACTION;
SELECT 'Failed' AS Result
,ERROR_NUMBER() AS ErrorNumber
,ERROR_MESSAGE() AS ErrorMessage
END CATCH;
END
This is what I have so far:
DECLARE #localParam as int = 123
DECLARE #localReturnValue int
DECLARE #localerrornumber int
EXEC ('
DECLARE #return_value int
BEGIN TRY
EXEC #remoteReturnValue = [Database].[Proc]
#param = #localParam
END TRY
BEGIN CATCH
print ERROR_NUMBER()
END CATCH
') AT [LINKEDSERVERNAME]
I want to pass #localParam to the remote procedure, set #localReturnValue = #remoteReturnValue and #localerrornumber = ERROR_NUMBER()
When [LINKEDSERVERNAME].[Database].[Proc] raises an error, the error number is always less than 20, so I can't just try/catch it normally from my own server
Begin Try
exec #sql
End Try
Begin Catch
Display error message (How to?)
return
End Catch
Now, on error with the exec #sql statement, I want it to display the error message first and then end the code.
declare #sql varchar(100) = 'Select 1/0'
Begin Try
exec(#sql)
End Try
Begin Catch
print 'error='+ERROR_MESSAGE()
return
End Catch
To get the error message generated by system
declare #sql varchar(100) = 'Select 1/0'
Begin Try
exec(#sql)
End Try
Begin Catch
print ERROR_MESSAGE()
return
I have a stored procedure which can return a error this example its going to return error always
create proc regresaerror
as
begin
raiserror 51001 'Error al borrar los precios especiales'
end
declare #error varchar(max)
set #error=''
begin try
set #error=exec regresaerror
end try
begin catch
end catch
I tried it (because I need insert this error in a temporary table) but that block code get this error
Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'exec'.
Stored procedure couldn't be updated for add a output variable
Then how can I catch the error?
1st solution:
BEGIN TRY
EXECUTE regresaerror
END TRY
BEGIN CATCH
SELECT
ERROR_NUMBER() AS ErrorNumber
,ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
Here is link to MSDN.
2nd solution:
create proc regresaerror
(
errmsg varchar(max) out
)
as
begin
set errmsg = 'Error al borrar los precios especiales'
return 51001 -- if error
end
declare #error varchar(max)
declare #numerror int
set #error=''
exec #numerror = regresaerror #error out