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 |
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2013-07-05 : 00:45:20
|
Select Number,ID From TableAWhere Number>= 1 And AutoNo <= 10how can i run it as batch by batch to update.number where 1 to 10, then 11-20, then 21-30 and so on.. |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-05 : 01:38:03
|
you can use row_number() function for that and use it for selecting batch.See illustration belowDECLARE @BatchSize int,@BatchNo intSELECT *FROM(SELECT ROW_NUMBER() OVER (ORDER BY AutoNo) AS Seq,*FROM Table)tWHERE Seq >= ((@BatchNo-1)* @BatchSize) + 1AND Seq <= @BatchNo * @BatchSize if using SQL 2012, another option available is OFFSET..FETCH statement as belowSELECT *FROM tableORDER BY AutoNoOFFSET ((@BatchNo-1) * @BatchSize) ROWSFETCH NEXT (@BatchSize) ROWS ONLY ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2013-07-05 : 02:36:51
|
it doesnt return any record.. |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-05 : 02:39:51
|
quote: Originally posted by peace it doesnt return any record..
what values you passed for @BatchSize and @BatchNo?you need to pass some values to it. I just showed you the stubfor ex:DECLARE @BatchSize int,@BatchNo intSELECT @BatchSize =10,@BatchNo = 2SELECT *FROM(SELECT ROW_NUMBER() OVER (ORDER BY AutoNo) AS Seq,*FROM Table)tWHERE Seq >= ((@BatchNo-1)* @BatchSize) + 1AND Seq <= @BatchNo * @BatchSize if using SQL 2012, another option available is OFFSET..FETCH statement as belowDECLARE @BatchSize int,@BatchNo intSELECT @BatchSize =10,@BatchNo = 2SELECT *FROM tableORDER BY AutoNoOFFSET ((@BatchNo-1) * @BatchSize) ROWSFETCH NEXT (@BatchSize) ROWS ONLY ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
peace
Constraint Violating Yak Guru
420 Posts |
Posted - 2013-07-05 : 02:56:18
|
Select Number,ID From TableAWhere Number>= 1 And Number<= 10i would like to schedule to update tableB which has millions of record.thinking to batch it maybe 1-10 then 11-20, 21-30 and so on..the method gave need to set it manually ? |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-07-05 : 03:09:57
|
quote: Originally posted by peace Select Number,ID From TableAWhere Number>= 1 And Number<= 10i would like to schedule to update tableB which has millions of record.thinking to batch it maybe 1-10 then 11-20, 21-30 and so on..the method gave need to set it manually ?
Nope. you can automate it if needed by using a loopsomething likeDECLARE @BatchSize int,@BatchNo int,@RecordCount intSELECT @BatchSize =10,@BatchNo = 1SELECT @RecordCount=COUNT(*)FROM TableAWHILE @BatchSize * @BatchNo <= @RecordCountOR (@RecordCount BETWEEN (@BatchSize * (@BatchNo-1))+1AND (@BatchSize * @BatchNo) )BEGINUPDATE bSET b.Col1= a.Col1,..FROM tablB bINNER JOIN(SELECT ROW_NUMBER() OVER (ORDER BY AutoNo) AS Seq,*FROM TableA)tON t.Col = b.RelatedCol...WHERE Seq >= ((@BatchNo-1)* @BatchSize) + 1AND Seq <= @BatchNo * @BatchSizeSELECT @BatchNo = @BatchNo + 1 END ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
 |
|
|
|
|
|
|