• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 486
  • Last Modified:

RAISERROR

Hi Experts ..

I have a procedure similar to the one I posted bellow, and I am trying to catch the exception from C# method to determine if the exception was thrown from within the IF block or not.

I want to build the application logic based on that .. do you have any idea how to do it?

Thanks
CREATE PROCEDURE spAddSomething
          @ID  BIGINT OUTPUT,
           @AnotherID BIGINT,
AS
BEGIN
     IF (NOT EXIST(SELECT [ID] FROM TableX WHERE [ID] = @AnotherID))
     BEGIN
        RAISERROR ('The passed Another Id odes not exist in persistence.', 16, 1)
     END
     .. insert statement
END

Open in new window

0
elkhawajah
Asked:
elkhawajah
  • 3
  • 3
  • 2
1 Solution
 
Aneesh RetnakaranDatabase AdministratorCommented:
I am NOT sure about handling this at the front end, i think it is better to return another value in case the ID exists rather than a RAISERROR


CREATE PROCEDURE spAddSomething
          @ID  BIGINT OUTPUT,
           @AnotherID BIGINT,
AS
BEGIN
     IF EXISTS(SELECT [ID] FROM TableX WHERE [ID] = @AnotherID)
     BEGIN
        SET @ID = -201
        RETURN
     END
     .. insert statement
END

go


or else you can modify the sp like this

    INSERT INTO TableX
    SELECT
    WHERE NOT EXISTS (SELECT 1 FROM TableX where ID = @AnotherID)
   SELECT @ID = SCOPE_IDENTITY()

at the front end, if the return id is NULL, NO insert operation performed .
0
 
elkhawajahAuthor Commented:
I cannot believe that this cannot be done in Sql Serverr 2005! in Oracle I get used to set an error number in the thrown exception, use that number to determine the cause of the problem.
0
 
Aneesh RetnakaranDatabase AdministratorCommented:
>I cannot believe that this cannot be done in Sql Serverr 2005!
I am not sure about this, i dont think any logic why you use sql server to throw an error rather than returning an error code; databases designed to store and retrieve the data, and it should be the front end's task to manipulate them. May be someone from .Net can answer your qn. to be frank this is 'Bad usage of SQL'
:)
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
Anthony PerkinsCommented:
Where is the problem?  C#, T-SQL?
0
 
elkhawajahAuthor Commented:
I would expect the procedure to throw exception, and I can use a code similar to the posted below to throw specific exception based on the cause error.
try
{
     .. Execute Procedure
}
catch(Exception ex)
{
    if(ex.InnerException is SqlException)
    {
        if((ex.InnerException as SqlException).Number == EntityNotFoundNumber)
        {
              throw new EntityNotFoundCustomException(msg, ex.InnerException);
        }
    }
    throw new ServiceCustomException(msg, ex);
}

Open in new window

0
 
Anthony PerkinsCommented:
So is the RAISEERROR throwing the exception in the Stored Procedure?
0
 
elkhawajahAuthor Commented:
Yes, but I want to use the information in RAISERROR to determine the cause of the error in the stored procedure, and throw the proper custom exception type.

I have more than one RAISERROR in the stored procedure, and each error has a corresponding custom exception in C# code.
0
 
Anthony PerkinsCommented:
I guess I am not following.  The error is raised in T-SQL, so what is missing? Are you saying you need a different error number in each case? If so then give it a different number.  For custom errors the convention is to use numbers > 50000.
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now