0

我目前正在用 T/SQL 编写一份特别报告。

基本上,我有一个包含 Windows 用户名的表,我必须为每个人运行一次存储过程(使用游标)。这可以使用EXECUTE AS USER = 'domain\username'. 但是,如果用户在生成表后丢失了他的 Windows 登录,我会收到以下错误:

  Could not obtain information about Windows NT group/user 'domain\user', error code 0x5.

此错误完全停止该过程,这会给我一个错误。有没有办法忽略此消息而直接跳到下一个光标值?

在此先感谢 Gunnar S. Knudsen

4

1 回答 1

1

如果您使用的是 SQL 2005 或更高版本,则可以使用TRY...CATCH (Transact-SQL)。以下是提供的链接中的示例:

-- Verify that the stored procedure does not already exist.
IF OBJECT_ID ( 'usp_GetErrorInfo', 'P' ) IS NOT NULL 
    DROP PROCEDURE usp_GetErrorInfo;
GO

-- Create procedure to retrieve error information.
CREATE PROCEDURE usp_GetErrorInfo
AS
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;
GO

BEGIN TRY
    -- Generate divide-by-zero error.
    SELECT 1/0;
END TRY
BEGIN CATCH
    -- Execute error retrieval routine.
    EXECUTE usp_GetErrorInfo;
END CATCH; 
于 2012-07-18T15:01:38.973 回答