views:

42

answers:

4
BEGIN    
 IF EXISTS(SELECT  * FROM Table1 WHERE ID=@ID)      
  BEGIN     
    UPDATE Table1 SET Name=@Name  WHERE ID=@ID    
    SELECT '1'   
  END    
 ELSE     
  SELECT '0'    
END  

Is this the row no. of the table or what ? Also "IF EXISTS" is checking what ? the table or if the ID exists or not ??

+4  A: 

It looks like whoever wrote that Stored Procedure is using that as a return value to indicate success or failure.

Doing things that way will result in a single row with a single column being returned for each call to the procedure.

The correct way to handle this would be to actually use the return value of the stored procedure, rather than returning the single column single row:

BEGIN
    IF EXISTS(SELECT * FORM Table1 WHERE ID = @ID)
    BEGIN
        UPDATE Table1 SET Name = @Name WHERE ID = @ID
        RETURN 1
    END

    RETURN 0
END
Justin Niessner
so 1 and 0 are return values ?? Why " RETURN 1 " not used instead ?
Serenity
@Happy Soul - I'm wondering the same thing. It could've easily been somebody who didn't know about RETURN.
Justin Niessner
SELECT '1' 's got 1 in single quotes so does this mean its a value only and not column no. or something?
Serenity
@Happy Soul - That just means they're returning 1 as a string value instead of a number.
Justin Niessner
oh ok..so 1 is a string..hmmmm
Serenity
I'd argue that the correct way would be **not** to use the return code and not to bother doing the existence check at all but simply do the update then check the rows affected passed back to the client application.
Martin Smith
Because it's T-SQL
AEMLoviji
@Martin - If you want to indicate success/failure, why return more information than is necessary? Returning the Row Count has its purposes...but that clearly isn't what the original code is going for (the update statement could affect multiple rows depending on what the keys are and the sproc would always return 1).
Justin Niessner
@Justin. It's entirely possible that another concurrent transaction deletes the row between the existence check and the attempted update. Does that still count as "success"?
Martin Smith
@Martin - I definitely wouldn't say the code was flawless (obviously). I was merely inferring intent from what code was already there.
Justin Niessner
A: 

Presumably some calling code checks this value to determine if a row was updated or not.

Rather than checking and updating (two table accesses) you might as well do this.

 UPDATE Table1 SET Name=@Name  WHERE ID=@ID    
 SELECT CASE WHEN @@Rowcount = 0 THEN 0 ELSE 1 END

If id is the PK then you can just do

 UPDATE Table1 SET Name=@Name  WHERE ID=@ID    
 SELECT  @@Rowcount 

Note as long as SET NOCOUNT is not on then the number of rows affected will get passed back to the client application anyway.

Martin Smith
A: 

Select '1' is used to indicate that Table1 contains the id value @ID (a parameter) was updated. Select '0' indicates that Table1 does not contain the id value @ID.

dwb
+2  A: 

The IF EXISTS is checking if there is a row in Table1 with the given ID. If there is a row it will update that row with the given name. The Select "1" will return "1" and Select "0" returns "0". The "1" or "0" would indicate if the row was found or not.

bmeding
ok...thanks....
Serenity