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.
| Author |
Topic |
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-05-19 : 18:30:43
|
| GreetingsI have to update a table with new data based on another table. I need to do it in batches as there are millions of records.Now the source table does not necessarily have sequential id to use to map to dest table. So how do I go about creating a while fetch status so I can process 1000 rows at a time. using >= and <=?Thanks!!!If you don't have the passion to help people, you have no passion |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-19 : 18:39:49
|
| There are other things you can batch on, like dates, departments, etc. Does your source table have anything that lends itself to a BETWEEN statement? JimEveryday I learn something that somebody else already knew |
 |
|
|
jimf
Master Smack Fu Yak Hacker
2875 Posts |
Posted - 2011-05-19 : 19:16:47
|
| You could also make use of a cte (or temp table) where you add the arbitrary sequential id, e.g.;with cte as (select [id] = row_number(),sourceColumn, etc.)UPDATE tgtSET tgtColumn = sourceColumnFROM targetTable tgtINNER JOIN cte c on tgt.xxx = c.xxxWHERE c.id between @here and @thereset @here = @there + 1set @there = @here + 10000JimEveryday I learn something that somebody else already knew |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-05-19 : 19:31:12
|
| thanks Jim let me try that out! all i have is Ids I can do between.If you don't have the passion to help people, you have no passion |
 |
|
|
yosiasz
Master Smack Fu Yak Hacker
1635 Posts |
Posted - 2011-05-20 : 17:03:50
|
if anyone can use this and improve it it just loops through a certain table and grabs the Id you specify and stuffs them into a table/table variable. Then you can do a table valued function to join to the value returned.it was very helpful to medeclare @start intdeclare @end intdeclare @end_of_top_1000 intdeclare @inc intdeclare @count intdeclare @strin_of_all_ids nvarchar(max)declare @strin_of_ids nvarchar(max)declare @finished bitdeclare @concat_table table(Items nvarchar(max))declare @Items nvarchar(max)SET @finished = 0SET @start = 0SET @end = 0SET @inc = 0 SELECT top 1 @start = YourTable_id FROM dbo.YourTableORDER BY YourTable_idASC SELECT top 1 @end= ourTable_id FROM dbo.YourTableORDER BY YourTable_idDESC SELECT top 1 @end_of_top_1000 = YourTable_id FROM(SELECT YourTable_id ROW_NUMBER() OVER(ORDER BY YourTable_idASC) as rownum FROM dbo.YourTable) aWhere rownum = 1000PRINT '@start ' + cast(@start as nvarchar(max))PRINT '@end ' + cast(@end as nvarchar(max))PRINT '@end_of_top_1000 ' + cast(@end_of_top_1000 as nvarchar(max))PRINT '@inc ' + cast(@inc as nvarchar(max))SET @inc = 1000WHILE @finished = 0 BEGIN SELECT @strin_of_all_ids = @strin_of_all_ids + cast(YourTable_id as nvarchar(max)) + ', ' FROM dbo.YourTable WHERE YourTable_id >= @start and YourTable_id <= @end_of_top_1000 PRINT @strin_of_all_ids SET @start = @end_of_top_1000 IF @inc > @end BEGIN SET @inc = @end END SELECT top 1 @end_of_top_1000 = YourTable_id FROM ( SELECT YourTable_id , ROW_NUMBER() OVER(ORDER BY YourTable_id ASC) as rownum FROM dbo.YourTable ) a Where rownum >= @inc SET @inc = @inc + 1000 PRINT '@start ' + cast(@start as nvarchar(max)) PRINT '@end ' + cast(@end as nvarchar(max)) PRINT '@end_of_top_1000 ' + cast(@end_of_top_1000 as nvarchar(max)) PRINT '@inc ' + cast(@inc as nvarchar(max)) INSERT INTO @concat_table SELECT @strin_of_all_ids SET @strin_of_all_ids = '' --IF @end_of_top_1000 >= @end IF @inc > @end BEGIN PRINT '@start ' + cast(@start as nvarchar(max)) PRINT '@end ' + cast(@end as nvarchar(max)) PRINT '@end_of_top_1000 ' + cast(@end_of_top_1000 as nvarchar(max)) PRINT '@inc ' + cast(@inc as nvarchar(max)) SET @finished = 1 END END; If you don't have the passion to help people, you have no passion |
 |
|
|
|
|
|
|
|