Re: Error handling in stored procedure AND checking



On Jan 3, 5:25 pm, "Plamen Ratchev" <Pla...@xxxxxxxxxxxxx> wrote:
SQL Server reports that the stored procedure executed successfully because
there are no errors. Just because your SQL statement attempts to update
non-existing SSN, it does not mean you will get an error. You have to handle
this using application logic, not to expect SQL Server to report an error.

If you really do want to get an error when invalid SSN update is attempted,
then you can add something like the code below at the end of your stored
procedure, just after the UPDATE statement:

IF @@ROWCOUNT = 0
RAISERROR ('Invalid SSN.', 16, 1)

The @@ROWCOUNT function returns the number of rows affected by the last
statement. Then if 0 rows have been updated it means the SSN is invalid and
RAISERROR will force an error.

However, based on the requirements that you need to return @SQLCode and
@ErrMsg, you are probably looking for something like this:

1). Add two OUTPUT parameters to the stored procedure for @@SQLCode and
@ErrMsg:
@SQLCode CHAR(1) OUTPUT, @ErrMsg VARCHAR(20) OUTPUT

2) Inside the stored procedure, just after the UPDATE check if rows have
been updated and set those two parameters:
IF @@ROWCOUNT = 0
SELECT @SQLCode = 'E', @ErrMsg = 'Invalid SSN.'

3) Then when you declare those two parameters and pass to the stored
procedure with the OUTPUT keyword.

This is just one example on how you can return the error code and message,
based on your application architecture different variations can be used. You
can probably go only with error message as the error code is redundant, but
not sure about your specs.

HTH,

Plamen Ratchevhttp://www.SQLStudio.com

thanks for the help, i understand what and how you are doing it and
had the output variables in my stored procedure at one time, but the
if statement was my downfall. I showed the guy that told me to create
the procedure and said it was coming along but wanted me to setup if
there was an error(i am guessing any error) that he wants an email
generated with the error sent to him. I am at a complete standstill
till i figure that one out.

Thanks for the help I do appreciate it.
.



Relevant Pages

  • Re: Stored Procedures v Views
    ... query draws the data from 4 tables. ... Do I use a stored proc or a view? ... > Just dynamically build a SQL statement using some other tool (like your VBA ... > this interface, even if your stored procedure is poorly written, you can ...
    (microsoft.public.sqlserver.programming)
  • Re: Cross Database Join, C++ program set up confusion
    ... the SQL statement I was trying to use in my CRecordset class in Query ... feeling that if I can get a stored procedure to generate a dynamic ... I found in MSDN documentation regarding CRecordset: ... If your tables are on the same data source, try the link I sent in my ...
    (microsoft.public.vc.database)
  • Re: Stored Procedures v Views
    ... "Louis Davidson" wrote: ... > Just dynamically build a SQL statement using some other tool (like your VBA ... > this interface, even if your stored procedure is poorly written, you can ... The code dynamically generates a SQL statement in VBA, ...
    (microsoft.public.sqlserver.programming)
  • Re: Can @@ROWCOUNT return NULL?
    ... from the stored procedure. ... > Immediately after each statement I save the value returned by @@ROWCOUNT to ... it appears that in SQL Server ... This is not the case here - the only DML queries are INSERTs into ...
    (comp.databases.ms-sqlserver)
  • Re: Stored Procedures v Views
    ... Build a stored procedure, ... The goal here will be to not use any dynamic sql like you used in Access. ... If the query is very complex, but frankly, if you have to do this, you are ... > they make are then used to create the SQL statement which is then used to ...
    (microsoft.public.sqlserver.programming)

Loading