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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Taking ages to run the code

Author  Topic 

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-05 : 04:15:51
CREATE PROCEDURE TESTING /*(@ds char(1), @startdate datetime, @enddate datetime, @startcode varchar(30), @endcode varchar(30))*/
AS
DECLARE @st_code VARCHAR(30) /*used by st_mast or st_msuspend*/
DECLARE @st_desc VARCHAR(30) /*used by st_mast or st_msuspend*/
DECLARE @trx_type VARCHAR(30) /*used by st_trx*/
DECLARE @do_date DATETIME /*used by st_trx*/
DECLARE @quantity DECIMAL(13,2) /*used by st_trx*/
DECLARE @qtt_out DECIMAL(13,2) /*used by st_trx*/
DECLARE @unit_price DECIMAL(13,2) /*used by st_trx*/
DECLARE @discount VARCHAR(30) /*used by st_trx*/
DECLARE @total_price DECIMAL(13,2) /*used by st_trx*/
DECLARE @cost DECIMAL(13,2) /*used by st_mast or st_msuspend*/
DECLARE @aver_cost DECIMAL(13,2) /*used by st_mast or st_msuspend*/
DECLARE @x INT /*This is used for making No.*/

DECLARE @balance DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @prev_balance DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @MRcost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @WAcost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @total_cost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @total_wacost DECIMAL(13,2) /*This is used to calculate formula data*/

DECLARE @lybalance DECIMAL(13,2) /*This is used for search last year quantity balance*/

DECLARE @numberss INT /*This is used for setting rows number to making sure which row come in first*/
DECLARE @numberss2 INT /*This is used to loop in temp table*/
DECLARE @tmpquantity DECIMAL(13,2) /*This is used to retrieve quantity in temp table*/
DECLARE @tmpdo_date DATETIME /*This is used to retrieve date in temp table, later used to group by*/

DECLARE @mon00 DECIMAL(13,2) /*BEFORE THIS YEAR*/
DECLARE @mon1 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon2 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon3 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon4 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon5 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon6 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon7 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon8 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon9 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon10 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon11 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon12 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/

DECLARE STK_CURSOR CURSOR FOR
SELECT st_code, st_desc, cost, aver_cost
FROM st_mast
WHERE st_code>='' and st_code <='11111'

BEGIN
PRINT CAST('No.' as char(5))+CAST('st_code' as char(17))+CAST('st_desc' as char(30))+CAST('@mon12' as char(10))+CAST('@mon11' as char(10))+CAST('@mon10' as char(10))+CAST('@mon9' as char(10))+CAST('@mon8' as char(10))+CAST('@mon7' as char(10))+CAST('@mon6' as char(10))+CAST('@mon5' as char(10))+CAST('@mon4' as char(10))+CAST('@mon3' as char(10))+CAST('@mon2' as char(10))+CAST('@mon1' as char(10))+CAST('@mon00' as char(10))
+CAST('balance' as char(13))+CAST('MRcost' as char(10))+CAST('WAcost' as char(10))+CAST('total_cost' as char(10))+CAST('total_wacost' as char(10))


SET @x = 0

OPEN STK_CURSOR

FETCH NEXT FROM STK_CURSOR
INTO @st_code, @st_desc, @cost, @aver_cost

WHILE @@FETCH_STATUS = 0
BEGIN

/*----------CREATE TEMP TABLE FOR FIFO----------*/
CREATE TABLE #TempSt_trx(
numbers INT,
date DATETIME,
qty DECIMAL(13,2))
/*----------CREATE TEMP TABLE FOR FIFO----------*/

SET @x = @x + 1
SET @balance = 0
SET @prev_balance = 0
SET @MRcost = 0
SET @WAcost = 0
SET @lybalance = 0
SET @numberss = 1
SET @mon1 = 0
SET @mon2 = 0
SET @mon3 = 0
SET @mon4 = 0
SET @mon5 = 0
SET @mon6 = 0
SET @mon7 = 0
SET @mon8 = 0
SET @mon9 = 0
SET @mon10 = 0
SET @mon11 = 0
SET @mon12 = 0
SET @mon00 = 0
/*--------------------------------------GET BALANCE, MRCOST, WACOST, TOTALWACOST, TOTALMACOST--------------------------------------*/
DECLARE STK_CURSOR1 CURSOR FOR
SELECT trx_type, quantity, qtt_out, unit_price, discount, total_price, do_date
FROM st_trx
WHERE do_date<='2007-12-31 00:00:00.000' and st_code = @st_code
ORDER BY do_date

