我有以下SQL存储过程,当我尝试保存它时,它会在最后一个select语句中抱怨“无效的列名'目标'”。
有人可以告诉我为什么这是一个无效的comlumn名称?我对NetSales基本上做同样的事情,并没有抱怨该列。
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
答案 0 :(得分:6)
尝试在新连接中运行它或首先在单独的批处理中删除临时表。
当您没有#temp
列并且解析器针对现有表验证列名时,Goal
表仍然悬挂在前一批中,遇到绑定错误并拒绝运行批处理。
如果临时表不存在,那么语句的编译将被推迟。