Wednesday, December 11

INSERT INTO - SQL

The INSERT INTO statement is used to insert new records in a table. It is a DML statement.

Syntax:


Here only values are specified for all corresponding columns in order.


INSERT INTO table_name                                         
VALUES (value1,value2,value3,...);                                                                         

or,

Here records are inserted in specified columns only.

INSERT INTO table_name (column1,column2,column3,...              
VALUES (value1,value2,value3,...);                                                                                                           
Example: We will insert values into Employee table, that we have created in Mytest_db                           database during previous sessions.

INSERT INTO employee
VALUES (1231,'Smith',15000,'M');
INSERT INTO employee
VALUES (1232,'Jones',15000,'M');

Note: From SQL Server 2008 onwards, the code can be simplified to the following.

INSERT INTO employee                                      VALUES (1233,'Alice',15000,'F'),(1234,'Biona',15000,'F');


Output:

Select * from dbo.employee;






















Now lets see to Insert data in specified columns;

INSERT INTO employee(ID,Name)
VALUES (1235,'Rijo'),(1236,'Ashni');
Output:
Select * from dbo.employee;
























The columns without values will display Null.