Jamie, thank you for your reply.
When I run the SQL in Query Analyzer it seems to run
fine. I'm not sure why it wouldn't create a resultset.
Does it not create a recordset to be returned to excel
when there is definitely are query returned in Query
Analzyer at the SQLSERVER? I'm not sure anymore I guess:
Here is the SQL:
SET NOCOUNT ON
SET ANSI_WARNINGS OFF
USE m2mdata01
DECLARE @IntCurrMonth INT
DECLARE @IntYearBeforeLast INT
DECLARE @IntLastYear INT
DECLARE @IntThisYear INT
DECLARE @DateSepFirstYearBeforeLast DATETIME
DECLARE @DateAugThirtyFirstLastYear DATETIME
DECLARE @DateSepFirstLastYear DATETIME
DECLARE @DateAugThirtyFirstThisYear DATETIME
SET @IntCurrMonth=MONTH(CURRENT_TIMESTAMP)
SET @IntYearBeforeLast=YEAR(DATEADD(YEAR,-
2,CURRENT_TIMESTAMP))
SET @IntLastYear=YEAR(DATEADD(YEAR,-1,CURRENT_TIMESTAMP))
SET @IntThisYear=YEAR(CURRENT_TIMESTAMP)
SET @DateSepFirstYearBeforeLast=CAST(SUBSTRING(CONVERT
(CHAR(8),DATEADD(YEAR,-2,CURRENT_TIMESTAMP),112),1,4)
+'0901' AS DATETIME)
SET @DateAugThirtyFirstLastYear=CAST(SUBSTRING(CONVERT
(CHAR(8),DATEADD(YEAR,-1,CURRENT_TIMESTAMP),112),1,4)
+'0831' AS DATETIME)
SET @DateSepFirstLastYear=CAST(SUBSTRING(CONVERT(CHAR
(8),DATEADD(YEAR,-1,CURRENT_TIMESTAMP),112),1,4)+'0901'
AS DATETIME)
SET @DateAugThirtyFirstThisYear=CAST(SUBSTRING(CONVERT
(CHAR(8),CURRENT_TIMESTAMP,112),1,4)+'0831' AS DATETIME)
IF @IntCurrMonth BETWEEN 1 AND 8
BEGIN
SELECT ProdClass=Inprod.fpc_name,
Part_Num=Aritem.fpartno,
Sep_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 9 AND YEAR(Armast.finvdate) = @IntYearBeforeLast THEN
Aritem.ftotprice END), 0.00),
Sep_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 9 AND YEAR(Armast.finvdate) =
@IntYearBeforeLast THEN
(Aritem.fshipqty*Inmast.fnusrqty1) END), 0)),
Oct_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 10 AND YEAR(Armast.finvdate) = @IntYearBeforeLast THEN
Aritem.ftotprice END), 0.00),
Oct_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 10 AND YEAR(Armast.finvdate) =
@IntYearBeforeLast THEN
(Aritem.fshipqty*Inmast.fnusrqty1) END), 0)),
Nov_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 11 AND YEAR(Armast.finvdate) = @IntYearBeforeLast THEN
Aritem.ftotprice END), 0.00),
Nov_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 11 AND YEAR(Armast.finvdate) =
@IntYearBeforeLast THEN
(Aritem.fshipqty*Inmast.fnusrqty1) END), 0)),
Dec_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 12 AND YEAR(Armast.finvdate) = @IntYearBeforeLast THEN
Aritem.ftotprice END), 0.00),
Dec_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 12 AND YEAR(Armast.finvdate) =
@IntYearBeforeLast THEN
(Aritem.fshipqty*Inmast.fnusrqty1) END), 0)),
Jan_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 1 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Jan_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 1 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Feb_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 2 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Feb_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 2 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Mar_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 3 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Mar_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 3 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Apr_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 4 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Apr_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 4 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
May_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 5 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
May_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 5 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Jun_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 6 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Jun_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 6 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Jul_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 7 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Jul_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 7 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Aug_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 8 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Aug_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 8 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Tot_Sales=ISNULL(SUM(CASE WHEN Armast.finvdate
BETWEEN @DateSepFirstYearBeforeLast AND
@DateAugThirtyFirstLastYear THEN Aritem.ftotprice END),
0.00),
Tot_Units=FLOOR(ISNUll(SUM(CASE WHEN Armast.finvdate
BETWEEN @DateSepFirstYearBeforeLast AND
@DateAugThirtyFirstLastYear THEN
(Aritem.fshipqty*Inmast.fnusrqty1) END), 0))
FROM Armast JOIN Aritem ON (UPPER(Armast.fcinvoice)
=UPPER(Aritem.fcinvoice))
JOIN Inmast ON (Aritem.fac=Inmast.fac AND
Aritem.fpartno=Inmast.fpartno AND Aritem.frev=Inmast.frev)
JOIN Inprod ON (Inmast.fprodcl=Inprod.fpc_number)
WHERE (Armast.finvtype='N') AND (Armast.fcStatus='U'
OR Armast.fcStatus='P' OR Armast.fcStatus='F')
AND (LEFT(Inprod.fpc_name,2)<>'RM') AND
(Inprod.fpc_name<>'RAW MATERIAL')
AND Aritem.fpartno >='99000000' AND Aritem.fpartno
<= '99999999'
GROUP BY Inprod.fpc_name, Aritem.fpartno
WITH ROLLUP
ORDER BY GROUPING(Inprod.fpc_name), Inprod.fpc_name,
GROUPING(Aritem.fpartno), Aritem.fpartno
END
ELSE IF @IntCurrMonth BETWEEN 9 AND 12
BEGIN
SELECT ProdClass=Inprod.fpc_name,
Part_Num=Aritem.fpartno,
Sep_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 9 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Sep_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 9 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Oct_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 10 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Oct_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 10 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Nov_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 11 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Nov_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 11 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Dec_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 12 AND YEAR(Armast.finvdate) = @IntLastYear THEN
Aritem.ftotprice END), 0.00),
Dec_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 12 AND YEAR(Armast.finvdate) =
@IntLastYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Jan_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 1 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Jan_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 1 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Feb_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 2 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Feb_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 2 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Mar_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 3 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Mar_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 3 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Apr_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 4 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Apr_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 4 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
May_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 5 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
May_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 5 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Jun_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 6 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Jun_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 6 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Jul_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 7 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Jul_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 7 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Aug_Sales=ISNULL(SUM(CASE WHEN MONTH(Armast.finvdate)
= 8 AND YEAR(Armast.finvdate) = @IntThisYear THEN
Aritem.ftotprice END), 0.00),
Aug_Units=FLOOR(ISNULL(SUM(CASE WHEN MONTH
(Armast.finvdate) = 8 AND YEAR(Armast.finvdate) =
@IntThisYear THEN (Aritem.fshipqty*Inmast.fnusrqty1)
END), 0)),
Tot_Sales=ISNULL(SUM(CASE WHEN Armast.finvdate
BETWEEN @DateSepFirstLastYear AND
@DateAugThirtyFirstThisYear THEN Aritem.ftotprice END),
0.00),
Tot_Units=FLOOR(ISNUll(SUM(CASE WHEN Armast.finvdate
BETWEEN @DateSepFirstLastYear AND
@DateAugThirtyFirstThisYear THEN
(Aritem.fshipqty*Inmast.fnusrqty1) END), 0))
FROM Armast JOIN Aritem ON (UPPER(Armast.fcinvoice)
=UPPER(Aritem.fcinvoice))
JOIN Inmast ON (Aritem.fac=Inmast.fac AND
Aritem.fpartno=Inmast.fpartno AND Aritem.frev=Inmast.frev)
JOIN Inprod ON (Inmast.fprodcl=Inprod.fpc_number)
WHERE (Armast.finvtype='N') AND (Armast.fcStatus='U'
OR Armast.fcStatus='P' OR Armast.fcStatus='F')
AND (LEFT(Inprod.fpc_name,2)<>'RM') AND
(Inprod.fpc_name<>'RAW MATERIAL')
AND Aritem.fpartno >='99000000' AND Aritem.fpartno
<= '99999999'
GROUP BY Inprod.fpc_name, Aritem.fpartno
WITH ROLLUP
ORDER BY GROUPING(Inprod.fpc_name), Inprod.fpc_name,
GROUPING(Aritem.fpartno), Aritem.fpartno
END
Thanks, DBAL