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 |
|
savagegod
Starting Member
3 Posts |
Posted - 2008-04-28 : 02:59:14
|
| Hello Friends,I have a doubt regarding balance sheet calculation in my Software. I am using a stored procedure for these calculations. I have some commands for calculation as given below:/******************************************************************/DECLARE @car_req FLOATDECLARE @amt_gen FLOATDECLARE @amt_shp FLOATDECLARE @amt_sho FLOATDECLARE @bal_gen FLOATDECLARE @bal_shp FLOATDECLARE @bal_sho FLOATDECLARE @tab_id INTCREATE TABLE #Temp ( rec_id INT IDENTITY NOT NULL , cargo_required FLOAT , amount_general FLOAT , amount_ship FLOAT , amount_shore FLOAT , balance_general FLOAT , balance_ship FLOAT , balance_shore FLOAT )INSERT INTO #Temp (cargo_required, amount_general, amount_ship, amount_shore)SELECT 5000, 1500, 1450, 1490UNION ALLSELECT 0, 3500, 3500, 3500UNION ALLSELECT 7000, 4500, 4550, 4560UNION ALLSELECT 0, 2500, 2000, 2000DECLARE temp_cursor CURSOR FAST_FORWARD FORSELECT rec_id, cargo_required, amount_general, amount_ship, amount_shoreFROM #TempOPEN temp_cursorFETCH NEXT FROM temp_cursorINTO @tab_id, @car_req, @amt_gen, @amt_shp, @amt_shoWHILE @@FETCH_STATUS = 0BEGIN IF (@car_req > 0) BEGIN UPDATE #Temp SET balance_general = @car_req - amount_general , balance_ship = @car_req - amount_ship , balance_shore = @car_req - amount_shore WHERE rec_id = @tab_id END ELSE BEGIN UPDATE #Temp SET balance_general = @bal_gen - amount_general , balance_ship = @bal_shp - amount_ship , balance_shore = @bal_sho - amount_shore WHERE rec_id = @tab_id END SELECT @bal_gen = balance_general, @bal_shp = balance_ship, @bal_sho = balance_shore FROM #Temp WHERE rec_id = @tab_id FETCH NEXT FROM temp_cursor INTO @tab_id, @car_req, @amt_gen, @amt_shp, @amt_shoENDSELECT * FROM #TempCLOSE temp_cursorDEALLOCATE temp_cursorDROP TABLE #Temp/***************************************************************/This is what acutally I am having in my stored procedure except the temporary table. The values which I am inserting here will get from my actual tables. What I need is to calculate balance. If you run this script you can see how the balance is calculating using the cursor.I want an efficient way to calculate the same. I know cursor is not at all good option in stored procedures. Do you have any Idea to make this script in an efficient and simple way?Thanks and RegardsBoney |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-28 : 03:49:31
|
Here is one of method using WHILE loop instead of cursorDECLARE @bal_gen FLOATDECLARE @bal_shp FLOATDECLARE @bal_sho FLOATDECLARE @tab_id INTCREATE TABLE #Temp(rec_id INT IDENTITY NOT NULL, cargo_required FLOAT, amount_general FLOAT, amount_ship FLOAT, amount_shore FLOAT, balance_general FLOAT, balance_ship FLOAT, balance_shore FLOAT)INSERT INTO #Temp (cargo_required, amount_general, amount_ship, amount_shore)SELECT 5000, 1500, 1450, 1490UNION ALLSELECT 0, 3500, 3500, 3500UNION ALLSELECT 7000, 4500, 4550, 4560UNION ALLSELECT 0, 2500, 2000, 2000SELECT @bal_gen=0,@bal_shp=0,@bal_sho=0SELECT @tab_id=MIN(rec_id)FROM #TempWHILE @tab_id IS NOT NULLBEGINUPDATE #TempSET @bal_gen=balance_general =CASE WHEN cargo_required > 0 THEN cargo_required ELSE @bal_gen END - amount_general, @bal_shp=balance_ship = CASE WHEN cargo_required > 0 THEN cargo_required ELSE @bal_shp END - amount_ship, @bal_sho=balance_shore = CASE WHEN cargo_required > 0 THEN cargo_required ELSE @bal_sho END - amount_shoreWHERE rec_id = @tab_idSELECT @tab_id=MIN(rec_id)FROM #TempWHERE rec_id >@tab_idENDSelect * FROM #Temp |
 |
|
|
savagegod
Starting Member
3 Posts |
Posted - 2008-04-28 : 04:52:52
|
Thank you so much Visakh. It's really a nice way. While loop is far better than cursor. But one more doubt, do we have any way to make this without using any loop method.I am going to use user method.Once again Thank You So Much. Thanks and RegardsBoney |
 |
|
|
|
|
|
|
|