Msg 207, Level 16, State 3 Invalid Column Name

Posted on 2006-06-30
Medium Priority
Last Modified: 2009-07-29
I have the following t-sql:

exec sp_msforeachdb @command1='

IF RIGHT(''?'',3) IN (''MSA'',''DMA'',''TSA'')


      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
      FROM ?.dbo.In_Tab
      WHERE Geo_Ind = @geographyID
      ORDER BY Market_Code


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!
Question by:ktt2
  • 2
LVL 23

Expert Comment

ID: 17017948
Hi there,

whats with the ? here-->  ?.dbo.In_Tab

Author Comment

ID: 17018127
it's referring to the current db's in_tab table...why I don't need it or it should be dbo.In_Tab?
LVL 23

Expert Comment

ID: 17018199
just dbo. should be fine, normally you would put something before dbo if you were referencing a different database or server
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 17018247
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...

Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

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.

Join & Write a Comment

In this article, we will show how to detach and attach a database and then show how to repair a corrupt database and attach it, If it has some errors. We will show how to detach and attach using SSMS or using T-SQL sentences.
A method of moving multiple mailboxes (in bulk) to another database in an Exchange 2010/2013/2016 environment...
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

597 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question