Solved

Updating Multiple rows

Posted on 2010-08-23
6
396 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
Industry Leaders: 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!

 
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

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!

Question has a verified solution.

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

When writing XML code a very difficult part is when we like to remove all the elements or attributes from the XML that have no data. I would like to share a set of recursive MSSQL stored procedures that I have made to remove those elements from …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

752 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