Question

I've created a procedure on a MySQL server which will always throw an error due to a primary key violation, this would be called from a Microsoft SQL Server over a linked server using this:

EXEC ('CALL temp_test;') AT LINKSVR;

When this is called i get this full message:

OLE DB provider "MSDASQL" for linked server "LINKSVR" returned message "[MySQL][ODBC 5.1 Driver][###########]Duplicate entry '1' for key 'PRIMARY'".
Msg 7215, Level 17, State 1, Line 1
Could not execute statement on remote server 'LINKSVR'.

Now what i want to do it put this in a TRY...CATCH...and output the error to a table, however when I use ERROR_MESSAGE() all I get us the last line

is there a way to get the linked server message that was returned?

Was it helpful?

Solution

Ok, got a good work around, it doesn't output to a table but i'm sure you can log it somewhere

effectively you do this

BEGIN TRY
    EXEC ('CALL sp_test;') AT LINKSVR;
END TRY
BEGIN CATCH
    RAISERROR(N'Error called on Linked Server',16,1)
END CATCH

RAISERROR will output the custom error and the message returned from the Linked Server, however the message from the link server still can't be stored in a table but with the SQL Server Agent Job Activity Monitor the job will still fail at the step this occurs and in the step details all the data is outputted

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top