I have a Table TBLmaterial where current Stock is defined for particular Materailid .
And i have a tbldrawingtable where required quantity is listed for a particular material . It should update present column as required quantity and status to Available if current stock is available .
for example : Materialid =2 have 4 requiredQTY in 102=drawid and 2 requiredQTY in 105=drawid . IT should update because total 6 CurrentStock is present for the material id 2.
As per latest update of code .below result will displayed which is correct.
If i check for material id 1 once again . it should update drawing id 107 for the material id 1 to Not available because no more current stock is available to update . my code below :
Alter procedure dbo.FetchInto(@Matid int)
as
begin
declare @CurrentStock int
declare @required Int
declare @present int
select @CurrentStock=M.CurrentStock
from Tblmaterial M
inner join
TblDrawingTable D
on M.Matid=D.Matid
where M.Matid =@Matid
select top 1 @required=d.RequiredQty from Tblmaterial M
inner join
TblDrawingTable D
on M.Matid=D.Matid
where m.Matid =@Matid
select top 1 @present=(m.CurrentStock-isnull(d.Present,0))
from TblDrawingTable D
inner join
Tblmaterial M
on D.Matid=m.Matid
where D.Matid=@Matid
if exists(select 1 from TblDrawingTable where Matid=@Matid and Present is
null)
begin
if (@required<=@CurrentStock and @present >0)
UPDATE TblDrawingTable SET present=@required,status='Available' where Drawid
= (select top 1 Drawid from TblDrawingTable where
Matid=@Matid) and Matid=@Matid
end
else if(@present=@required)
update TblDrawingTable SET status='Not Available',Present=@present where
Drawid <> (select top 1 Drawid from TblDrawingTable where
Matid=@Matid) and Matid=@Matid
if exists(select 1 from TblDrawingTable where Matid=@Matid and Present is
null)
begin
if (@required<=@CurrentStock and @present>0)
UPDATE TblDrawingTable SET present=(@CurrentStock-
@required),status='Available' where Drawid <> (select top 1 Drawid from
TblDrawingTable where
Matid=@Matid) and Matid=@Matid
end
else if(@present=@required)
update TblDrawingTable SET status='Not Available',Present=@present where
Matid=@Matid
end