sql - Select a specific number of rows in a loop when the key is not number but an alphanumeric value -
i @ trying select 1000 rows tables in sql server , in cases easy because have key bigint. thus, store last number of key have fetched , add 100. see below:
--get last loaded record declare @startindex bigint  set @startindex = 1 + (select [storedindex]                        [db].[dbo].[masterdata]                        [status] = 'lastload')  --declare , set last record loaded declare @endindex bigint  --retrieve next @step+1 or less records , store them in temporary table select t1.* #tempresults     --get next @step+1 or less records     (select *      anotherdb.[tablename]      [tables_id] between @startindex , @startindex + 1000) t1  --set index of last record inserted set @endindex = (select max([tables_id])--the next record fetched largest id                  #tempresults) however how when key alphanumeric value?
what equivalent for
where [tables_id] between @startindex , @startindex + 1000 if @startindex nvarchar, example 123g7_56y7f ?
thank you!
method 1:
if using sql server 2012 or higher can use offset fetch below:
  select    *        anotherdb.[tablename]   order  col1    offset xx rows fetch next yy rows only; col1 = column going sort data
xx = number of rows skipped
yy = number of rows fetched
method 2:
you can make use of row_number function(sql server 2008 or higher), below:
  select    *        ( select    * ,                     row_number() on ( order col1) rn                anotherdb.[tablename]         ) k       k.rn between xx , yy; 
Comments
Post a Comment