Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Issues while updating...

Posted on 2008-06-20
12
Medium Priority
?
246 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 143

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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 143

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
 
LVL 143

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 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

885 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