Insert Statement not working

  • I ran an Insert script:

    insert into tableA (column1, column2)

    values( select column1 from tableB ,select column2 from tableC)

    Isnt this valid in SQL Server 2005?

  • Well syntactically, you want something like this:

    insert into tableA (column1, column2)

    values(

    (select column1 from tableB)

    ,(select column2 from tableC)

    )

    This is valid syntax, however, the logic of this looks pretty suspect.

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

  • Does'nt seem to work.

    "Keep Trying"

  • That statement wont work.. ...

    You can insert using this statement

    Insert into tablesa(column1, column2)

    select column1, column2 from tableb

    The select statement can return many records... all records will be inserted in to the table(tablea).

    Just check and let me know.

  • Hi,

    rbarryyoung's code is not working.

    Gouthama's code is working, but it is not effective from the context because actually its trying to insert from two tables.

    I guess, we need to use JOIN between tableB and tableC.

  • Absolutely YES.. you need to use inner join if you want to insert values from different tables.

    The result set which you will get in the select statement... same records will be inserted in to the table which you want to insert.

  • Oops, you folks are right, my bad. That's what I get for posting at 2AM...

    [font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
    Proactive Performance Solutions, Inc.
    [/font]
    [font="Verdana"] "Performance is our middle name."[/font]

Viewing 7 posts - 1 through 6 (of 6 total)

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