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

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 T -SQL help 2
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

mk_garg20
Constraint Violating Yak Guru

Australia
343 Posts

Posted - 11/24/2005 :  23:58:32  Show Profile  Reply with Quote
here is link to orogonal post.
I think it was not clear but i want to achieve there.
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=58189

so modified my post here


create table #stock
(
[id] int,
stockcode varchar(10),
startrange int,
endrange int,
nextavailablenumber int,
StartPick int,
EndPick int
)

INSERT INTO #stock values (1,'A',1,20,1,NULL,NULL)
INSERT INTO #stock values (2,'A',201,250,201,NULL,NULL)
INSERT INTO #stock values (3,'A',401,500,401,NULL,NULL)

/*
declare @qty_to_pick int
set @qty_to_pick =30

 if @qty_to_pick is 30 then 
help me in writing a query 
to update table with following values
id	stockcode	startrange	endrange	nextavailablenumber	StartPick	EndPick
1	A		1		20		21			1		20
2	A		201		250		211			201		210
3	A		401		500		401			NULL		NULL
*/

/*
declare @qty_to_pick int
set @qty_to_pick =80

 if @qty_to_pick is 80 then help me in writing a query 
to update table with following values

id	stockcode	startrange	endrange	nextavailablenumber	StartPick	EndPick
1	A		1		20		21			1		20
2	A		201		250		251			201		250
3	A		401		500		411			401		410
*/

select * from #stock



DROP TABLE #stock



mk_garg

Edited by - mk_garg20 on 11/27/2005 17:04:36

mk_garg20
Constraint Violating Yak Guru

Australia
343 Posts

Posted - 11/27/2005 :  16:54:35  Show Profile  Reply with Quote
Look like nobody can help.


mk_garg
Go to Top of Page

mk_garg20
Constraint Violating Yak Guru

Australia
343 Posts

Posted - 11/27/2005 :  19:48:12  Show Profile  Reply with Quote
I got it

create table #stock
(
[id] int,
stockcode varchar(10),
startrange int,
endrange int,
nextavailablenumber int,
StartPick int,
EndPick int
)

INSERT INTO #stock values (1,'A',1,20,1,NULL,NULL)
INSERT INTO #stock values (2,'A',201,250,201,NULL,NULL)
INSERT INTO #stock values (3,'A',401,500,401,NULL,NULL)

Declare @qty int,@id int, @start int,@end int,@qtyavailable int,@diff int,@startpick int,@endpick int,@nextpick int
Set @qty=10

while (@qty>0)
Begin 
	select @id=min(id) from #stock where nextavailablenumber<endrange
	select @start=nextavailablenumber,@end=endrange	from #stock where id=@id
	set @diff=@end  - @start + 1
	if @diff > @qty
		BEGIN
		set @startpick=@start
		set @endpick=@start+@qty - 1
		set @nextpick=@start+ @qty
		set @qty=@qty-@diff
		update #stock set startpick=@startpick,endpick=@endpick,nextavailablenumber=@nextpick where id=@id
		END
	else
		BEGIN
		set @startpick=@start
		set @endpick=@end
		set @nextpick=@end+1
		set @qty=@qty-@diff
		update #stock set startpick=@startpick,endpick=@endpick,nextavailablenumber=@nextpick where id=@id
		end
End

select * from #stock

DROP TABLE #stock


unless someone can suggest set base approach.

mk_garg
Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.03 seconds. Powered By: Snitz Forums 2000