How to do this type of insert?

  • I'm sorry if this question has been asked here, but I couldn't find. I am new to sql and need to do the following type of insert:

    Insert data INTO Table 1 FROM Table 2. The data is to be inserted based on the values of certain columns in Table 1 i.e. Table1.columnA =null, Table1.columnB = null (or in some cases a particular value).

    What would be the SQL query I can use to achieve this?

    Thanks.

  • Without more data, it is really hard to answer this question. You would start with a really simple query like this:

    Insert into Table1

    select * from Table2

    You can then add your where clause and anything else after the second line. If you provide some sample data and more detail of what you are looking for, I can provide a query closer to your needs or you can read more about inserting data here:

    http://blog.sqlauthority.com/2007/08/15/sql-server-insert-data-from-one-table-to-another-table-insert-into-select-select-into-table/

    --------
    For SQL Tips, check out my blog: http://lantztechknowledge.blogspot.com/[/url]
    You can also follow my twitter account to get daily updates: @BLantz2455

  • The two basic forms of the statement are:

    --the first table must already exist. You can use * if inserting ALL columns,

    --otherwise you must give a column list

    INSERT INTO existing_table

    SELECT *

    FROM source_table

    --or

    INSERT INTO existing_table

    (Col1,Col2,Col3)

    SELECT Val1,Val2,Val3

    FROM source_table

    --if copying to a new table that doesn't exist you can do:

    SELECT *

    INTO new_table_name

    FROM old_tablename

    --note that with this latter method you are creating a heap table.

    --Creating a new table this way is great for a quick-and-dirty backup,

    --but it only creates the columns and data...no indexes, triggers, etc.

    For an actual copy of a table you can right click on the database name in SSMS and use

    the Generate Scripts function which will give you options to include copying user rights, triggers, FKs, and any data including IDENTITY inserts if that's what you need.

     

  • Please provide us more detail so that we can answer your question more clearly...

    _______________________________________________________________
    To get quick answer follow this link:
    http://www.sqlservercentral.com/articles/Best+Practices/61537/

  • Thanks for the responses guys, here is more info.

    Both tables already exist.

    Table 1 has supplier information and about 24 columns & about 100K rows of data. One of those columns (its a serial number) is missing data (aka value is currently NULL) in various rows - maybe about 5-6K rows have null data.

    The goal is to locate those rows in Table 1 where the value in the serial number column is NULL/empty and copy over those rows from Table 2. (Table 2 also has the same but latest supplier information and same 24 columns).

    Hope that adds clarification. Please let me know if further info is needed. [/size]

  • It sounds like maybe you are wanting to update those rows where the SerialNum is NULL and insert when the row is not found? You should take a look at MERGE. http://technet.microsoft.com/en-us/library/bb510625.aspx

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • if you are intending to add rows then use MERGE as Sean suggests.

    If you are only intending to update data then just use UPDATE with a join on the tables.

    some sample code below....but as you say "Table 2 also has the same but latest supplier information and same 24 columns" ...so whats the requirement for Table 1...or is this just homework?

    SELECT TOP 100

    SupplierID = IDENTITY(INT, 1, 1),

    SerialID = CAST(Abs(Checksum(Newid()) % 90000 + 1) AS INT)

    INTO dbo.TAB1

    FROM sys.all_columns ac1

    /* create copy of TAB1 */

    SELECT * INTO TAB2 FROM TAB1

    /* set every 5th supplier serialId to NULL */

    UPDATE TAB1

    SET SerialID = NULL

    WHERE (SupplierID % 5 = 0)

    /*check to see missing matches */

    SELECT TAB1.SupplierID, TAB1.SerialID AS TAB1SID, TAB2.SerialID AS TAB2SID

    FROM TAB1 INNER JOIN

    TAB2 ON TAB1.SupplierID = TAB2.SupplierID

    AND ISNULL(TAB1.SerialID,0) <> TAB2.SerialID

    /* update TAB1 SerialID from TAB2*/

    UPDATE TAB1

    SET SerialID = TAB2.SerialID

    FROM TAB1 INNER JOIN

    TAB2 ON TAB1.SupplierID = TAB2.SupplierID

    WHERE (TAB1.SerialID IS NULL)

    /*check to see missing matches */

    SELECT TAB1.SupplierID, TAB1.SerialID AS TAB1SID, TAB2.SerialID AS TAB2SID

    FROM TAB1 INNER JOIN

    TAB2 ON TAB1.SupplierID = TAB2.SupplierID

    AND ISNULL(TAB1.SerialID,0) <> TAB2.SerialID

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

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

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