Software Developer, Equity Analyst, Polymath.
How to Insert from other table that already exists
The basic Insert statement is.
CREATE TABLE Customer ( ID int identity(1,1), CustomerName varchar(30), Address varchar(100), Phone varchar (100) ) INSERT INTO Customer (CustomerName, Address, Phone)
How about you want to put or just copy some of the data from other table to this Customer Table?
INSERT INTO Customer (CustomerName, Address, Phone) SELECT CustomerName, Address, Phone From OldCustomer
In above query you’ll insert your customer table with data from OldCustomer table with ID less than 50 (0-49).
How about if you just want to create a replication of a table with data type?
SELECT * INTO newTable FROM OldTable You don’t need to create the table first. Cause on select into statement the create table is already done then the insertion. SELECT * INTO Customer FROM OldCustomer How about if you just want to create a replication of some column in a table and columns’ data type? SELECT CustomerName, Phone INTO Customer FROM OldCustomer And if there are needs to use join or where clause just use it as you need it. example : SELECT A.CustomerName, A.Phone, B.City INTO Customer FROM OldCustomer A JOIN City B On A.CityID= B.CityID WHERE City LIKE ':%' INSERT INTO Customer (CustomerName, Address, Phone) SELECT CustomerName, Address, Phone From OldCustomer A JOIN City B On A.CityID= B.CityID Where A.ID > 50 AND City LIKE 'L%'
Where ID > 50
values ('Jane', 'anywhere street', '9097655')
This entry was posted by Ray Tawil on October 28, 2009 at 3:59 pm, and is filed under MS Sql Server. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site.
Leave a Reply
You must be logged in to post a comment.