Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to do this type of insert?


How to do this type of insert?

Author
Message
Like2SQL
Like2SQL
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 223
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.
Calibear
Calibear
SSC Veteran
SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)SSC Veteran (231 reputation)

Group: General Forum Members
Points: 231 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
Steven Willis
Steven Willis
Mr or Mrs. 500
Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)Mr or Mrs. 500 (549 reputation)

Group: General Forum Members
Points: 549 Visits: 1721
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.

 
kapil_kk
kapil_kk
SSCrazy
SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)SSCrazy (2.6K reputation)

Group: General Forum Members
Points: 2579 Visits: 2764
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/
Like2SQL
Like2SQL
SSC Journeyman
SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)SSC Journeyman (82 reputation)

Group: General Forum Members
Points: 82 Visits: 223
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]
Sean Lange
Sean Lange
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18438 Visits: 17190
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)
J Livingston SQL
J Livingston SQL
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3731 Visits: 33522
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

Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search