English 中文(简体)
如何在存储程序中用 RAISEROR 设定参数?
原标题:How can i create out parameters with RAISERROR in stored procedure?

如何在 MSSQL 中用 RAISEROR 创建参数?

此 id 存储 程序 :

BEGIN TRY

//OPERATION

END TRY
BEGIN CATCH
    DECLARE @ErrorMessage NVARCHAR(4000);
    DECLARE @ErrorSeverity INT;
    DECLARE @ErrorState INT;

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

    -- Use RAISERROR inside the CATCH block to return error
    -- information about the original error that caused
    -- execution to jump to the CATCH block.
    RAISERROR (@ErrorMessage, -- Message text.
               @ErrorSeverity, -- Severity.
               @ErrorState -- State.
               );
END CATCH;
最佳回答

如果我能理解你的问题 你可以创建一个SP 返回错误 以便:

CREATE PROCEDURE [dbo].[mySp]
(
    @ErrorMessage NVARCHAR(4000) output =   ;
    @ErrorSeverity INT output = 0;
    @ErrorState INT output = 0;
)
AS

BEGIN TRY
 -- OPERATION
END TRY
BEGIN CATCH

    SELECT 
        @ErrorMessage = ERROR_MESSAGE(),
        @ErrorSeverity = ERROR_SEVERITY(),
        @ErrorState = ERROR_STATE();

END CATCH;

执行 SP 并检查错误:

DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;

EXEC mySp (@ErrorMessage output, @ErrorSeverity output, @ErrorState output);

if len(@ErrorMessage) > 0
BEGIN
    -- an error occur
    select @ErrorMessage, @ErrorSeverity, @ErrorState;

    -- or you can use the RAISERROR
    RAISERROR (@ErrorMessage, @ErrorSeverity, @ErrorState);
END
ELSE
BEGIN
    print  OK ;   
END
问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签