Solved

Issues while updating...

Posted on 2008-06-20
12
202 Views
Last Modified: 2010-04-21
Hi,

I am facing an issue while updating a value in a table.

While I am executing a update query with condition, its executing fine but its returning total count of the rows of the table. Actually its updating only 2 rows.

I want it to return the count of the rows which got updated...

anyone help me...

thanks in advance...
0
Comment
Question by:Jagdish Devaku
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21829050
can you show the relevant code, please?

the @@ROWCOUNT after the UPDATE should indicate the number of rows affected, not those "changed".
0
 
LVL 39

Expert Comment

by:appari
ID: 21829092
post your sql
0
 
LVL 14

Author Comment

by:Jagdish Devaku
ID: 21829357

update <tablename>
set <columnname1> = 'value1'
where <columnname2> = 'value2'
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21829386
and how are you running that sql, ie counting the rows affected?
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21831698
the count shown in the message is the number of rows that have been affected - ie changed.

NULLS can sometimes lead to a misunderstanding...


update <tablename>
set <columnname1> = 'value1'
where <columnname2> = 'value2'
select @@rowcount as total_rows_updated

to see which ones were being updated, could use the output clause...

declare @tmp_log table (<uniquefieldlist> int, <columnname1> varchar(100),<old_columnname1> varchar(100), <columnname2> varchar(100))  -- where definitions match properly...
update <tablename>
set <columnname1> = 'value1'
output inserted.<uniquefieldlist>,inserted.<columnname1> ,deleted.<columnname1> ,inserted.<columnname2>  into @tmp_log
where <columnname2> = 'value2'
select * from @tmp_log
0
 
LVL 14

Author Comment

by:Jagdish Devaku
ID: 21836659
When I execute the below statement...

update <tablename>
set <columnname1> = 'value1'
where <columnname2> = 'value2'

it sometimes give me the no. of row actually updated but some times its giving me the count of the rows in the table....

@@rowcount will run as another statement.
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 21836935
>sometimes ...
only if the UPDATE - WHERE is to be affecting all the rows of the table...
now, you might have a trigger on the table?
0
 
LVL 14

Author Comment

by:Jagdish Devaku
ID: 21836952
i dont have any trigger...

i am doing the same as before... but the message its hsowing is different..
0
 
LVL 14

Author Comment

by:Jagdish Devaku
ID: 21897174
Recently when i run the update statement...

UPDATE tbl_Stores set shopping_mode='online' where shopping_mode='online'

It gave the following two results...

(1192 row(s) affected)

(876 row(s) affected)

I would like to know whats happening exactly...???
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 21897305
if your update gives 2 times a x rows affected, you MUST have a trigger on that table.
0
 
LVL 14

Author Closing Comment

by:Jagdish Devaku
ID: 31469064
Thanks
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 21994008
In SQl Server Management Studio, use the object browser (on the left) to "drill down" to the table concerned, ie: tbl_stores. You do that by expanding the server, then databases, then the database itself, then tables using the little + icon on the left - much the same way as you would with explorer.

One you are positioned on that table, expand it and you will see a "Triggers" section. expand that, and if there is anything there, then you definitely have a trigger...

Right click on that trigger and select the option Script Trigger and then to a new query window. That will show you what is happening. If it does exist, then you can post it here and we can help decipher.

It could also be possible that there are audit events and customer audit events happening - and that is a lot more involved. It could also be a cascading update on a foreign key - but would imagine a bit more activity...

Getting two lots of (... row(s) affected) most definitely points to the existance of a trigger as a first choice...
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now