Invalid object name

  • This short piece of SQL gives "Invalid object name 'test2'. I can create table test but creating table test2 fails. What am I doing wrong? Thanks as always.

    use dz

    go

    create table test (ColumnA int)

    insert into test values (1)

    select * from test

    go

    insert into test2 select * from test

    go

  • You insert into Test2, but you never create it, hence the table does not exist.

    Add a CREATE TABLE statement for Test2

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Geez, I should have known that!

    It's the difference between

    select * into test2 from test -- inserts into a new table

    insert into test2 select * from test -- inserts into an existing table

    Thanks GilaMonster.

  • aiki4ever-796329 (8/27/2013)


    Geez, I should have known that!

    It's the difference between

    select * into test2 from test -- inserts into a new table

    insert into test2 select * from test -- inserts into an existing table

    Thanks GilaMonster.

    yes, when you write statment -

    SELECT * INTO Test2 FROM test

    It will create the table.

    WHile, when you write INSERT INTO Test2 SELECT * FROM test

    It will insert the data into test2 if test2 is already created

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply