Why can''t I do ''create table blah as (select....''?

  • Hi all,

    I am attempting to create a table from another table.  I thought I could do:

    CREATE TABLE NEW_ADDRESSES AS

    (SELECT * FROM ADDRESSES)

    Is this not possible? I keep getting an error 'Incorrect syntax near the keyword as'.

    Thanks,

    Paula.

  • Possible, but not with that syntax. Did you check books online?

    The syntax you're looking for is

    SELECT * INTO New_Addresses FROM Addresses

    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
  • Yes, I checked books online but was looking for a 'create' statement.  In your example I presume the new_addresses table would have to exist already?

  • No. It has to not exist. 

    SELECT INTO creates the table and inserts the results of the select into it.

    To insert results into an existing table, you'd use INSERT INTO <table> SELECT ....

    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
  • Thanks, that's great.

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

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