views:

190

answers:

3

Say we have stored procedure(s) performing simple operations

CREATE PROCEDURE [dbo].[AddNewAuthorReturnID]
(   
    @Author_Name VARCHAR(MAX),
    @Author_ID int OUTPUT
)
AS
    SET NOCOUNT OFF;
BEGIN
 INSERT INTO AUTHORS (@Author_Name)
 VALUES (@Author_Name)
 SET @Author_ID = SCOPE_IDENTITY()
 SELECT @Author_ID
END

in above procedure, the retuned id is an indication of successful operation.

Consider this

CREATE PROCEDURE [dbo].[DeleteAuthor]
(       
    @Author_ID int 
)
AS
    SET NOCOUNT OFF;
BEGIN
 DELETE FROM AUTHORS 
 WHERE
 (Author_ID = @Author_ID)
END
  • How can we know the operation was successful and record (author) was succesfully removed, if we use above procedure ?

  • Same with an update operation ?

thanks

+1  A: 

You could select @@rowcount

It will show you the rows affected.

e.g

CREATE PROCEDURE [dbo].[DeleteAuthor]
(       
    @Author_ID int 
)
AS
    SET NOCOUNT OFF;
BEGIN
 DELETE FROM AUTHORS 
 WHERE
 (Author_ID = @Author_ID)
 SELECT @@ROWCOUNT 
END

This can be applied to update too.

CREATE PROCEDURE [dbo].[UpdateAuthor]
(       
    @Author_ID int 
)
AS
    SET NOCOUNT OFF;
BEGIN
 UPDATE AUTHORS 
    SET AuthorName = 'John'
 WHERE
 (Author_ID = @Author_ID)
 SELECT @@ROWCOUNT 
END

Alternatively you could use @@Error and raise an error id @@rowcount > 1 (if you only wanted to update one row).

e.g

CREATE PROCEDURE [dbo].[DeleteAuthor]
(       
    @Author_ID int 
)
AS
    SET NOCOUNT OFF;
BEGIN
 DELETE FROM AUTHORS 
 WHERE
 (Author_ID = @Author_ID)

 IF @@ROWCOUNT <>1
 BEGIN
  RAISERROR ('An error occured',10,1)
  RETURN -1
 END
END

As Giorgi says this will be returned as a returncode.

John Nolan
@John, What if the statement did not affect any rows but no error has occurred?
Giorgi
what about an update operation ?
Asad Butt
@asdi, you can use @@ROWCOUNT in the same way with update operation but I do not recommend using it because if the stored procedure executes without error but no rows are affected it will return 0
Giorgi
What is the better approach then ? should we make a check after the operation than record is still there or not ?
Asad Butt
@asdi, return @@ERROR from you stored procedure.
Giorgi
+2  A: 

You can to return @@ROWCOUNT to determine if your last statement affected any record.

Rubens Farias
@Rubens, What if the statement did not affect any rows but no error has occurred?
Giorgi
You're right, @Giorgi but I would to expect update/delete statements to affect records on online applications; bottom line is: depends on what OP want to do
Rubens Farias
+2  A: 

You can return value from stored procedure using return statement. The @@ERROR variable is equal to zero if there was no error.

@@ERROR

CREATE PROCEDURE [dbo].[DeleteAuthor]
(       
    @Author_ID int 
)
AS
    SET NOCOUNT OFF;
BEGIN
 DELETE FROM AUTHORS 
 WHERE
 (Author_ID = @Author_ID)

 Return @@ERROR

END
Giorgi
This returns if there is no error but what if the user was expecting to delete a record and it wasn't there?
John Nolan