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.Season
                               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!
ktt2Asked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
the issue is that the entire dynamic script is run against ALL databases, including master, model etc.
the test in the name is part of the script, but it's validated in it's entirety, and for master, model etc there is no such table with such columns, hence the error.

to solve that, you would need to make the inner part of the sql dynamic also...
0
 
aprestoCommented:
Hi there,

whats with the ? here-->  ?.dbo.In_Tab
0
 
ktt2Author Commented:
it's referring to the current db's in_tab table...why I don't need it or it should be dbo.In_Tab?
0
 
aprestoCommented:
just dbo. should be fine, normally you would put something before dbo if you were referencing a different database or server
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.