DML Commands in SQL Server: INSERT (Part 2) -- Insert into a table from some other table

Posted onThursday, 5 April 2012bySQL Developer

DML: Data Manipulation Language.

In the earlier session ( Part 1 for INSERT Command), we have discussed how to insert or store data into a table using simple commands.

Using the commands which were discussed in part1, only data can be inserted into table if we know the values. That is data can be inserted by explicitly specifying the values which needs to be stored.

Suppose we have a table (TABLE_1) with all the required information and we want to insert data from that table (TABLE_1) to another table (TABLE_2), then this can be done by using the following approach:

Here is the syntax for inserting data from one table to another table:

INSERT INTO TABLE_2( Column_1, Column_2, | , | ,

Column_N)SELECT Column_1, Column_2, | , | ,

Column_NFROM TABLE_1Example:

Lets assume that I have lot of email accounts and I am maintaining different password for each account.

Here is the table I created to store my email ids and corresponding password:

CREATETABLE MYPASSWORDS

(

ACCOUNTVARCHAR(20),

ACCOUNTIDVARCHAR(100),

PWDVARCHAR(20)

)

and, I inserted following records as per the Insert syntax given in Part1.