ktt2
asked on
Msg 207, Level 16, State 3 Invalid Column Name
I have the following t-sql:
/*****SAMPLE T-SQL TO RUN FOR IMPORTING DATA FROM THE DB's In_Tab table*****/
exec sp_msforeachdb @command1='
IF RIGHT(''?'',3) IN (''MSA'',''DMA'',''TSA'')
BEGIN
DECLARE @db VARCHAR(30)
DECLARE @bookID tinyint
DECLARE @geographyID tinyint
DECLARE @SeasonID tinyint
DECLARE @bookYear int
SET @db = ''?''
SET @geographyID = (SELECT Geography_ID
FROM Book_Central.dbo.Geography
WHERE Description = RIGHT(@db,3))
SET @SeasonID = (SELECT Season_ID
FROM AudiencePro_Central.dbo.Se ason
WHERE LEFT(Season_Name,4) = SUBSTRING(@db,CHARINDEX('' _'',@db)+1 ,4))
SET @bookYear = (SELECT
CASE SUBSTRING(@db,CHARINDEX('' 0'',@db)+1 ,1)
WHEN ''0'' THEN 2000
WHEN ''1'' THEN 2001
WHEN ''2'' THEN 2002
WHEN ''3'' THEN 2003
WHEN ''4'' THEN 2004
WHEN ''5'' THEN 2005
WHEN ''6'' THEN 2006
END AS Book_Year)
SET @bookID = (SELECT Book_ID
FROM Book_Central.dbo.Book
WHERE Geography_ID = @GeographyID
AND Season_ID = @SeasonID
AND Book_Year = @bookYear)
--Now insert the rows into the In_Tab_table.
/*
INSERT INTO Book_Central.dbo.In_Tab
Book_ID
,Market_ID
,Demographic_ID
,Value
*/
SELECT
@bookID
,Market_Code
,Aud_Char_ID
,Value
FROM ?.dbo.In_Tab
WHERE Geo_Ind = @geographyID
ORDER BY Market_Code
END
'
Where ? refers to the db on the server. The only ones I’m interested in are the ones that the name ends in MSA/TSA/DMA. These have the In_Tab table with the following columns:
• Market_Code
• Aud_Char_ID
• Geo_Ind
However if I run that t-sql above, I immediately get the error message:
Msg 207, Level 16, State 3, Line 50
Invalid column name 'Market_Code'.
Msg 207, Level 16, State 3, Line 50
Invalid column name 'Aud_Char_ID'.
Msg 207, Level 16, State 3, Line 50
Invalid column name 'geo_Ind'.
The column names the message is referring to points to the column names I’ve typed in the t-sql above. I checked all of the db’s I’m querying for any mismatching in names, but all match.
It seems that the t-sql is returning the results, but why am I still getting these error messages?
I initially thought that one of the databases is incorrectly referencing the columns, but I don’t think so after doing the verification of any mismatches.
Please advise!
/*****SAMPLE T-SQL TO RUN FOR IMPORTING DATA FROM THE DB's In_Tab table*****/
exec sp_msforeachdb @command1='
IF RIGHT(''?'',3) IN (''MSA'',''DMA'',''TSA'')
BEGIN
DECLARE @db VARCHAR(30)
DECLARE @bookID tinyint
DECLARE @geographyID tinyint
DECLARE @SeasonID tinyint
DECLARE @bookYear int
SET @db = ''?''
SET @geographyID = (SELECT Geography_ID
FROM Book_Central.dbo.Geography
WHERE Description = RIGHT(@db,3))
SET @SeasonID = (SELECT Season_ID
FROM AudiencePro_Central.dbo.Se
WHERE LEFT(Season_Name,4) = SUBSTRING(@db,CHARINDEX(''
SET @bookYear = (SELECT
CASE SUBSTRING(@db,CHARINDEX(''
WHEN ''0'' THEN 2000
WHEN ''1'' THEN 2001
WHEN ''2'' THEN 2002
WHEN ''3'' THEN 2003
WHEN ''4'' THEN 2004
WHEN ''5'' THEN 2005
WHEN ''6'' THEN 2006
END AS Book_Year)
SET @bookID = (SELECT Book_ID
FROM Book_Central.dbo.Book
WHERE Geography_ID = @GeographyID
AND Season_ID = @SeasonID
AND Book_Year = @bookYear)
--Now insert the rows into the In_Tab_table.
/*
INSERT INTO Book_Central.dbo.In_Tab
Book_ID
,Market_ID
,Demographic_ID
,Value
*/
SELECT
@bookID
,Market_Code
,Aud_Char_ID
,Value
FROM ?.dbo.In_Tab
WHERE Geo_Ind = @geographyID
ORDER BY Market_Code
END
'
Where ? refers to the db on the server. The only ones I’m interested in are the ones that the name ends in MSA/TSA/DMA. These have the In_Tab table with the following columns:
• Market_Code
• Aud_Char_ID
• Geo_Ind
However if I run that t-sql above, I immediately get the error message:
Msg 207, Level 16, State 3, Line 50
Invalid column name 'Market_Code'.
Msg 207, Level 16, State 3, Line 50
Invalid column name 'Aud_Char_ID'.
Msg 207, Level 16, State 3, Line 50
Invalid column name 'geo_Ind'.
The column names the message is referring to points to the column names I’ve typed in the t-sql above. I checked all of the db’s I’m querying for any mismatching in names, but all match.
It seems that the t-sql is returning the results, but why am I still getting these error messages?
I initially thought that one of the databases is incorrectly referencing the columns, but I don’t think so after doing the verification of any mismatches.
Please advise!
ASKER
it's referring to the current db's in_tab table...why I don't need it or it should be dbo.In_Tab?
just dbo. should be fine, normally you would put something before dbo if you were referencing a different database or server
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
whats with the ? here--> ?.dbo.In_Tab