OPEN STK_CURSOR1

FETCH NEXT FROM STK_CURSOR1
INTO @trx_type, @quantity, @qtt_out, @unit_price, @discount, @total_price, @do_date

WHILE @@FETCH_STATUS = 0
BEGIN

SET @prev_balance = @balance
SET @balance = @balance + (@quantity - @qtt_out)

if @trx_type = 'PDO'
BEGIN

if @discount = '' or @discount IS NULL
SET @MRcost = @unit_price
else
SET @MRcost = NULLIF(@total_price,0)/NULLIF(@quantity,0)

if @prev_balance >=0
BEGIN
if @WAcost = 0
SET @WAcost = @MRcost
else
SET @WAcost = NULLIF((@prev_balance*@WAcost + @quantity*@MRcost),0)/NULLIF((@prev_balance+@quantity),0)
END
else
SET @WAcost = @MRcost

if @lybalance >= @quantity
SET @lybalance = @lybalance - @quantity
else
BEGIN
INSERT INTO #TempSt_trx VALUES(CONVERT(INT, @numberss), CONVERT(DATETIME,@do_date), CONVERT(DECIMAL(13,2), @quantity-@lybalance))
SET @numberss = @numberss + 1
SET @lybalance = 0
END

END

if @trx_type IN ('GRO','DO','INV','CS','DN','POS')
BEGIN
SET @lybalance = @lybalance + @qtt_out

DECLARE TEMP_CURSOR CURSOR FOR
SELECT qty FROM #TempSt_trx order by numbers

OPEN TEMP_CURSOR

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

SET @numberss2 = 1
WHILE @@FETCH_STATUS = 0
BEGIN

if @tmpquantity > @lybalance
BEGIN
UPDATE #TempSt_trx SET qty = @tmpquantity - @lybalance where numbers = CONVERT(int, @numberss2)
SET @lybalance = 0
BREAK
END
ELSE
BEGIN
SET @lybalance = @lybalance - @tmpquantity
UPDATE #TempSt_trx SET qty = 0 where numbers = CONVERT(int, @numberss2)
END

SET @numberss2 = @numberss2 + 1

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

END

CLOSE TEMP_CURSOR
DEALLOCATE TEMP_CURSOR
END

if @trx_type IN ('ADJ','CN')
BEGIN
if @quantity <0 or @quantity > 0
BEGIN
if @lybalance >= @quantity
SET @lybalance = @lybalance - @quantity
else
BEGIN
INSERT INTO #TempSt_trx VALUES(CONVERT(INT, @numberss), CONVERT(DATETIME,@do_date), CONVERT(DECIMAL(13,2), @quantity-@lybalance))
SET @numberss = @numberss + 1
SET @lybalance = 0
END

END
else
BEGIN
SET @lybalance = @lybalance + @qtt_out

DECLARE TEMP_CURSOR CURSOR FOR
SELECT qty FROM #TempSt_trx order by numbers

OPEN TEMP_CURSOR

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

SET @numberss2 = 1
WHILE @@FETCH_STATUS = 0
BEGIN

if @tmpquantity > @lybalance
BEGIN
UPDATE #TempSt_trx SET qty = @tmpquantity - @lybalance where numbers = CONVERT(int, @numberss2)
SET @lybalance = 0
BREAK
END
ELSE
BEGIN
SET @lybalance = @lybalance - @tmpquantity
UPDATE #TempSt_trx SET qty = 0 where numbers = CONVERT(int, @numberss2)
END

SET @numberss2 = @numberss2 + 1

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

END

CLOSE TEMP_CURSOR
DEALLOCATE TEMP_CURSOR
END
END

FETCH NEXT FROM STK_CURSOR1
INTO @trx_type, @quantity, @qtt_out, @unit_price, @discount, @total_price, @do_date
END
CLOSE STK_CURSOR1
DEALLOCATE STK_CURSOR1

