I have the following SQL stored procedure that when I try to save it, it complains about “Invalid column name ‘Goal'” in the last select statement.
Can someone tell me why this is an invalid comlumn name? I am doing basically the same thing for NetSales and it doesn’t complain about that column.
if object_id('tempdb..#tmpHours') is not null
DROP TABLE #tmpHours
if object_id('tempdb..#tmpPay') is not null
DROP TABLE #tmpPay
SET @MondayOfCurrentWeek = (SELECT DATEADD(wk, DATEDIFF(wk,0,@WeekOf), 0) )
SELECT p.PerceptionistID AS PerceptionistID, p.BaseCommission AS BaseCommission, p.BonusCommission AS BonusCommission,
h.WeekOf AS WeekOf, h.WorkHours AS WorkHours, h.PTOHours AS PTOHours, h.HolidayHours AS HolidayHours,
ROUND(h.WorkHours, 0) AS HoursRounded,
(
SELECT COUNT(c.PerceptionistID)
FROM T_Call c
WHERE
c.PerceptionistID = p.PerceptionistID
AND c.OutcomeID = @OutcomeSale
AND EnteredOn BETWEEN @MondayOfCurrentWeek AND DATEADD(dd, 7, @MondayOfCurrentWeek)
) AS GrossSales,
(
SELECT COUNT (c.PerceptionistID)
FROM T_CallCredit cc
INNER JOIN T_Call c
ON cc.CallID = c.CallID
WHERE
c.PerceptionistID = p.PerceptionistID
AND cc.CallCreditStatusID NOT IN (17, 18) -- 17 - 'Error in Customer Account', 18 - 'Courtesy Credit'
AND cc.EnteredOn BETWEEN @MondayOfCurrentWeek AND DATEADD(dd, 7, @MondayOfCurrentWeek)
) AS Credits
INTO #tmpHours
FROM T_Perceptionist p
RIGHT JOIN T_PerceptionistHours h
ON p.PerceptionistID = h.PerceptionistID
WHERE h.WeekOf = @MondayOfCurrentWeek
SELECT PerceptionistID, CAST((HoursRounded*2) AS int) AS Goal, GrossSales, Credits, (GrossSales - Credits) AS NetSales,
HoursRounded, WorkHours, PTOHours, HolidayHours, BaseCommission, BonusCommission,
[dbo].fnCalculateCommission(BaseCommission, BonusCommission, (GrossSales - Credits), CAST((HoursRounded*2) AS int)) AS CommissionPay
INTO #tmpPay
FROM #tmpHours
--- Complains about column name 'Goal' in the following statement
SELECT PerceptionistID, Goal, GrossSales, Credits, NetSales, HoursRounded, WorkHours, PTOHours,
HolidayHours, BaseCommission, BonusCommission, CommissionPay
FROM #tmpPay
Try running it in a new connection or dropping the temp tables in a separate batch first.
Possibly the
#temptables are still hanging about from a previous batch when you did not have theGoalcolumn and the parser validates the column names against the existing table, encounters a binding error and refuses to run the batch.If the temp tables don’t exist at all compilation of the statement will be deferred.