I've added a new column, packageNo
, to my table:
create table Packages(
id varchar(20) primary key, -- ok, I know :)
orderNo uniqueIdentifier not null,
orderlineNo int not null,
packageNo int not null default(0)
)
Now I want to generate the packageNo
with the following rules:
- reset it for each order
- ascendantfor order, orderline
My problem is that the script I wrote uses 15 minutes for 26500 rows on my testServer. Here it is:
set NoCount ON
declare @Counter int
declare @handledCounter int
declare @currentorder uniqueIdentifier
declare @fetchedOrder uniqueIdentifier
declare @fetchedId varchar(20) -- will using PK speed up things?
declare PackageNo_Cursor cursor for
select orderNo, id from packages order by orderNo, orderlineNo for update of packageNo
open PackageNo_Cursor
fetch next from PackageNo_Cursor into @fetchedOrder, @fetchedId
set @currentOrder = @fetchedOrder
set @counter = 0
set @handledCounter = 0
while @@fetch_status = 0
begin
if (@currentOrder <> @fetchedOrder)
begin -- reset counter for each order
set @currentOrder = @fetchedOrder
set @counter = 0
end
set @counter = @counter + 1
set @handledCounter = @handledCounter +1
if (@handledCounter % 50 = 0)
begin
print 'handled = ' + cast(@handledCounter as varchar)
end
update packages set packageNo = @counter where current of PackageNo_Cursor
fetch next from PackageNo_Cursor into @fetchedOrder, @fetchedId
end
close PackageNo_Cursor
deallocate PackageNo_Cursor
This should result in:
id - orderno - lineNo - packageNo (what I need to set)
ean1 - guid1 - 1 - 1
ean2 - guid1 - 2 - 2
ean3 - guid2 - 1 - 1
ean15- guid2 - 3 - 2
ean15- guid2 - 4 - 3
Can I make this run any faster?