/*-----MONTH JAN TO DEC-----*/
DECLARE RTEMP_CURSOR CURSOR FOR
SELECT [1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12], (SELECT SUM(qty)as mon00 FROM #TempSt_trx where date <'2007-01-01 00:00:00.000')as mon00here shd print sum event there is no record but it dunno y cant print out
FROM (
SELECT MONTH(date)as date, sum(qty) as qty FROM #TempSt_trx WHERE date>='2007-01-01 00:00:00.000' and date<='2007-12-31 00:00:00.000' GROUP BY date
)AS AA
PIVOT (
SUM(qty) FOR date IN ([1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12])
)AS pvt

OPEN RTEMP_CURSOR

FETCH NEXT FROM RTEMP_CURSOR
INTO @mon1, @mon2, @mon3, @mon4, @mon5, @mon6, @mon7, @mon8, @mon9, @mon10, @mon11, @mon12, @mon00

CLOSE RTEMP_CURSOR
DEALLOCATE RTEMP_CURSOR
/*-----MONTH JAN TO DEC-----*/


/*----------DROP FIFO TABLE----------*/
drop table #TempSt_trx
/*----------DROP FIFO TABLE----------*/

if @mon1 IS NULL
SET @mon1 = 0
if @mon2 IS NULL
SET @mon2 = 0
if @mon3 IS NULL
SET @mon3 = 0
if @mon4 IS NULL
SET @mon4 = 0
if @mon5 IS NULL
SET @mon5 = 0
if @mon6 IS NULL
SET @mon6 = 0
if @mon7 IS NULL
SET @mon7 = 0
if @mon8 IS NULL
SET @mon8 = 0
if @mon9 IS NULL
SET @mon9 = 0
if @mon10 IS NULL
SET @mon10 = 0
if @mon11 IS NULL
SET @mon11 = 0
if @mon12 IS NULL
SET @mon12 = 0
if @mon00 IS NULL
SET @mon00 = 0

IF @st_desc IS NULL
SET @st_desc = ' '
if @MRcost = 0
SET @MRcost = @cost
if @WAcost = 0
SET @WAcost = @aver_cost

SET @total_cost = @balance * @MRcost
SET @total_wacost = @balance * @WAcost

PRINT CAST(@x as char(5))+CAST(@st_code as char(17))+CAST(@st_desc as char(30))+CAST(@mon12 as char(10))+CAST(@mon11 as char(10))+CAST(@mon10 as char(10))+CAST(@mon9 as char(10))+CAST(@mon8 as char(10))+CAST(@mon7 as char(10))+CAST(@mon6 as char(10))+CAST(@mon5 as char(10))+CAST(@mon4 as char(10))+CAST(@mon3 as char(10))+CAST(@mon2 as char(10))+CAST(@mon1 as char(10))+CAST(@mon00 as char(10))
+CAST(@balance as char(13))+CAST(@MRcost as char(10))+CAST(@WAcost as char(10))+CAST(@total_cost as char(10))+CAST(@total_wacost as char(10))

FETCH NEXT FROM STK_CURSOR
INTO @st_code, @st_desc, @cost, @aver_cost
END
CLOSE STK_CURSOR
DEALLOCATE STK_CURSOR

END

Hi, i have just finish coded this sto pro WHICH INCLUDED First In First Out concept, however it is taking ages to show result, can any1 help me to improve it(spent 5 days in it)

[edit]-leftover 1 variable sorry

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-05 : 04:23:12
First rule of performance queries is to NOT USE CURSORS.
Post your table structure together with proper and accurate sample data.
Then also post your expected output.

We might be able to suggest a SET-BASED approach.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-05 : 05:20:00
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ST_MAST](
[ST_CODE] [VARCHAR(30)] NULL,
[ST_DESC] [VARCHAR(30)] NULL,
[COST] [DECIMAL(13,2)] NULL,
[AVER_COST] [DECIMAL(13,2)] NULL
) ON [PRIMARY]

INSERT INTO [dbo].[ST_MAST]([ST_CODE], [ST_DESC], [COST], [AVER_COST])
SELECT 1011, 'STOCK 1', 33, 23 UNION ALL
SELECT 1012, 'STOCK 2', 45, 56 UNION ALL
SELECT 1013, 'STOCK 3', 56, 19

CREATE TABLE [dbo].[ST_TRX](
[TRX_TYPE] [CHAR(3)] NULL,
[QUANTITY] [DECIMAL(13,2)] NULL,
[QTT_OUT] [DECIMAL(13,2)] NULL,
[UNIT_PRICE] [DECIMAL(13,2)] NULL,
[DISCOUNT] [DECIMAL(13,2)] NULL,
[TOTAL_PRICE] [DECIMAL(13,2)] NULL,
[DO_DATE] [DATETIME] NULL
) ON [PRIMARY]

INSERT INTO [dbo].[ST_TRX]([TRX_TYPE], [QUANTITY], [QTT_OUT], [UNIT_PRICE], [DISCOUNT], [TOTAL_PRICE], [DO_DATE])
SELECT DO, 0, 100, 453, ,'', 45 ,'2006-12-15 00:00:00.000', 1011 UNION ALL
SELECT PDO, 35, 0, 456, ,'', 87 ,'2006-12-16 00:00:00.000', 1011 UNION ALL
SELECT DO, 0, 5, 786, ,'', 38 ,'2006-12-17 00:00:00.000', 1011 UNION ALL
SELECT DO, 0, 10, 8, ,'', 5 ,'2006-12-18 00:00:00.000', 1011 UNION ALL
SELECT PDO, 100, 0, 78, ,'', 7 ,'2007-01-15 00:00:00.000', 1011 UNION ALL
SELECT DO, 0, 5, 5, ,'', 3 ,'2007-02-15 00:00:00.000', 1011 UNION ALL
SELECT DO, 0, 4, 7, ,'', 45 ,'2007-02-15 00:00:00.000', 1011 UNION ALL
SELECT PDO, 1, 0, 867, ,'', 1 ,'2007-03-15 00:00:00.000', 1011 UNION ALL
SELECT PDO, 5, 0, 6, ,10, 50 ,'2007-04-15 00:00:00.000', 1011 UNION ALL
SELECT DO, 0, 7, 5, ,'', 8 ,'2007-05-15 00:00:00.000', 1011 UNION ALL
SELECT DO, 0, 5, 45, ,'', 56 ,'2007-05-15 00:00:00.000', 1011




output require
--------------
No. st_code st_desc @mon12 @mon11 @mon10 @mon9 @mon8 @mon7 @mon6 @mon5 @mon4 @mon3 @mon2 @mon1 @mon00 balance MRcost WAcost total_costtotal_waco
1 1011 STOCK 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.00 0.00 0.00 0.00 0.00 5.00 33.00 23.00 165.00 115.00
2 1012 STOCK 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 45.00 56.00 0.00 0.00
3 1013 STOCK 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 56.00 19.00 0.00 0.00

sorry i was sripting this >"<, result might be not accurate coz i count my self
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-06 : 10:27:23
any1???helps?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-06 : 13:09:18
1. It's weekend
2. It's the National Day in Sweden (we celebrate)
3. Tomorrow is sunday
4. Day after tomorrow is monday

See you then.


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-07 : 06:28:25
quote:
Originally posted by waterduck

any1???helps?


can you explain what mon0,mon1,... represent? each months stock?
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-07 : 11:30:35
-> peso haha i would think that too, cause i also holiday-ing. can c tat many of pros din reply
-> visakh16 mon = month, mon00 = last year balance....this sto-pro main function is that it can allow user to know which month stock in balance after stock out...like

Jan - buy 1000 iphone
Feb - sell 50 iphone
Mac - buy 10 iphone

so the result would likely become
Mon1 Mon2 Mon3
950 0 10
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-07 : 13:34:09
so what you need is cummulative sales over months?
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-07 : 21:08:15
almost there, this sto-pro also accumulate those costing, which is affected by trx_type='PDO'
i searching for a more fast and efficient method to replace this stupid sto-pro created by me...
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-07 : 23:01:20
CREATE PROCEDURE TESTING (@ds char(1), @startdate datetime, @enddate datetime, @startcode varchar(30), @endcode varchar(30))
AS
DECLARE @st_code VARCHAR(30) /*used by st_mast or st_msuspend*/
DECLARE @st_desc VARCHAR(30) /*used by st_mast or st_msuspend*/
DECLARE @trx_type VARCHAR(30) /*used by st_trx*/
DECLARE @do_date DATETIME /*used by st_trx*/
DECLARE @quantity DECIMAL(13,2) /*used by st_trx*/
DECLARE @qtt_out DECIMAL(13,2) /*used by st_trx*/
DECLARE @unit_price DECIMAL(13,2) /*used by st_trx*/
DECLARE @discount VARCHAR(30) /*used by st_trx*/
DECLARE @total_price DECIMAL(13,2) /*used by st_trx*/
DECLARE @cost DECIMAL(13,2) /*used by st_mast or st_msuspend*/
DECLARE @aver_cost DECIMAL(13,2) /*used by st_mast or st_msuspend*/
DECLARE @x INT /*This is used for making No.*/

