browse: forums | FAQ
Connecting Tech Pros Worldwide

Hey there! Do you need Microsoft SQL Server help?

Get answers from our community of Microsoft SQL Server experts on BYTES! It's free.

Value of @@Error after performing update that finds no records?

M Wells
Guest
 
Posts: n/a
#1: Jul 20 '05
Hi All,

Just wondering if you perform an update using a where condition that
actually returns no rows to be updated, does @@Error contain an error
message afterwords?

We want to fire an update ona table against records that may or may
not exists. As a matter of course we test for an @@Error afterwards
and rollback our transaction and exit our stored proc if an error is
returned.

However, we don't want this to happen if there are no records in the
target table that match the where clause of the update statement.

So, if no records are found, does this raise an error?

Many thanks in advance!

Much warmth,

Murrau



John Bell
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Value of @@Error after performing update that finds no records?


Hi

Just because no rows are updated does not mean it is an error!! @@ERROR will
still be 0
The number of rows changed will be in @@ROWCOUNT. This could have been
ascertained with a simple test such as:

create table Myupdatetable ( id int, val char(1) )

insert into Myupdatetable ( id, val ) values ( 1, 'A' )
insert into Myupdatetable ( id, val ) values ( 2, 'B' )
insert into Myupdatetable ( id, val ) values ( 3, 'C' )


UPDATE Myupdatetable
SET id = id + 2
WHERE ID % 2 = 1

SELECT @@ERROR, @@ROWCOUNT

UPDATE Myupdatetable
SET id = id + 2
WHERE ID = 0

SELECT @@ERROR, @@ROWCOUNT



John

"M Wells" <planetquirky@planetthoughtful.org> wrote in message
news:8j6p60h8a3h3odaj2g6p371dsbt334m1pi@4ax.com...[color=blue]
> Hi All,
>
> Just wondering if you perform an update using a where condition that
> actually returns no rows to be updated, does @@Error contain an error
> message afterwords?
>
> We want to fire an update ona table against records that may or may
> not exists. As a matter of course we test for an @@Error afterwards
> and rollback our transaction and exit our stored proc if an error is
> returned.
>
> However, we don't want this to happen if there are no records in the
> target table that match the where clause of the update statement.
>
> So, if no records are found, does this raise an error?
>
> Many thanks in advance!
>
> Much warmth,
>
> Murrau[/color]


M Wells
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Value of @@Error after performing update that finds no records?


On Thu, 01 Apr 2004 23:45:42 GMT, "John Bell"
<jbellnewsposts@hotmail.com> wrote:
[color=blue]
>Hi
>
>Just because no rows are updated does not mean it is an error!! @@ERROR will
>still be 0
>The number of rows changed will be in @@ROWCOUNT. This could have been
>ascertained with a simple test such as:[/color]

Thank you John!

Much warmth,

Murray
Closed Thread