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)
 Upadting multiple rows using loop

Author  Topic 

abhit_kumar
Posting Yak Master

147 Posts

Posted - 2010-02-03 : 06:23:52
Hello Frnds,

Currently the below code is working but not works for Rest qty column, because i think the when inserting into temp table is inserting data in not grouping manner.

so when looping case is not porperly, and rest qty col brings wrong data.




IF OBJECT_ID('spFluctuationPaidBill') IS NOT NULL
DROP PROC spFluctuationPaidBill
GO

CREATE PROCEDURE spFluctuationPaidBill
@FromDate DateTime,
@ToDate DateTime
AS
BEGIN

SET NOCOUNT ON;

CREATE TABLE #FluctuationTable(
ID INT IDENTITY(1,1),ItemCode nvarchar(100),ItemDesc nvarchar(100),OnHandQty int,GRPONo int,
GRPODate datetime,ICRate decimal(18,12),GRPOQty int,RestQty int,RatePerQty decimal(18,12),APInvNo int,
APInvDate datetime,OGRate decimal(18,12))

INSERT INTO #FluctuationTable
SELECT OITM.ItemCode,OITM.ItemName,sum(OITW.onhand) as onhand,abc.basenum as GRPONo, abc.docdate as GRPODate,
(select top 1 docrate from opdn where docentry = abc.baseentry and docnum = abc.basenum) as GRPORate,
(select sum(quantity) from pdn1 where docentry = abc.baseentry and abc.itemcode = itemcode) As GRPOQty,null,

(select top 1 price from pdn1 where docentry = abc.baseentry and abc.itemcode = itemcode) As GRPOUnitPrice,

(select distinct top 1 opch.docnum from opch inner join pch1 on opch.docentry = pch1.docentry
where pch1.baseentry = abc.baseentry) as APInvNo,

(select distinct top 1 opch.docdate from opch inner join pch1 on opch.docentry = pch1.docentry
where pch1.baseentry = abc.baseentry) as APInvDt,
(select distinct top 1 opch.u_dodamt from opch inner join pch1 on opch.docentry = pch1.docentry
where pch1.baseentry = abc.baseentry) as APInvDocRate

FROM OITW

INNER JOIN OITM ON OITW.ItemCode = OITM.ItemCode
INNER JOIN OITB ON OITM.ItmsGrpCod = OITB.ItmsGrpCod
left join sri1 as abc on OITW.ItemCode = abc.ItemCode

WHERE(OITW.OnHand <> 0 Or oitw.iscommited <> 0 Or oitw.onorder <> 0 Or oitw.minstock <> 0)
AND (OITW.ItemCode IN (SELECT pdn1.ItemCode FROM Opdn

inner join pdn1 on opdn.docentry = pdn1.docentry
where opdn.series = 314 or opdn.series = 94 or opdn.series = 663))and (abc.Direction = 0)
AND (abc.LineNum =(SELECT MAX(LineNum) AS Expr1 FROM SRI1
WHERE (ItemCode = abc.ItemCode) AND (SysSerial = abc.SysSerial))) AND basetype = 20
and abc.docdate >= (@FromDate) and abc.docdate <= (@ToDate)

group by OITM.ItemCode,OITM.ItemName, oitm.suppcatnum,
abc.basenum, abc.sysserial, abc.baseentry, abc.basenum, abc.docdate,
oitm.ItmsGrpcod, oitb.ItmsGrpNam, abc.baseentry, abc.SysSerial, abc.ItemCode,oitw.itemcode
order by OITM.ItemName,abc.basenum

DECLARE @ID INT
DECLARE @COUNT INT
DECLARE @ITEM VARCHAR(20)
DECLARE @GRPO INT
DECLARE @TOTAL INT
DECLARE @SUM INT
SELECT @COUNT = COUNT(*) FROM #FluctuationTable
SET @ID =1


select @COUNT


WHILE (@ID <= @COUNT)
BEGIN
SELECT @ITEM=ItemDesc, @GRPO=GRPOQty,@TOTAL=OnHandQty FROM #FluctuationTable WHERE ID=@ID

IF (@TOTAL > @GRPO)
UPDATE #FluctuationTable SET RestQty = @GRPO WHERE ID=@ID
ELSE
BEGIN

select SUM(GRPOQty) FROM #FluctuationTable WHERE ItemDesc=@ITEM
AND OnHandQty=@TOTAL AND GRPOQty<>@GRPO AND ID > @ID

select SUM(GRPOQty) FROM #FluctuationTable where OnHandQty=28 and GRPOQty<>40 AND ID > @ID

UPDATE #FluctuationTable SET RestQty = (OnHandQty-@SUM) WHERE ID=@ID
END
SET @ID = @ID + 1
END


select itemcode,ItemDesc,OnHandQty,GRPONo,GRPOQty,RestQty from #FluctuationTable
group by itemcode,ItemDesc,OnHandQty,GRPONo,GRPOQty,RestQty order by itemcode,GRPONo

END
GO

EXEC spFluctuationPaidBill '05/01/2009','12/01/2009'



Based upon item wise i have displayed Total stock qty and

then item wise GRPO details with Qty data.

I have created Stored procedure for this.

Now i have kept one column RestQty which is basically used to
display rest quantity based upon the data of Total Stock Qty and GRPO quantity.

To Implement this logic i have used loop condition but its not returning correct
qty on Rest Qty column.

Rest Qty should work like this:-

Item aaaaa

If Total Quantity = 28

GRPO Quantity = 40(First GRPO Qty),
7(second last GRPO Qty), &
3 (Last GRPO Quantity)



Now Total quantity always first check with Last GRPo Qty,
it means 28 > 3 then 3 comes to Rest Qty col.

then Total quantity check with second Last GRPo Qty,
it means 28 > 7 + 3(Last GRPo Qty) then 7 comes to Rest qty col.

then Total quantity first check with first GRPo Qty,
it means 28 > 40(first grpo qty) + 7 + 3, its not satisfied,

hence it should display 28 - (7+3) = 18 in rest qty col.


if we sum Rest qty column for Item aaaa then it should equal to Total Qty for particular item.

so its should work for Item aaaa, based upon this other items should work like this.

so i only need to correct the data of Rest Qty col, other are ok.

Please help me to correct this query.

Regards,

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-03 : 06:30:04
as i suggested you yesterday you need to have some column in your table which is unique valued to determine first and last records for restqty calculation. sql table does not have concept of first or last unless you specify it explicitly by means of order by. And of course once thats identifies you dont need loop to update rest qty. it can be done using set based approach
Go to Top of Page
   

- Advertisement -