Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to do this type of insert? Expand / Collapse
Author
Message
Posted Thursday, May 2, 2013 9:53 PM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 15, 2014 12:09 PM
Points: 45, Visits: 124
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.
Post #1449043
Posted Thursday, May 2, 2013 11:07 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Thursday, August 8, 2013 7:16 PM
Points: 221, Visits: 132
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/
You can also follow my twitter account to get daily updates: @BLantz2455
Post #1449046
Posted Thursday, May 2, 2013 11:09 PM
SSC-Addicted

SSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-AddictedSSC-Addicted

Group: General Forum Members
Last Login: Sunday, September 29, 2013 1:24 AM
Points: 429, Visits: 1,721
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.

 
Post #1449048
Posted Friday, May 3, 2013 3:16 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Tuesday, September 9, 2014 3:27 AM
Points: 1,893, Visits: 2,329
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/
Post #1449094
Posted Friday, May 3, 2013 4:47 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Monday, September 15, 2014 12:09 PM
Points: 45, Visits: 124
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="6"]
[/size]

Post #1449122
Posted Friday, May 3, 2013 7:33 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Friday, September 19, 2014 2:34 PM
Points: 12,923, Visits: 12,342
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1449193
Posted Sunday, May 5, 2013 10:56 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Yesterday @ 2:32 PM
Points: 1,912, Visits: 19,379
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
Post #1449525
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse