Thankyou TheJon,
I am trying to convert our stored procedures to use temp variables instead of temp tables (since MS reporting services does not support temp tables I guess).
Here is my first attempt and I am getting the error must declare @Summary, even though I have declared it, this is only after update statement, if I just try the code until marked line, it does not give any syntax error.
Please take a look at this.
--------------------------------------
CREATE PROCEDURE MSRS_OnHand
@Season varchar(35)
AS
declare @summary TABLE
( prodgroup varchar(35),
barcode varchar(15),
style varchar(15),
description varchar(35),
colour varchar(20),
siz varchar(15),
qty int,
Cost money,
retail money,
margin float,
retailvalue money,
costvalue money
)
insert into @summary
select
g.prodgroup,
q.Barcode,
g.Style,
g.Description,
b.Colour,
b.siz as Size,
SUM(q.quantity),
ISNULL(p.LastCost,ISNULL(p.OriginalCost,0)) as Cost,
p.Retail1 ,
0 as Margin,
0 as TotRetailValue,
0 as TotCostvalue
from
quantity q inner join branch br on
q.branch=br.name inner join barcode b on
q.barcode = b.barcode inner join garment g on
b.style = g.style inner join pricing p on
g.style = p.style and
b.pricepoint = p.pricepoint
where
p.region = 'New Zealand' and
br.retailbranch='Y' and
q.branch <> 'CENTRAL DISTRIBUTION CENTRE' and
q.quantity > 0 and
g.season = @Season
GROUP BY g.prodgroup,
q.Barcode,
g.Style,
g.Description,
b.Colour,
b.siz ,
p.retail1,
p.Lastcost,
p.OriginalCost
--------------------------------------error after this
UPDATE @summary
SET qty = @summary.qty+cdc.OnHandCDC
FROM
(SELECT
Barcode,
Sum(Quantity) AS OnHandCDC
FROM
QUANTITY
WHERE
Branch = 'CENTRAL DISTRIBUTION CENTRE'
GROUP BY Barcode) as cdc
WHERE @summary.barcode = cdc.barcode
UPDATE @summary
SET qty = @summary.qty+it.InTransit
FROM
(SELECT
td.Barcode,
Sum(IsNull(td.Quantity,0)) InTransit
FROM
TranHeader th INNER JOIN TranDetail td on
td.Trandate=th.Trandate and
td.Documentnum=th.Documentnum and
td.Type=th.Type
WHERE
th.Type='TFO' and
th.TransferReceived is null
GROUP BY
td.Barcode) as it
WHERE @summary.barcode = it.barcode
select * from @summary
order by prodgroup,barcode
GO
---------------------------------