locked
My Insert Into Select is not working RRS feed

  • Question

  • Here's what I have.

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (wrdID, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]

    I am trying to combine the values from the [dbo].[consonants] with a user inserted texts and then insert this into a temporary table. The error is: Msg 207, Level 16, State 1, Line 1
    Invalid column name 'wrdID'.

    Any help is much appreciated.

    Wednesday, July 18, 2012 2:15 AM

Answers

  • Hi,

    Try this

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (ID, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]

    Regards
    Satheesh

    • Marked as answer by Charlie2 Wednesday, July 18, 2012 1:40 PM
    Wednesday, July 18, 2012 3:17 AM
  • Hi,

    Try this

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (ID, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]

    Regards
    Satheesh

    Thank you so much for all the help. It worked. However, I've also discovered this:

    SELECT wrdID, consonant + 'ab' as Word INTO #wrdCombined
    FROM [lauj].[consonants]

    Between the Select Into and Insert Into I have above, which one is more efficient?

    • Marked as answer by Charlie2 Wednesday, July 18, 2012 1:40 PM
    Wednesday, July 18, 2012 3:28 AM
  • Try

    SELECT a.* FROM #wrdCombined a,[dbo].[words] b
    WHERE #wrdCombined.Word = [dbo].[words].word



    Many Thanks & Best Regards, Hua Min

    • Marked as answer by Charlie2 Wednesday, July 18, 2012 1:40 PM
    Wednesday, July 18, 2012 3:52 AM

All replies

  • Look closer in your CREATE TABLE statement. You named the first column ID and not the wrdID. Fix it either in the INSERT or in the CREATE table to be the same.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    • Proposed as answer by M_06 Wednesday, July 18, 2012 7:33 AM
    Wednesday, July 18, 2012 2:24 AM
  • Use

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (id, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]



    Many Thanks & Best Regards, Hua Min


    • Edited by Jackson_1990 Wednesday, July 18, 2012 2:24 AM
    • Proposed as answer by M_06 Wednesday, July 18, 2012 7:33 AM
    Wednesday, July 18, 2012 2:24 AM
  • Hi,

    Try this

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (ID, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]

    Regards
    Satheesh

    • Marked as answer by Charlie2 Wednesday, July 18, 2012 1:40 PM
    Wednesday, July 18, 2012 3:17 AM
  • Hi,

    Try this

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (ID, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]

    Regards
    Satheesh

    Thank you so much for all the help. It worked. However, I've also discovered this:

    SELECT wrdID, consonant + 'ab' as Word INTO #wrdCombined
    FROM [lauj].[consonants]

    Between the Select Into and Insert Into I have above, which one is more efficient?

    • Marked as answer by Charlie2 Wednesday, July 18, 2012 1:40 PM
    Wednesday, July 18, 2012 3:28 AM
  • If you don't have a table created in which you want to insert the data into then using SELECT INTO #tableName .. creates the table on the fly.

    If you have the table already, you need to use INSERT INTO SELECT ... syntax as using SELECT INTO #tablename... returns error saying #tableName already exists.

    If you create table at first then you could add all the constraints, keys as required.

    Using SELECT INTO #tableName creates a simple table without any keys and constraints and you need to ALTER that table later if you need to add any constraints or keys as required (if you use it for other purposes).

    Reg. efficiency piece - I don't think there would be any performance difference between the two (I might be wrong though).

    Thanks!

    Wednesday, July 18, 2012 3:36 AM
  • Hi,

    Try this

    CREATE TABLE #wrdCombined
    (id int,
    word varchar(50)
    )
    INSERT INTO #wrdCombined (ID, Word)
    SELECT wrdID, consonant + 'ab' as Word
    FROM [dbo].[consonants]

    Regards
    Satheesh

    Thank you so much for all the help. It worked. However, I've also discovered this:

    SELECT wrdID, consonant + 'ab' as Word INTO #wrdCombined
    FROM [lauj].[consonants]

    Between the Select Into and Insert Into I have above, which one is more efficient?

    The difference is

    by this

    SELECT wrdID, consonant + 'ab' as Word INTO #wrdCombined
    FROM [lauj].[consonants]

    it means you do not need to specifically create #wrdCombined at all before the insert.

    But using insert, you do need to create #wrdCombined!


    Many Thanks & Best Regards, Hua Min

    Wednesday, July 18, 2012 3:47 AM
  • Deepak, thanks for the insight.

    A quick question regarding this temporary table creating and reading from again. I have the following statement.

    SELECT wrdID, consonant + 'ab' as Word INTO #wrdCombined
    FROM [lauj].[consonants]
    
    SELECT * FROM #wrdCombined
    WHERE #wrdCombined.Word = [dbo].[words].word

    I have error on the second Select statement. Here's the error:

    Msg 4104, Level 16, State 1, Line 2
    The multi-part identifier "dbo.words.word" could not be bound.

    However, when I tried this:

    SELECT *
      FROM [dbo].[words]
      WHERE [dbo].[words].word = 'pab'

    It works fine. So, why am I getting this error?


    Wednesday, July 18, 2012 3:47 AM
  • Try

    SELECT a.* FROM #wrdCombined a,[dbo].[words] b
    WHERE #wrdCombined.Word = [dbo].[words].word



    Many Thanks & Best Regards, Hua Min

    • Marked as answer by Charlie2 Wednesday, July 18, 2012 1:40 PM
    Wednesday, July 18, 2012 3:52 AM
  • As dbo.Words and #wrdCombined are two separate tables, you need to JOIN them in FROM clause (as shown by Hua Min) before you can use columns from the respective tables.
    Wednesday, July 18, 2012 4:01 AM
  • Select INTO is quicker than INSERT INTO.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog

    Wednesday, July 18, 2012 4:04 AM

  • SELECT wrdID, consonant + 'ab' as Word INTO #wrdCombined
    FROM [lauj].[consonants]
    
    SELECT * FROM #wrdCombined
    WHERE #wrdCombined.Word = [dbo].[words].word


    It works fine. So, why am I getting this error?


    You don't have  [dbo].[words].Word in your from clause, so you cant really reference this table

    Regards
    Satheesh

    Wednesday, July 18, 2012 5:09 AM
  • You guys/gals are awesome! Many thanks for the all the help.
    Wednesday, July 18, 2012 1:42 PM