Inserting data between existing table

Before we go, first we will create a table and you assume that the table already exists. Because that is how the following statement executes that means it will look for the existence of both the table involved in the query. Below is the table create statement:

 

CREATE TABLE DisType

(

      discounttype varchar(40),

      discount decimal(4, 2) NOT NULL

);

 

We just created a Distype table with two columns. And we are going to insert data into this table from the existing Discounts table in the Pubs database.

 

Now look at the below insert statement:

 

Insert into DisType(Discounttype,discount)

Select DiscountType, Discount from Discounts;

 

Here, We specified the target table (One that should exist) DisType and specified the columns in that table. Instead of passing the values manually we specified the source table to pick that data. As we are looking values for only two columns DiscountType, Discount in the target table DisType, those two columns in the source table Discounts is retrieved through select statement. Note that it does not require that column name should match. But the data type should be matched for smooth insertion of data from source to destination table.
Ebook Download
View all
Learn
View all