Home Forums SQL Server 2008 T-SQL (SS2K8) Inserting multiple records from a single variable RE: Inserting multiple records from a single variable

  • 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