Sunday, 10 February 2013

SQL INSERT INTO Statement Syntax and Example

SQL INSERT INTO Statement
The INSERT INTO statement is used to insert a new rows/records into a table.

Syntax:

It is possible to write the INSERT INTO statement in two forms:
In the first form we doesn't specify the column names where the data will be inserted, only their values.
INSERT INTO Table_name VALUES (value1, value2, value3,...)
In the second form we specifies both the column names and the values to be inserted:
INSERT INTO Table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)

Example:

First we have to create a table (EMP) as follows to Insert Data :
CREATE TABLE [dbo].[EMP](
[Emp_ID] [float] NULL,
[E_Name] [varchar](255) NULL,
[Job] [varchar](255) NULL,
[Dept_ID] [float] NULL,
[Salary] [float] NULL



GO
-------------------------------------------------------------------------------------
Criteria-I : To insert a new row in the "EMP" table.
We use the following SQL statement:

INSERT INTO EMP VALUES (1234,'Tamatam', 'Analyst',23,10000)

-------------------------------------------------------------------------------------
Criteria- II : To Insert Data Only in Specified Columns:
It is also possible to only add data in specific columns.
The following SQL statement will add a new row, but only adds data in to the specific columns

INSERT INTO EMP (Emp_ID, E_Name) VALUES ( 2345,'Reddy' )


Thanks,
TAMATAM

2 comments:

  1. what if one of the value is datetime value like 21/10/2014 22:32, what would be insert statement please

    ReplyDelete
  2. HI User ,
    Date and Time also a string so that you can insert in the same way to that Date and Time Field.
    '21/10/2014 22:32'.
    Try, if any issue , explain in detail.
    I will help you.

    ReplyDelete

Follow Me by Email

ExcelKingdom-Popular Posts

ExcelKingdom-Random Posts