I created a small logging framework so I can easily call a stored procedure like spLog or spLogError.
The stored procedures and tables all reside in a separate database called Logging.
Now the code that I use looks as follows:
if OBJECT_ID('dbo.testLogProcLinkedServer') is not null
drop procedure dbo.testLogProcLinkedServer;
go
CREATE PROCEDURE [dbo].[testLogProcLinkedServer]
AS
BEGIN
set nocount on;
declare @msg nvarchar(max),
@dbId int = db_id();
begin try
declare @i int;
set @i = 1 / 0;
end try
begin catch
exec [MyLinkedServer].[Logging].dbo.spLogError @objectId = @@PROCID,
@databaseId = @dbId,
@message = N'Testing catch errors, check field ErrorMessage for more information.';
end catch
end
go
exec dbo.testLogProcLinkedServer;
Now the thing is that ERROR_MESSAGE() does not get into the spLogError, it is NULL. But when I remove MyLinkedServer (on my development environment, the databases are on the same server so there I am able to do this), the ERROR_MESSAGE() is available and all is working as intended.
When looking up "tsql catch linked server" on Google. I find this SO question: Catching Errors through a linked server with severity < 20
But the main difference with this question and mine, is that the most topics are refering to catching an error from a remote procedure call, but I don't want to catch something from a remote procedure call. I would like to send it my error data and write the corresponding table.
Can anyone point me to what is causing this?