DECLARE @balance DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @prev_balance DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @MRcost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @WAcost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @total_cost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @total_wacost DECIMAL(13,2) /*This is used to calculate formula data*/
DECLARE @total_atcost DECIMAL(13,2) /*This is used to calculate formula data in temp table*/

DECLARE @lybalance DECIMAL(13,2) /*This is used for search last year quantity balance*/

DECLARE @numberss INT /*This is used for setting rows number to making sure which row come in first*/
DECLARE @numberss2 INT /*This is used to loop in temp table*/
DECLARE @tmpquantity DECIMAL(13,2) /*This is used to retrieve quantity in temp table*/
DECLARE @tmpdo_date DATETIME /*This is used to retrieve date in temp table, later used to group by*/

DECLARE @mon00 DECIMAL(13,2) /*BEFORE THIS YEAR*/
DECLARE @mon1 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon2 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon3 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon4 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon5 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon6 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon7 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon8 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon9 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon10 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon11 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
DECLARE @mon12 DECIMAL(13,2) /*after pivot slot into variable to show out month by month*/
if @ds = 'a'
BEGIN
DECLARE STK_CURSOR CURSOR FOR
SELECT st_code, st_desc, cost, aver_cost
FROM st_mast
WHERE st_code>=@startcode and st_code <=@endcode
END
if @ds = 'i'
BEGIN
DECLARE STK_CURSOR CURSOR FOR
SELECT m.st_code, m.st_desc, m.cost, m.aver_cost
FROM (select * from st_mast union select * from st_msuspend)m
WHERE m.st_code>=@startcode and m.st_code <=@endcode
END
if @ds = 'o'
BEGIN
DECLARE STK_CURSOR CURSOR FOR
SELECT st_code, st_desc, cost, aver_cost
FROM st_msuspend
WHERE st_code>=@startcode and st_code <=@endcode
END

BEGIN
PRINT CAST('No.' as char(5))+CAST('st_code' as char(17))+CAST('st_desc' as char(30))+CAST('@mon12' as char(10))+CAST('@mon11' as char(10))+CAST('@mon10' as char(10))+CAST('@mon9' as char(10))+CAST('@mon8' as char(10))+CAST('@mon7' as char(10))+CAST('@mon6' as char(10))+CAST('@mon5' as char(10))+CAST('@mon4' as char(10))+CAST('@mon3' as char(10))+CAST('@mon2' as char(10))+CAST('@mon1' as char(10))+CAST('@mon00' as char(10))
+CAST('balance' as char(13))+CAST('MRcost' as char(10))+CAST('WAcost' as char(10))+CAST('total_cost' as char(10))+CAST('total_wacost' as char(10))+CAST('total_atcost' as char(10))


SET @x = 0

OPEN STK_CURSOR

FETCH NEXT FROM STK_CURSOR
INTO @st_code, @st_desc, @cost, @aver_cost

WHILE @@FETCH_STATUS = 0
BEGIN

/*----------CREATE TEMP TABLE FOR FIFO----------*/
CREATE TABLE #TempSt_trx(
numbers INT,
date DATETIME,
qty DECIMAL(13,2),
COST DECIMAL(13,2))
/*----------CREATE TEMP TABLE FOR FIFO----------*/

SET @x = @x + 1
SET @balance = 0
SET @prev_balance = 0
SET @MRcost = 0
SET @WAcost = 0
SET @lybalance = 0
SET @numberss = 1
SET @mon1 = 0
SET @mon2 = 0
SET @mon3 = 0
SET @mon4 = 0
SET @mon5 = 0
SET @mon6 = 0
SET @mon7 = 0
SET @mon8 = 0
SET @mon9 = 0
SET @mon10 = 0
SET @mon11 = 0
SET @mon12 = 0
SET @mon00 = 0
/*--------------------------------------GET BALANCE, MRCOST, WACOST, TOTALWACOST, TOTALMACOST--------------------------------------*/
DECLARE STK_CURSOR1 CURSOR FOR
SELECT trx_type, quantity, qtt_out, unit_price, discount, total_price, do_date
FROM st_trx
WHERE do_date<=@enddate and st_code = @st_code
ORDER BY do_date

