Inserting multiple records from a single variable

  • I have two tables. Table 1 has column "job", table 2 has column "job" and column "item". In table table 2 there are multiple "items" for each "job"

    I would like to insert all of the "items" into table 1, based on a join table1.job = table2.job

    What is the best way accomplish this task?

    Thanks!

  • jhinch (4/24/2014)


    I have two tables. Table 1 has column "job", table 2 has column "job" and column "item". In table table 2 there are multiple "items" for each "job"

    I would like to insert all of the "items" into table 1, based on a join table1.job = table2.job

    What is the best way accomplish this task?

    Thanks!

    First create a query joining the two tables and filter the results or whatever needed.

    Then construct an insert clause and use the previous query as the source.

    insert into mytable(column list)

    select [column list]

    from mytable1 inner join mytable2

    on [join condition]

    ๐Ÿ˜Ž

  • Here is an example to further clarify the question;

    Table1

    Job

    1

    2

    3

    Table2

    Job Item

    1 123

    1 456

    2 123

    2 999

    2 121

    3 787

    I would like to insert all items into table 1 based on the matching job number in table2. i.e.;

    table1

    Job Item

    1 123

    1 456

    2 123

    2 999

    2 121

    3 787

  • Your sample indicates that table1 is an exact copy of two columns of table2 - is this really the case?

    Here's one way to do what you want, if table1 is restricted to jobs which already exist in it:

    SELECT DISTINCT job

    INTO #temp

    FROM Table1

    TRUNCATE TABLE Table1

    INSERT INTO Table1 (job, item)

    SELECT job, item

    FROM Table2 t2

    WHERE EXISTS (SELECT 1 FROM #temp t1 WHERE t1.job = t2.job)

    DROP TABLE #temp

    โ€œWrite the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.โ€ - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • You don't necessarily need to use a join, try the WHERE EXISTS clause. Look it up on MSDN.

    ----------------------------------------------------

  • MMartin1 (5/20/2014)


    You don't necessarily need to use a join, try the WHERE EXISTS clause. Look it up on MSDN.

    WHERE EXISTS requires an Equi-Join within the sub-query.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • jhinch (4/24/2014)


    Here is an example to further clarify the question;

    Table1

    Job

    1

    2

    3

    Table2

    Job Item

    1 123

    1 456

    2 123

    2 999

    2 121

    3 787

    I would like to insert all items into table 1 based on the matching job number in table2. i.e.;

    table1

    Job Item

    1 123

    1 456

    2 123

    2 999

    2 121

    3 787

    Why are you making a copy of the table? Why not just use Table2?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ...

    Why are you making a copy of the table? Why not just use Table2?

    And if you really want a complete copy of Table2 in Table1 then you can do just that:

    drop table Table1

    select * into Table1 from Table2

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

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

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