无法在SQL SERVER 2008R2中捕获错误

时间:2017-12-05 14:32:07

标签: sql sql-server-2008 sql-server-2008-r2 cursor

我试图使用try catch块捕获下面查询中的错误但是即使查询抛出错误,查询也没有遇到catch块。当我在不使用TRY-CATCH块的情况下执行查询时,我收到以下错误:

  

Msg 7314,Level 16,State 1,Procedure PROC_NAME,97行   OLE DB提供程序" SQLNCLI11"对于链接服务器" LINKEDSERVER"不包含表格"" DB_NAME"。" dbo"。" VIEW_NAME""。该表不存在或当前用户没有该表的权限。   Msg 2020,Level 16,State 1,Line 34   为实体" SP_NAME"报告的依赖关系可能不包含对所有列的引用。这是因为实体引用了一个不存在的对象,或者是因为实体中的一个或多个语句中的错误。在重新运行查询之前,请确保实体中没有错误,并且实体引用的所有对象都存在。

SELECT DISTINCT NAME INTO #ALL_SPS FROM SYSOBJECTS SO,SYSCOMMENTS SC WHERE SO.ID = SC.ID 
AND TEXT LIKE '%LINKEDSERVER%'

CREATE TABLE #ERRORS
(
    ERRORNUMBER VARCHAR(100),
    ERRORSEVERITY   VARCHAR(100),
    ERRORSTATE  VARCHAR(100),
    ERRORPROCEDURE  VARCHAR(100),
    ERRORLINE   VARCHAR(100),
    ERRORMESSAGE VARCHAR(100)
)

CREATE TABLE #TEMP(SP_NAME VARCHAR(500),DB VARCHAR(100), LINKEDSERVER VARCHAR(100))

BEGIN    
DECLARE @SP_NAME VARCHAR(MAX)  
DECLARE @STARTTIME DATETIME =GETDATE()

DECLARE HDR_CURSOR CURSOR FOR   

SELECT NAME FROM #ALL_SPS --IS TEMP TABLE IMPORTED FROM SHARED EXCEL 

OPEN HDR_CURSOR    

FETCH NEXT FROM HDR_CURSOR INTO @SP_NAME

WHILE (@@FETCH_STATUS = 0 )    
   BEGIN    
BEGIN TRY
BEGIN TRANSACTION;

    INSERT INTO #TEMP
    SELECT  
    TBLSQLREFERENCEDENTITY.REFERENCED_ENTITY_NAME AS REFERENCEDENTITY,  
    COALESCE(REFERENCED_DATABASE_NAME,DB_NAME()) AS REFERENCEDDATABASE,  
    COALESCE(TBLSQLREFERENCEDENTITY.REFERENCED_SERVER_NAME,'CURRENT SERVER') AS REFERENCEDSERVER   
    FROM SYS.DM_SQL_REFERENCED_ENTITIES('DBO.'+@SP_NAME, 'OBJECT') TBLSQLREFERENCEDENTITY  
    WHERE  TBLSQLREFERENCEDENTITY.REFERENCED_SERVER_NAME ='LINKEDSERVER'
    GROUP BY REFERENCED_ENTITY_NAME,REFERENCED_DATABASE_NAME,REFERENCED_SERVER_NAME 
    COMMIT TRANSACTION;
END TRY
BEGIN CATCH

INSERT INTO #ERRORS
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;

END CATCH
  FETCH NEXT FROM HDR_CURSOR INTO @SP_NAME

 END    
 CLOSE HDR_CURSOR    
 DEALLOCATE HDR_CURSOR    
 SELECT DATEDIFF(MS,@STARTTIME,GETDATE()) 'TIME TAKEN IN MS'
 SELECT DISTINCT * FROM #TEMP
 SELECT DISTINCT * FROM #ERRORS

END

1 个答案:

答案 0 :(得分:1)

您正在混合编译和运行时错误。假设您拥有该表的权限,更好的方法就是......

Rx.Observable.fromPromise

确定在链接服务器中的当前数据库中的对象

...
BEGIN TRANSACTION;

--see if that object exists in your linked server...
if(select 1 
   from  YourLinkedServer.master.sys.objects
   where [name] = @SP_NAME) is not null


--if it exists, then insert from it...
INSERT INTO #TEMP
SELECT  
TBLSQLREFERENCEDENTITY.REFERENCED_ENTITY_NAME AS REFERENCEDENTITY,  
COALESCE(REFERENCED_DATABASE_NAME,DB_NAME()) AS REFERENCEDDATABASE,  
COALESCE(TBLSQLREFERENCEDENTITY.REFERENCED_SERVER_NAME,'CURRENT SERVER') AS REFERENCEDSERVER   
FROM SYS.DM_SQL_REFERENCED_ENTITIES('DBO.'+@SP_NAME, 'OBJECT') TBLSQLREFERENCEDENTITY  
WHERE  TBLSQLREFERENCEDENTITY.REFERENCED_SERVER_NAME ='LINKEDSERVER'
GROUP BY REFERENCED_ENTITY_NAME,REFERENCED_DATABASE_NAME,REFERENCED_SERVER_NAME 

COMMIT TRANSACTION;

...