OPEN STK_CURSOR1

FETCH NEXT FROM STK_CURSOR1
INTO @trx_type, @quantity, @qtt_out, @unit_price, @discount, @total_price, @do_date

WHILE @@FETCH_STATUS = 0
BEGIN
SET @prev_balance = @balance
SET @balance = @balance + (@quantity - @qtt_out)

if @trx_type = 'PDO'
BEGIN

if @discount = '' or @discount IS NULL
SET @MRcost = @unit_price
else
SET @MRcost = NULLIF(@total_price,0)/NULLIF(@quantity,0)

if @prev_balance >=0
BEGIN
if @WAcost = 0
SET @WAcost = @MRcost
else
SET @WAcost = NULLIF((@prev_balance*@WAcost + @quantity*@MRcost),0)/NULLIF((@prev_balance+@quantity),0)
END
else
SET @WAcost = @MRcost

if @lybalance >= @quantity
SET @lybalance = @lybalance - @quantity
else
BEGIN
INSERT INTO #TempSt_trx VALUES(CONVERT(INT, @numberss), CONVERT(DATETIME,@do_date), CONVERT(DECIMAL(13,2), @quantity-@lybalance), @MRcost)
SET @numberss = @numberss + 1
SET @lybalance = 0
END
END

if @trx_type IN ('GRO','DO','INV','CS','DN','POS')
BEGIN
SET @lybalance = @lybalance + @qtt_out

DECLARE TEMP_CURSOR CURSOR FOR
SELECT qty FROM #TempSt_trx order by numbers

OPEN TEMP_CURSOR

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

SET @numberss2 = 1
WHILE @@FETCH_STATUS = 0
BEGIN

if @tmpquantity > @lybalance
BEGIN
UPDATE #TempSt_trx SET qty = @tmpquantity - @lybalance where numbers = CONVERT(int, @numberss2)
SET @lybalance = 0
BREAK
END
ELSE
BEGIN
SET @lybalance = @lybalance - @tmpquantity
UPDATE #TempSt_trx SET qty = 0 where numbers = CONVERT(int, @numberss2)
END

SET @numberss2 = @numberss2 + 1

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

END

CLOSE TEMP_CURSOR
DEALLOCATE TEMP_CURSOR
END

if @trx_type IN ('ADJ','CN')
BEGIN
if @quantity <0 or @quantity > 0
BEGIN
if @lybalance >= @quantity
SET @lybalance = @lybalance - @quantity
else
BEGIN
INSERT INTO #TempSt_trx VALUES(CONVERT(INT, @numberss), CONVERT(DATETIME,@do_date), CONVERT(DECIMAL(13,2), @quantity-@lybalance), @MRcost)
SET @numberss = @numberss + 1
SET @lybalance = 0
END
END
else
BEGIN
SET @lybalance = @lybalance + @qtt_out

DECLARE TEMP_CURSOR CURSOR FOR
SELECT qty FROM #TempSt_trx order by numbers

OPEN TEMP_CURSOR

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity

SET @numberss2 = 1
WHILE @@FETCH_STATUS = 0
BEGIN
if @tmpquantity > @lybalance
BEGIN
UPDATE #TempSt_trx SET qty = @tmpquantity - @lybalance where numbers = CONVERT(int, @numberss2)
SET @lybalance = 0
BREAK
END
ELSE
BEGIN
SET @lybalance = @lybalance - @tmpquantity
UPDATE #TempSt_trx SET qty = 0 where numbers = CONVERT(int, @numberss2)
END

SET @numberss2 = @numberss2 + 1

FETCH NEXT FROM TEMP_CURSOR
INTO @tmpquantity
END

CLOSE TEMP_CURSOR
DEALLOCATE TEMP_CURSOR
END
END

FETCH NEXT FROM STK_CURSOR1
INTO @trx_type, @quantity, @qtt_out, @unit_price, @discount, @total_price, @do_date
END
CLOSE STK_CURSOR1
DEALLOCATE STK_CURSOR1

