• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 247
  • Last Modified:

Issues while updating...

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
Jagdish Devaku
Asked:
Jagdish Devaku
  • 5
  • 4
  • 2
  • +1
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
can you show the relevant code, please?

the @@ROWCOUNT after the UPDATE should indicate the number of rows affected, not those "changed".
0
 
appariCommented:
post your sql
0
 
Jagdish DevakuSr DB ArchitectAuthor Commented:

update <tablename>
set <columnname1> = 'value1'
where <columnname2> = 'value2'
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
and how are you running that sql, ie counting the rows affected?
0
 
Mark WillsTopic AdvisorCommented:
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
 
Jagdish DevakuSr DB ArchitectAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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
 
Jagdish DevakuSr DB ArchitectAuthor Commented:
i dont have any trigger...

i am doing the same as before... but the message its hsowing is different..
0
 
Jagdish DevakuSr DB ArchitectAuthor Commented:
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
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
if your update gives 2 times a x rows affected, you MUST have a trigger on that table.
0
 
Jagdish DevakuSr DB ArchitectAuthor Commented:
Thanks
0
 
Mark WillsTopic AdvisorCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now