Solved

Msg 207, Level 16, State 3 Invalid Column Name

Posted on 2006-06-30
4
821 Views
Last Modified: 2009-07-29
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!
0
Comment
Question by:ktt2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:apresto
ID: 17017948
Hi there,

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

Author Comment

by:ktt2
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?
0
 
LVL 23

Expert Comment

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

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 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...
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
In today’s complex data management environments, it is not unusual for UNIX servers to be dedicated to a particular department, purpose, or database.  As a result, a SAS® data analyst often works with multiple servers, each with its own data storage…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

760 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now