Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 batch update

Author  Topic 

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-12 : 13:37:23
I have a process where Im working on 100 or so records at a time. transforming some data in the fields.

How can i batch update all 200 records at once in the database once i have completed all my process.

basically there is a worker process that does some stuff on some records and then i want to update those records all at once with the new info

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-04-12 : 13:39:32
just write a set based update based on some condition. like in first process use a column in table to designate what all records were processed and then in second step update them based on this column value.

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-12 : 13:41:32
Place your updates in a temp table, then join the temp table to the main table to apply them all at once.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-12 : 15:28:13
how do you join a temp table to a main table and update the main table?
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-13 : 00:25:50
Change the value in below statement to your actual table and column name.

Update MainTable Set ColumnToUpdate = TempTable.ColumnWhichHasValue
from MainTable inner join TempTable
on MainTable.CommonKey = TempTable.CommonKey

Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-13 : 00:30:16
Example for your understanding:


Declare @MainTable Table
(Srno int identity,
Sname varchar(50),
Marks int)

Declare @TempTable Table
(Srno int ,
SumOfMarks int)
Insert into @MainTable(Sname) Values ('Xyz')
Insert into @MainTable(Sname) Values ('ABC')
Insert into @MainTable(Sname) Values ('EFG')


INSERT INTO @TempTable (Srno,SumOfMarks) Values (1, 366)
INSERT INTO @TempTable (Srno,SumOfMarks) Values (2, 421)
INSERT INTO @TempTable (Srno,SumOfMarks) Values (3, 350)

Update @MainTable Set Marks = T.SumOfMarks
from @MainTable M inner join @TempTable T
on M.Srno = T.Srno

Select * from @MainTable


Regards,
Bohra

I am here to learn from Masters and help new bees in learning.
Go to Top of Page

CrazyT
Yak Posting Veteran

73 Posts

Posted - 2010-04-13 : 11:32:00
thanks alot
Go to Top of Page

pk_bohra
Master Smack Fu Yak Hacker

1182 Posts

Posted - 2010-04-14 : 01:46:09
You are welcome
Go to Top of Page
   

- Advertisement -