0

I have table A which holds the columns (test1, test2, test3) and then table B which holds the columns (test4, test5, test6).

Table B, column test6 was added in after the table was created by using

ALTER TABLE dbo.B
    ADD test6 VARCHAR(100)

I want to do:

INSERT dbo.A (test1, test2, test3)
SELECT test4, test5, test6
FROM dbo.B

It keeps coming back saying that test6 is an "Invalid Column Name". Could it be something to do with adding the table in using the ALTER TABLE statement?

Table A, the one I'm inserting into is PK constrained, I'm not sure if that's relevant, though.

alistaire
  • 42,459
  • 4
  • 77
  • 117
Heights
  • 11
  • 6
  • Check the columns of your table using `SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'B' AND table_schema = 'dbo''` – Thomas G Mar 18 '16 at 23:36
  • you are missing into keyword – Jande Mar 18 '16 at 23:58
  • @sqlProfile87: no, he is not missing that keyword - it's **optional** , you don't *have to* use it. [See the MSDN docs on INSERT for details](https://msdn.microsoft.com/en-us/library/ms174335.aspx) – marc_s Mar 19 '16 at 07:58
  • Does this answer your question? ["Invalid column name" error when calling insert after table created](https://stackoverflow.com/questions/16165803/invalid-column-name-error-when-calling-insert-after-table-created) – argonym Feb 20 '20 at 18:36

1 Answers1

1

Are both tasks written in one SQL-script? In that case I'd recommend you to add a GO-statement between these tasks.

ALTER TABLE dbo.B
    ADD test6 VARCHAR(100)
GO


INSERT INTO dbo.A (test1, test2, test3)
SELECT test4, test5, test6
FROM dbo.B

If this is not working either, use the following task to check if the colum exists.

SELECT * FROM dbo.B
Yvo
  • 55
  • 6