Home » T-SQL Insert Statement

T-SQL Insert Statement

by Online Tutorials Library

T-SQL INSERT STATEMENT

In T-SQL, INSERT statement is used to add new rows into the table.

  • INSERT INTO SELECT requires the data types in source and targets the match of table.
  • The existing records in the table are not affected by the INSERT statement.

Syntax:

The following are the two syntaxes of Insert into a statement.

Where column1, column2,…. are the columns name in the table.

We cannot specify the column name in the SQL query when we add the values for the columns. The order of the values is used in the same order as specify below.

The Syntax of INSERT Statement is given below:

Example:

Following statements will create six records in CUSTOMERS table –

INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)   VALUES (001, 'Rahul', 23, 'Kota', 20000.00 );    INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)   VALUES (002, 'Klintan', 22, 'Mumbai', 15000.00 );      INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)   VALUES (003, 'kamal', 31, 'Delhi', 25000.00 );      INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)   VALUES (004, 'Chitra', 28, 'kanyakumari', 65000.00 );     INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)   VALUES (005, 'Santanu', 26, 'Madhyapredesh', 38500.00 );      INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY)   VALUES (006, 'Savitri', 24, 'Bhopal', 4500.00 );  

Syntax:

We can create a record in CUSTOMERS table using the second syntax as follows –

All the above statement will produce the following records in CUSTOMERS table-

ID Name AGE ADDRESS Salary
001 Rahul 23 Kota 20000.00
002 Clinton 22 Mumbai 15000.00
003 Kamal 31 Delhi 25000.00
004 Chitra 28 Kanyakumari 65000.00
005 Santanu 26 Madhya Pradesh 38500.00
006 Savitri 24 Bhopal 4500.00
007 Manii 30 Indonesia 15000.00

Populate one table using another table

To populate the data in one table we need to use SELECT statement over another table that has set of given fields, which are required to populate the first table. The syntax is:


You may also like