Skip to content

SQL Server Tutorial - INSERT Statement 101

The INSERT statement is a Data Manipulation Language (DML) statement that adds one or more new rows to a table. The simplified syntax of the INSERT statement is as follows:

INSERT [ INTO ] < table_or_view > [ ( < column_list > ) ] < data_values >

The INSERT statement inserts the < data_values > as one or more rows into the specified table or view. The < column_list > is a list of comma-separated column names that can be used to specify the columns for which data is supplied.

If the < column_list > is not specified in the INSERT statement, all the columns in the table or view receive data. When the < column_list > does not include all columns in the table or view, either the default value (if defined for the column) or NULL is inserted into any column that is not specified in the list.

Here are a few examples of how to use the INSERT statement:

  • Using INSERT to Insert a Single Row of Literal Values
INSERT INTO [dbo].[SuperHero] ( [SuperHeroName], [FirstName], [LastName] )
VALUES ( 'Superman', 'Clark', 'Kent' )
  • Using INSERT to Insert Multiple Rows of Literal Values (SQL Server 2008 Row Constructor)
INSERT INTO [dbo].[SuperHero] ( [SuperHeroName], [FirstName], [LastName] )
VALUES ( 'Superman', 'Clark', 'Kent' ),
       ( 'Batman', 'Bruce', 'Wayne' ),
       ( 'Iron Man', 'Tony', 'Stark' )
  • Using INSERT to Insert Multiple Rows of Literal Values (SQL Server 2005 and Prior SQL Server Version)
INSERT INTO [dbo].[SuperHero] ( [SuperHeroName], [FirstName], [LastName] )
SELECT 'Superman', 'Clark', 'Kent'
UNION ALL
SELECT 'Batman', 'Bruce', 'Wayne'
UNION ALL
SELECT 'Iron Man', 'Tony', 'Stark'
  • Using INSERT to Insert a Single Row of Literal Values That Does Not Yet Exist on the Destination Table
INSERT INTO [dbo].[Currency] ( [CurrencyCode], [CurrencyName] )
SELECT 'USD', 'U.S. Dollar'
WHERE NOT EXISTS (SELECT 'X' FROM [dbo].[Currency]
                  WHERE [CurrencyCode] = 'USD')
  • Using INSERT to Insert Rows Into a Table Without Specifying the Columns of the Destination Table
CREATE TABLE [dbo].[Currency] (
    [CurrencyCode]        VARCHAR(3),
    [CurrencyName]        VARCHAR(50)
)

INSERT INTO [dbo].[Currency]
VALUES ( 'USD', 'U.S. Dollar' )
  • Using INSERT to Insert Rows from Another Table
INSERT INTO [dbo].[OldStudents] ( [StudentNumber], [FirstName], [LastName] )
SELECT [StudentNumber], [FirstName], [LastName]
FROM [dbo].[CurrentStudents]
  • Using INSERT to Insert Rows from Another Table That Does Not Yet Exist on the Destination Table
INSERT INTO [dbo].[OldStudents] ( [StudentNumber], [FirstName], [LastName] )
SELECT [StudentNumber], [FirstName], [LastName]
FROM [dbo].[CurrentStudents] AS [Current]
WHERE NOT EXISTS (SELECT 'X' FROM [dbo].[OldStudents] AS [Old]
                  WHERE [Current].[StudentNumber] = [Old].[StudentNumber)