>Sorry for the old thread. I was just re-reading it and I have a question:
>
>would it be better to change
>
IF EXISTS (SELECT * FROM cart WHERE cartid = @cartID)
>
>to
IF EXISTS (SELECT CartID FROM cart WHERE cartid = @cartID)
>
>Thanks a lot in advance.
For this example, it probably doesn't matter since you have to have cartid anyhow.
Now, if you're options were:
IF EXISTS(SELECT * FROM cart WHERE cartid = @cartID)
OR
IF (SELECT COUNT(*) FROM cart WHERE cartid = @cartID) > 0
OR
IF (SELECT COUNT(cartID) FROM cart WHERE cartid = @cartID) > 0
OR
IF (SELECT COUNT(anothercolumn) FROM cart WHERE cartid = @cartID) > 0
It's a different matter. The EXISTS() will always provide better performance since SQL Server can stop at the first row that qualifies and declare a TRUE condition.
The second and third examples should perform the same, as long as there are no NULLs in the cartID column.
I would expect the second and third examples to perform better then the fourth since both the second and third only reference one column and it is more likely that there is a non-clustered index that covers the queries.
-Mike