Tip of the Day : SQL Server Tutorial - INSERT Statement 101

Welcome to SQL Server Helper !!!

This site is intended for those who are beginning to use SQL Server as part of their day-to-day activities.  You will find in this site a collection of useful functions, triggers, stored procedures and tips and tricks related to SQL Server.

Should you have any comments or questions regarding this site or if you want to ask SQL Server-related questions, e-mail us here.

We hope we are able to help and we are glad to help!!!

SQL Server Tip of the Day - February 16, 2019

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)

SQL Server 2012

SQL Server 2008

User-Defined Functions

Date Functions

A collection of useful user-defined functions that deal with dates.

String Functions

A collection of useful user-defined functions that deal with strings (varchar/char/nvarchar/nchar).

Tree Functions

A collection of useful user-defined functions that deal with tree or hierarchical design structures.

Table-Valued Functions

A collection of useful table-valued user-defined functions that can be used to join with other tables.

SQL Server Built-in Functions

A reference to all built-in functions available within SQL Server grouped into categories.

Tips and Tricks

A collection of useful SQL Server-related tips and tricks:

SQL Server Error Messages

A list of SQL Server error messages and for certain error messages, discusses ways on how to solve the error or work around them:

Frequently Asked Questions