简体   繁体   中英

Inserting into table A from table B, giving invalid column name error

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM