|
|
Question : msg 207 "invalid column name" when adding columns to temp table
|
|
I'm creating a pair of temp tables to eventually use in a stored procedure that outputs a single recordset. The code at the end of my post is from the first of those two temp tables. I began by creating the table without the columns 'wthrYr', 'baseYr', 'baseMn', 'currentYr', and 'currentMn'. Once I verified that I the temp table returned the desired values, I added the above columns. Then when I tried to execute the query from within SQL Query Analyzer, I receive the following errors:
Server: Msg 207, Level 16, State 3, Line 22 Invalid column name 'wthrYr'. Server: Msg 207, Level 16, State 1, Line 22 Invalid column name 'baseYr'. Server: Msg 207, Level 16, State 1, Line 22 Invalid column name 'baseMn'. Server: Msg 207, Level 16, State 1, Line 22 Invalid column name 'currentYr'. Server: Msg 207, Level 16, State 1, Line 22 Invalid column name 'currentMn'.
I've tried to rename the temp table, creating it as a view to pull from, and adding the go statements to dictate parse order. Nothing helps. I've also read the other posts here about msg 207. Nothing helps. Any sugestions?
--Code begins CREATE TABLE #CAPBPTDefaults( channelID INT, channelCode VARCHAR(64), logicalDeviceInfo VARCHAR(50), logicalDeviceCode VARCHAR(16), logicalDeviceTypeInfo VARCHAR(32), placeCode VARCHAR(16), placeInfo VARCHAR(50), wthrYr VARCHAR(50), yr INT, mn INT, dy INT, baseYr VARCHAR(500), baseMn INT, currentYr VARCHAR(500), currentMn INT, sumTotal NUMERIC(4,2), CAPSumBPTDef NUMERIC(4,2), CAPWinBPTDef NUMERIC(4,2) )
INSERT INTO #CAPBPTDefaults( channelID, channelCode, logicalDeviceInfo, logicalDeviceCode, logicalDeviceTypeInfo, placeCode, placeInfo, wthrYr, yr, mn, dy, baseYr, baseMn, currentYr, currentMn, sumTotal, CAPSumBPTDef, CAPWinBPTDef) SELECT ChannelSummary.channelID, Channel.channelCode, LogicalDevice.logicalDeviceInfo, LogicalDevice.logicalDeviceCode, LogicalDeviceType.logicalDeviceTypeInfo, Place.placeCode, Place.placeInfo, 'BASE' AS wthrYr, YEAR(summaryDate) yr, MONTH(summaryDate)mn, DAY(summaryDate)dy, YEAR(summaryDate)baseYr, MONTH(summaryDate)baseMn, '2005' currentYr, MONTH(summaryDate)currentMn, sumTotal, CAST((SELECT value FROM SystemData WHERE fieldID = 'CAPSummerBPTDefault') AS NUMERIC(4,2)) CAPSumBPTDef, CAST((SELECT value FROM SystemData WHERE fieldID = 'CAPWinterBPTDefault') AS NUMERIC(4,2)) CAPWinBPTDef
FROM ChannelSummary CROSS JOIN SystemData INNER JOIN Channel ON ChannelSummary.channelId = Channel.channelID INNER JOIN LogicalDevice ON Channel.logicalDeviceID = LogicalDevice.logicalDeviceID INNER JOIN LogicalDeviceType On LogicalDevice.logicalDeviceTypeID = LogicalDeviceType.logicalDeviceTypeID INNER JOIN Place ON LogicalDevice.placeID = Place.placeID
GROUP BY channelSummary.channelID, YEAR(summaryDate), MONTH(summaryDate), DAY(summaryDate), channelCode, sumtotal, logicalDeviceInfo, logicalDeviceCode, logicalDeviceTypeInfo, placeCode, placeInfo, wthrYr, baseYr, baseMn, currentYr, currentMn go
|
Answer : msg 207 "invalid column name" when adding columns to temp table
|
|
After reading it again, you don't need a GROUP BY because you're not doing any summary calculations. You can just to a SELECT DISTINCT instead.
|
|
|
|
|