Solved

RAISERROR

Posted on 2009-05-20
8
460 Views
Last Modified: 2012-05-07
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
Comment
Question by:elkhawajah
  • 3
  • 3
  • 2
8 Comments
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24432390
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
 
LVL 1

Author Comment

by:elkhawajah
ID: 24433942
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
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24434216
>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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434281
Where is the problem?  C#, T-SQL?
0
 
LVL 1

Author Comment

by:elkhawajah
ID: 24434362
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
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 24434503
So is the RAISEERROR throwing the exception in the Stored Procedure?
0
 
LVL 1

Author Comment

by:elkhawajah
ID: 24434560
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
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 250 total points
ID: 24436890
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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question