Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Issues while updating...

Posted on 2008-06-20
12
222 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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 combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.

791 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