The SQL INSERT statement adds new records into tables or to insert data into SQL tables. The syntax of the INSERT statement in SQL Server is
-- SQL Server INSERT Syntax INSERT INTO [Destination Table] ([Column1], [Column2],..., [ColumnN]) VALUES ([Column1_Value], [Column2_Value],..., [ColumnN_Value])
- Destination Table: Fully qualifies Table name to insert records
- Column1…..ColumnN: Choose the number of columns from the tables.
- Column1_Value…..ColumnN_Value: Values that you want to insert. For example, Column1_Value inserted for Column1. If you omit the field names, you must specify the column_values (field values) in the order defined by the destination table structure
We can write this Sql Server Insert Statement as,
-- SQL Server INSERT Syntax INSERT INTO [Destination Table] VALUES ([Column1_Value], [Column2_Value],..., [ColumnN_Value])
This table holds two records, and we will perform the insert operations on it.
SQL INSERT Statement Example
This Insert Statement example insert a new record into [SQL Insert] Table in a more traditional way. Here, we haven’t inserted the ID value because it is an identity column, and it will be updated automatically
-- SQL Server Insert Example USE [SQL Tutorial] GO INSERT INTO [dbo].[SQL Insert] ( [FirstName], [LastName], [Occupation], [YearlyIncome], [Sales]) VALUES ('Tutorial', 'Gateway', 'Education', 10000, 200)
If we are inserting data for all the Columns, then we can ignore the column names of the destination table (Syntax 2). It means the above SQL Server Insert Statement can also write as:
-- SQL Insert Statement Example INSERT INTO [dbo].[SQL Insert] VALUES ('SQL', 'Programming', 'Database', 50000, 1800)
TIP: It is not good practice to ignore the column names. So, always provide the column names in SQL Server.
Above T-SQL query will insert data into [FirstName], [LastName], [Occupation], [YearlyIncome], and [Sales] columns. Whenever you omit the field names, you must specify the column_values (field values) in the order defined by the destination table structure. It means SQL value will be inserted in [FirstName] column etc.
How to Insert Multiple records into a Table?
This Sql Server Insert Statement example insert multiple records into [SQL Insert] Table in a more traditional way.
-- SQL Server Insert Statement Example INSERT INTO [dbo].[SQL Insert] VALUES ('Virat', 'Kohli', 'Cricket', 15700, 800) INSERT INTO [dbo].[SQL Insert] VALUES ('Rohit', 'Sharma', 'Cricket', 15000, 500)
The oldest way to insert multiple records into [SQL Insert] Table.
-- SQL Server Insert Statement Example INSERT INTO [dbo].[SQL Insert] SELECT 'John', 'Levi', 'Professional', 75700, 1900 UNION ALL SELECT 'Doe', 'Martin', 'Management', 95000, 5600
Here, we use the most popular way to insert multiple records into [SQL Insert] Table. This SQL Insert Statement will work in SQL Server 2008 and later versions
-- SQL Insert Statement Example INSERT INTO [dbo].[SQL Insert] VALUES ('Imran', 'Khan', 'Skilled Professional', 15900, 100) ,('Doe', 'Lara', 'Management', 15000, 60) ,('Ramesh', 'Kumar', 'Professional', 65000, 630)
How to Insert few records into a table?
In this Sql Server Insert Statement example, we insert a few Records into [SQL Insert] table. Remember, When you are inserting a few records into the table, you must specify the column names.
-- SQL Server Insert Statement Example INSERT INTO [dbo].[SQL Insert] ( [FirstName], [Occupation], [Sales]) VALUES ('Insert', 'SQL Tutorials', 1225)
Above Insert Statement query will insert data into [FirstName], [Occupation], and [Sales] columns. NULL values is inserted in the remaining columns.
Sql Insert Into Select Statement
The Employ table we use for Insert Statement query
In this Insert Statement example, we select rows from the Employ table and insert them into a destination table. Here, we restrict the rows using the WHERE Clause.
-- SQL Server Insert Statement Example INSERT INTO [dbo].[SQL Insert] SELECT [FirstName], [LastName], [Occupation], [YearlyIncome], [Sales] FROM [SQL Tutorial].[dbo].[Employ] WHERE ID > 2
Above query insert [FirstName], [LastName], [Occupation], [YearlyIncome], and [Sales] columns from the source table where [ID] value is greater than 2 into the [SQL Insert] table.
Please refer to the INSERT INTO Select Statement article. Let us see whether the INSERT INTO Statement inserted the selected data into the destination table or not