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 |
|
SHIVPREET2K1
Starting Member
32 Posts |
Posted - 2010-03-07 : 07:30:44
|
Dear friends Please help me to sort out this simple problemI have a two variables which is getting a values from a cursor. its working fine. Suppose currently that variables has value@dispatched=5000@item_code='xxx'now i have a table with following sturctureOrder_no order_Date Item_code Order_Qty dispatch_qty abc 12-jan-10 xxx 2000 0xyz 15-feb-10 xxx 4000 0 what i need is to set off my dispatch value in the above mentioned table in the dispatch_qty column. Pleast tell me how can i do it.The final result for those values should beOrder_no order_Date Item_code Order_Qty dispatch_qty abc 12-jan-10 xxx 2000 2000xyz 15-feb-10 xxx 4000 3000 Thanks in advance for your helpShivpreet2k1 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-07 : 08:36:15
|
I don't like cursors but I would do that in a cursor. No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-07 : 09:17:27
|
| [code]--now i have a table with following sturctureCREATE TABLE Orders(Order_no varchar(10),order_Date datetime,Item_code varchar(3),Order_Qty int,dispatch_qty int) CREATE CLUSTERED INDEX IDX_Order_no ON Orders(order_Date,Order_no)INSERT INTO OrdersSELECT 'abc','12-jan-10','xxx',2000,0 UNION ALLSELECT 'xyz','15-feb-10','xxx',4000,0) UNION ALLSELECT 'wer','30-Jan-10','xxx',5000,0)DECLARE @dispatched int,@item_code varchar(3),@Order_no varchar(10),@allocated intSELECT @dispatched=10000SELECT * FROM OrdersSELECT TOP 1 @Order_no= Order_no,@item_code=Item_codeFROM OrdersUPDATE OrdersSET @allocated=dispatch_qty= CASE WHEN Order_Qty< @dispatched THEN Order_Qty ELSE @dispatched END, @Order_no = Order_no, @dispatched=@dispatched-@allocatedFROM Orders (TABLOCKX) WHERE Item_code=@item_codeOPTION (MAXDOP 1)SELECT * FROM OrdersDROP Table Ordersoutput--------------------------------before updateOrder_no order_Date Item_code Order_Qty dispatch_qtyabc 2010-01-12 00:00:00.000 xxx 2000 0wer 2010-01-30 00:00:00.000 xxx 5000 0xyz 2010-02-15 00:00:00.000 xxx 4000 0after updateOrder_no order_Date Item_code Order_Qty dispatch_qtyabc 2010-01-12 00:00:00.000 xxx 2000 2000wer 2010-01-30 00:00:00.000 xxx 5000 5000xyz 2010-02-15 00:00:00.000 xxx 4000 3000[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-03-07 : 13:33:28
|
cool  No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-07 : 13:45:05
|
quote: Originally posted by webfred cool  No, you're never too old to Yak'n'Roll if you're too young to die.
Thanks ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|