How to Capture OLDBC Warning/Error message Captured from LinkedServer

30 Views Asked by At

How can I capture an error message returned from a linked server?

BEGIN TRY

        --execute oracle script     
        EXECUTE ('BEGIN DB.SP_SPName(?,?,?,?,?,?,?); end;', @p_STORE_ID,
                    @p_CREATE_DATE, @p_CREATE_USER, @p_From_DATE, @p_To_DATE, @p_INCLUDE_SISTER, @p_Session) AT LinkServerName;
                    
    END TRY
    BEGIN CATCH
        -- if error, abort execution 
        print('This is the Error : ' +ERROR_MESSAGE())
        select 4 as Result, ERROR_MESSAGE() as ErrorMsg
        return
    END CATCH`

Above code Returns this Error Message :

OLE DB provider "OraOLEDB.Oracle" for linked server "LinkServerName" returned message "ORA-20010: Invalid store id ORA-06512: at "DB.SP_SPName", line 104 ORA-06512: at line 1". This is the Error : Could not execute statement on remote server 'LinkServerName'.

How can I catch the "ORA-20010: Invalid store id" Part from the OLEDB error/warning message.

I tried putting SET XACT_ABORT ON and passing inline try catch block to the Oracle server as well.

Something Like This :

SET @command = '
    BEGIN TRY
        exec (''select * from xxx'') 
        SELECT @resultOUT = @@ERROR
    END TRY
    BEGIN CATCH
        SELECT @resultOUT = @@ERROR
    END CATCH'
    SET @ParmDefinition = N'@resultOUT nvarchar(5) OUTPUT'
    exec my_linked_server.sp_executesql 
        @command, 
        @ParmDefinition, 
        @resultOUT=@result OUTPUT

I did some research and know SQL Server does not recognize this as an error message etc so can't catch it. But these posts were from like 14 years(2010) ago. So there must be some fix right.....right..

0

There are 0 best solutions below