Solved

Updating Multiple rows

Posted on 2010-08-23
6
401 Views
Last Modified: 2012-05-10
I want update 12 carore records. I want to update from record 1 to 5000, then from 5001 to 10000, then from 10001 to 15000 and so on. How to do it in a loop. Query is here but it always update first 5000. How to update next 5000 record from 50001 to 10,000 and then from 10,001 to 15000.
SET ROWCOUNT 5000
WHILE (1=1) BEGIN
   BEGIN TRANSACTION

update newformularydtl
set newformularydtl.ddid=b.ddid from newformularydtl a,Medispan.dbo.mmw_drug_pack b
where a.ProductID=b.ppid
   IF @@ROWCOUNT = 0
   BEGIN
      COMMIT TRANSACTION
      BREAK
   END
   COMMIT TRANSACTION
END
0
Comment
Question by:Salman_Ali
[X]
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
6 Comments
 
LVL 58

Accepted Solution

by:
cyberkiwi earned 405 total points
ID: 33507523
where a.ProductID=b.ppid
and a.ddid <> b.ddid
0
 
LVL 7

Expert Comment

by:Alpha Au
ID: 33508217
any condition to indicate the record is updated or not?

like what cyberkiwi suggested.
SET ROWCOUNT 5000
WHILE (1=1) BEGIN
   BEGIN TRANSACTION

update newformularydtl
set newformularydtl.ddid=b.ddid 
from newformularydtl a,Medispan.dbo.mmw_drug_pack b
where a.ProductID=b.ppid
and a.ddid<>b.ddid 
   IF @@ROWCOUNT = 0
   BEGIN
      COMMIT TRANSACTION
      BREAK
   END
   COMMIT TRANSACTION
END

Open in new window

0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33508636
and, in case that the productid could be null, you need to check that case also.
with isnull, putting a "default value" that cannot be in the normal range, but in the same data type.
so, if the field productid is numerical, you might not have "0" as possible value:

SET ROWCOUNT 5000
WHILE (1=1) BEGIN
   BEGIN TRANSACTION

update newformularydtl
set newformularydtl.ddid=b.ddid 
from newformularydtl a,Medispan.dbo.mmw_drug_pack b
where a.ProductID=b.ppid
and isnull(a.ddid, 0) <> isnull(b.ddid , 0)
   IF @@ROWCOUNT = 0
   BEGIN
      COMMIT TRANSACTION
      BREAK
   END
   COMMIT TRANSACTION
END

Open in new window

0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33508657
Well, while we are improving.. this caters for all cases and has no condition that 0 cannot be in the data.

SET ROWCOUNT 5000
WHILE (1=1) BEGIN
   BEGIN TRANSACTION

update newformularydtl
set ddid=b.ddid
from Medispan.dbo.mmw_drug_pack b
where newformularydtl.ProductID=b.ppid
  and ((newformularydtl.ddid <> b.ddid)
       or (newformularydtl.ddid is null and b.ddid is null))
   IF @@ROWCOUNT = 0
   BEGIN
      COMMIT TRANSACTION
      BREAK
   END
   COMMIT TRANSACTION
END
0
 
LVL 19

Expert Comment

by:Bhavesh Shah
ID: 33509433

Mr.Genius(angelIII) & Mr.Sage(Cyberkiwi)

Is it not good if author makes one perfect index update the data one shot.

Please comment on it.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 33509476
for the record:
> 12 carore (aka crore) http://en.wikipedia.org/wiki/Crore
seems to be 12 x 10 million rows.

to update those in 1 shot can take some time, especially if most rows have to be updated.
if most rows are already ok, the update should indeed be done in 1 single go, but ensuring that rows that are already ok arenot updated (without the rowcount):
update newformularydtl
set ddid=b.ddid
from Medispan.dbo.mmw_drug_pack b
where newformularydtl.ProductID=b.ppid
  and ((newformularydtl.ddid <> b.ddid)
       or (newformularydtl.ddid is null and b.ddid is null))

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

So every once in a while at work I am asked to export data from one table and insert it into another on a different server.  I hate doing this.  There's so many different tables and data types.  Some column data needs quoted and some doesn't.  What …
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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