Insert and Select statements

  • I am working on a query

    where I am inserting data from another table

    Insert into table1(col1,col2,col3)

    select col4,col5,([highlight="#98"]col6+col1)

    [/highlight]from table2

    How can I refer col1 data in the select statement?

    Please help me out

    Thanks.

  • You have to join the two tables somehow

    the INSERT INTO part is a bit of a red herring.

    SELECT A.Field1, B.Field3

    FROM A INNER JOIN B ON A.SomeKey = B.OtherKey

    WHERE...

    then you just wrap that in an insert

    INSERT INTO TableA(Field1, Field2...)

    SELECT A.Field1, B.Field3

    FROM A INNER JOIN B ON A.SomeKey = B.OtherKey

    WHERE...

  • If you are inserting col4 into col1, don't you want to use (col6 + col4)?

    Drew

    J. Drew Allen
    Business Intelligence Analyst
    Philadelphia, PA

  • If you want to refer to col1 in table2, you can refer to it in the select statement. Example:

    SELECT col4, col5, col2

    FROM table2;

    If, on the other hand, you have a col1 in both tables and you want to select from both tables, then it's like pietlinden said - just join the two tables together.

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

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