T-SQL Tutorials - TSQLTutorials.com

INSERT Statement


Introduction

The INSERT statement is used for inserting (adding) data to a table. This SQL Statement begins with the keywords INSERT INTO. There are two methods for inserting data into tables:

Syntax

This first method specifies the columns into which, the corresponding values will be added. The Values and Columns must be in the same corresponding order that you specify them.

INSERT INTO table_name (column1, column2, column3 ..)
VALUES (value1, value2, value3 ..)

In this second method, only the values for the columns are specified. A value for each column in the table must be specified. The Values must be in the same correspoding order in which the columns are in the table.

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



Inserting records by specifying the columns

This example will insert values into the 'Name', 'Position' and 'Salary' columns in the 'Employees' table. Note: All columns in the table do not have to be specified when you are using this type of syntax. In this example we do not insert a value for the 'Office' column.

Table Before INSERT Statement

SELECT *
FROM Employees
Here is a resultset from the above SELECT query:
NamePositionOfficeSalary
Joe GrapeManagerHouston80000
John PlumSoftware DeveloperHouston65000
Frank AppleSoftware DeveloperCleveland62000
Patty PineappleSoftware DeveloperCleveland60000
Judy PeachSoftware DeveloperBoston50000
Jane OrangeProject ManagerHouston75000

INSERT Statement

INSERT INTO Employees (Name, Salary, Position)
VALUES ('Johnny Appleseed','60000','Marketing')

Table After INSERT Statement

SELECT *
FROM Employees
Here is a resultset from the above SELECT query:
NamePositionOfficeSalary
Joe GrapeManagerHouston80000
John PlumSoftware DeveloperHouston65000
Frank AppleSoftware DeveloperCleveland62000
Patty PineappleSoftware DeveloperCleveland60000
Judy PeachSoftware DeveloperBoston50000
Jane OrangeProject ManagerHouston75000
Johnny AppleseedMarketing60000

Inserting a record using all columns in the table

This example will insert a record into the table without specifying the column names. This example will insert values into the 'Name', 'Position', 'Office' and 'Salary' columns in the 'Employees' table.

In this example we will assume that the 'Employees' table contains the follow columns: 'Name', 'Position', 'Office' and 'Salary'.

Table Before INSERT Statement

SELECT *
FROM Employees
Here is a resultset from the above SELECT query:
NamePositionOfficeSalary
Joe GrapeManagerHouston80000
John PlumSoftware DeveloperHouston65000
Frank AppleSoftware DeveloperCleveland62000
Patty PineappleSoftware DeveloperCleveland60000
Judy PeachSoftware DeveloperBoston50000
Jane OrangeProject ManagerHouston75000

INSERT Statement

INSERT INTO Employees
VALUES ('Johnny Appleseed','Marketing','Seattle','60000')

Table After INSERT Statement

SELECT *
FROM Employees
Here is a resultset from the above SELECT query:
NamePositionOfficeSalary
Joe GrapeManagerHouston80000
John PlumSoftware DeveloperHouston65000
Frank AppleSoftware DeveloperCleveland62000
Patty PineappleSoftware DeveloperCleveland60000
Judy PeachSoftware DeveloperBoston50000
Jane OrangeProject ManagerHouston75000
Johnny AppleseedMarketingSeattle60000


Microsoft SQL Server Standard 2008 R2 10CAL 228-09180

Price: $2040
Buy It Now
SQL Server 2008 2005 Training Exam Test Developer DVD Enterprise Book mcts 5cal

Current Price: $16.99
Current Bids: 0
★ NEW ★ MS SQL 2005 Server Standard with CAL ★ L@@K PRICE REDUCTION★

Price: $395
Buy It Now
Microsoft SQL Server 2008 - Enterprise with 25 CAL's

Price: $13850
Buy It Now
228-04023 Microsoft SQL Server 2005 Standard 5 CAL

Price: $1450
Buy It Now
Microsoft SQL Server 2005 Workgroup Edition 5 CAL, New Retail, PN A5K-01017

Price: $699.95
Buy It Now







Copyright 2012 - TSQLTutorials.com