Solved

Issues while updating...

Posted on 2008-06-20
12
189 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]
Comment Utility
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
Comment Utility
post your sql
0
 
LVL 14

Author Comment

by:Jagdish Devaku
Comment Utility

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

Expert Comment

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

Expert Comment

by:Mark Wills
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
>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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks
0
 
LVL 51

Expert Comment

by:Mark Wills
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

771 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

11 Experts available now in Live!

Get 1:1 Help Now