Reputation: 2020
I have two tables that i added a new column to. The column name is LocationKey. Whenever i try and use that column in a alter stored procedure i get a error message below. I went back and copy pasted the column name. It is exact! it is where it needs to be on both tables.
Msg 207, Level 16, State 1, Procedure usp_MY_Stored_procedure, Line 122
Invalid column name 'LocationKey'.
I have googled and nothing that i came up with has resolved the problem. What could i be missing?
Link i have read: 1. "Invalid column name" error when calling insert after table created
Anything i can try to troubleshoot this further would be greatly appreciated.
-- DROP LEADING DOUBLE QUOTE (and PhotoUrl column)
INSERT INTO [table2](
[Comment1]
,[Comment 2]
,[Comment 3]
,[Comment 4]
,[Name]
,[PhotoUrl]
,[LocationKey]
)
SELECT
[Comment1]
, [Comment 2]
, [Comment 3]
, [Comment 4]
, SUBSTRING([Name],1,100)-- Limit to 100 chars
, [PhotoUrl] -- varchar(MAX)
, [LocationKey]
FROM [table1]
Upvotes: 1
Views: 1383
Reputation: 18749
You need to change the SQL, since the syntax looks incorrect, try this...
INSERT INTO [table2](
[Comment1]
,[Comment 2]
,[Comment 3]
,[Comment 4]
,[Name]
,[PhotoUrl]
,[LocationKey]
)
SELECT
[Comment1]
, [Comment 2]
, [Comment 3]
, [Comment 4]
, SUBSTRING([Name],1,100)-- Limit to 100 chars
, [PhotoUrl] -- varchar(MAX)
, [LocationKey]
FROM [table1]
EDIT: Based on comments
If you want to colour code the different environments, you can use this tip http://evanon.net/blog/post/2011/04/16/Colour-coded-Environments-in-SQL-Server-Management-Studio-2008.aspx
Upvotes: 2
Reputation: 1441
In case the comma issue in the other answers is just a typo in your question, two other thoughts come to mind:
Also, you can copy and paste your SELECT statement to a new query, and try running it. That will tell you if the invalid column error is coming from Table1 or Table2.
Another thought - are you possibly querying through a view, and not direct to the table? If the view isn't defined with SCHEMABINDING, you'll need to refresh the view to have the new field appear.
Upvotes: 2
Reputation: 14915
Your syntax is wrong. Remove leading commas, add parenthesis. See updated code below.
INSERT INTO [table2]
(
[Comment1]
,[Comment 2]
,[Comment 3]
,[Comment 4]
,[Name]
,[PhotoUrl]
,[LocationKey]
)
SELECT
[Comment1]
,[Comment 2]
,[Comment 3]
,[Comment 4]
,SUBSTRING([Name],1,100)
,[PhotoUrl]
,[LocationKey]
FROM [table1];
Upvotes: 0