/*-----MONTH JAN TO DEC-----*/
DECLARE RTEMP_CURSOR CURSOR FOR
SELECT [1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12],[13]
FROM (
SELECT * FROM(
SELECT MONTH(date)as date, sum(qty) as qty
FROM #TempSt_trx
WHERE date>=@startdate and date<=@enddate
GROUP BY date
UNION
SELECT 13 as date, SUM(qty)as qty FROM #TempSt_trx where date < @startdate)ABC
)AS AA
PIVOT (
SUM(qty) FOR date IN ([1],[2],[3],[4],[5],[6],[7],,[9],[10],[11],[12],[13])
)AS pvt

OPEN RTEMP_CURSOR

FETCH NEXT FROM RTEMP_CURSOR
INTO @mon1, @mon2, @mon3, @mon4, @mon5, @mon6, @mon7, @mon8, @mon9, @mon10, @mon11, @mon12, @mon00

CLOSE RTEMP_CURSOR
DEALLOCATE RTEMP_CURSOR
/*-----MONTH JAN TO DEC-----*/

/*-----TotalAT_Cost-----*/
DECLARE RTEMP_CURSOR CURSOR FOR
SELECT SUM(qty*cost) as totalat_cost
FROM #TempSt_trx

OPEN RTEMP_CURSOR

FETCH NEXT FROM RTEMP_CURSOR
INTO @total_atcost

CLOSE RTEMP_CURSOR
DEALLOCATE RTEMP_CURSOR
/*-----TotalAT_Cost-----*/

/*----------DROP FIFO TABLE----------*/
drop table #TempSt_trx
/*----------DROP FIFO TABLE----------*/

if @mon1 IS NULL
SET @mon1 = 0
if @mon2 IS NULL
SET @mon2 = 0
if @mon3 IS NULL
SET @mon3 = 0
if @mon4 IS NULL
SET @mon4 = 0
if @mon5 IS NULL
SET @mon5 = 0
if @mon6 IS NULL
SET @mon6 = 0
if @mon7 IS NULL
SET @mon7 = 0
if @mon8 IS NULL
SET @mon8 = 0
if @mon9 IS NULL
SET @mon9 = 0
if @mon10 IS NULL
SET @mon10 = 0
if @mon11 IS NULL
SET @mon11 = 0
if @mon12 IS NULL
SET @mon12 = 0
if @mon00 IS NULL
SET @mon00 = 0

IF @st_desc IS NULL
SET @st_desc = ' '
if @MRcost = 0
SET @MRcost = @cost
if @WAcost = 0
SET @WAcost = @aver_cost
if @total_atcost IS NULL
SET @total_atcost = 0

SET @total_cost = @balance * @MRcost
SET @total_wacost = @balance * @WAcost

PRINT CAST(@x as char(5))+CAST(@st_code as char(17))+CAST(@st_desc as char(30))+CAST(@mon12 as char(10))+CAST(@mon11 as char(10))+CAST(@mon10 as char(10))+CAST(@mon9 as char(10))+CAST(@mon8 as char(10))+CAST(@mon7 as char(10))+CAST(@mon6 as char(10))+CAST(@mon5 as char(10))+CAST(@mon4 as char(10))+CAST(@mon3 as char(10))+CAST(@mon2 as char(10))+CAST(@mon1 as char(10))+CAST(@mon00 as char(10))
+CAST(@balance as char(13))+CAST(@MRcost as char(10))+CAST(@WAcost as char(10))+CAST(@total_cost as char(10))+CAST(@total_wacost as char(10))+CAST(@total_atcost as char(10))

FETCH NEXT FROM STK_CURSOR
INTO @st_code, @st_desc, @cost, @aver_cost
END
CLOSE STK_CURSOR
DEALLOCATE STK_CURSOR

END


Updated sto-pro, please refer to this sto-pro, btw how to put 'tab' in forum?
{edit}-add in tab for better views
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-07 : 23:12:30
use the [ code ] [ /code ] tag with any space in between the [ ]. Refer to the forum faq http://www.sqlteam.com/forums/faq.asp


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-07 : 23:25:08
can you check the DDL & DML you posted on Posted - 06/05/2009 : 05:20:00. There are some error and mix-match on the columns in the INSERT statement.


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-07 : 23:38:28
CREATE TABLE [dbo].[ST_MAST](
[ST_CODE] VARCHAR(30) NULL,
[ST_DESC] VARCHAR(30) NULL,
[COST] DECIMAL(13,2) NULL,
[AVER_COST] DECIMAL(13,2) NULL
) ON [PRIMARY]

INSERT INTO [dbo].[ST_MAST]([ST_CODE], [ST_DESC], [COST], [AVER_COST])
SELECT 1011, 'STOCK 1', 33, 23 UNION ALL
SELECT 1012, 'STOCK 2', 45, 56 UNION ALL
SELECT 1013, 'STOCK 3', 56, 19

CREATE TABLE [dbo].[ST_TRX](
[TRX_TYPE] CHAR(3) NULL,
[QUANTITY] DECIMAL(13,2) NULL,
[QTT_OUT] DECIMAL(13,2) NULL,
[UNIT_PRICE] DECIMAL(13,2) NULL,
[DISCOUNT] DECIMAL(13,2) NULL,
[TOTAL_PRICE] DECIMAL(13,2) NULL,
[DO_DATE] DATETIME NULL,
[ST_CODE] VARCHAR(30) NULL
) ON [PRIMARY]

INSERT INTO [dbo].[ST_TRX]([TRX_TYPE], [QUANTITY], [QTT_OUT], [UNIT_PRICE], [DISCOUNT], [TOTAL_PRICE], [DO_DATE], [st_code])
SELECT 'DO', 0, 100, 453 ,'', 45 ,'2006-12-15 00:00:00.000', 1011 UNION ALL
SELECT 'PDO', 35, 0, 456 ,'', 87 ,'2006-12-16 00:00:00.000', 1011 UNION ALL
SELECT 'DO', 0, 5, 786 ,'', 38 ,'2006-12-17 00:00:00.000', 1011 UNION ALL
SELECT 'DO', 0, 10, 8 ,'', 5 ,'2006-12-18 00:00:00.000', 1011 UNION ALL
SELECT 'PDO', 100, 0, 78 ,'', 7 ,'2007-01-15 00:00:00.000', 1011 UNION ALL
SELECT 'DO', 0, 5, 5 ,'', 3 ,'2007-02-15 00:00:00.000', 1011 UNION ALL
SELECT 'DO', 0, 4, 7 ,'', 45 ,'2007-02-15 00:00:00.000', 1011 UNION ALL
SELECT 'PDO', 1, 0, 867 ,'', 1 ,'2007-03-15 00:00:00.000', 1011 UNION ALL
SELECT 'PDO', 5, 0, 6 ,10, 50 ,'2007-04-15 00:00:00.000', 1011 UNION ALL
SELECT 'DO', 0, 7, 5 ,'', 8 ,'2007-05-15 00:00:00.000', 1011 UNION ALL
SELECT 'DO', 0, 5, 45 ,'', 56 ,'2007-05-15 00:00:00.000', 1011

My mistake, please forgive me....
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-08 : 00:41:35
any1 can lead me to minimize exec time or guide me to create a more simple procedure to meet the requirement?
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-06-08 : 02:10:28
And what is the expected result/output from the sample data posted 06/07/2009 : 23:38:28?


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-08 : 02:20:27
[code]
No. st_code st_desc @mon12 @mon11 @mon10 @mon9 @mon8 @mon7 @mon6 @mon5 @mon4 @mon3 @mon2 @mon1 @mon00 balance MRcost WAcost total_costtotal_wacototal_atco
1 1011 STOCK 1 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 5.00 0.00 0.00 0.00 0.00 5.00 10.00 3.05 50.00 15.25 50.00
2 1012 STOCK 2 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 45.00 56.00 0.00 0.00 0.00
3 1013 STOCK 3 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 56.00 19.00 0.00 0.00 0.00
[/code]
all the calculation done by the sto-pro is correct but i wanna learn sometime new or a new method to replace this time consuming sto-pro
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-08 : 04:24:32
the reason i use cursor is because i need to use print...my comp printing process goes by using print function to print the records into variety of format....
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-08 : 04:44:57
what's the purpose of using PRINT to show the result ? It's rather unconventional to do that.

What's the front end client that you are using ? Or is it a reporting tool ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-08 : 04:55:30
Comp using clarion 6 to do the job process goes by
allow user to select range of value->select report format(summary, detail, etc)->select report format(pdf, notepad, exel, etc)
don ask me y...i jz a trainee...
This also means that the format of report is stored in sto-pro, by select which sto-pro, the report can be generated
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-08 : 21:22:52
any helps?
Go to Top of Page

waterduck
Aged Yak Warrior

982 Posts

Posted - 2009-06-09 : 05:40:46
can any1 help me turn my sto-pro into set-based instead of cursor?
Go to Top of Page
    Next Page

- Advertisement -