How to Insert Values into a Table in SQL Server

In this tutorial you will learn how to insert values into a table in SQL Server. If you used any type of database language, whether it be in Visual Studio or Access, the syntax should look familiar to you. Knowing how to insert values into a table is very important because a table needs data in order to be useful. We will make use of INSERT INTO and VALUES keywords. The syntax in SQL Server may look weird if you have not programmed with databases before but the keywords make sense if you look at it, for example:

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

We will first specify a table and its columns. We will then enter values into the same order that we entered the columns. So we will insert value1 into column1 of table_name. Make sense? It will make more sense when we add actual names to the table, columns and values.

Open Microsoft SQL Server and connect to a database or server compact. Create a table called Employees with details of the employee as shown below:



We used the IDENTITY keyword to auto increment the EmpId column, thus giving each employee a different ID. Click the execute button and make sure you get a message that says “Command(s) completed successfully.” that confirms the table was created.

So now that we have an Employees table, let’s add an employee into it. We will use the same syntax from the example above but switch out the generic names for the names in our table.



In the code box above we first clarify that we will enter values into the Employees table. Inside the parenthesis we clarify the columns in the table with FirstName, LastName, HireDate, MgrId, Ssn, and Salary. Notice how we did not include the EmpId column. It is not necessary in this case because since we set it to auto increment with the IDENTITY keyword, it will increment automatically without input, so there is no need for us to declare an ID for the employee.

So since we specified the columns in the table, we must enter the appropriate values in the same order that we declared the columns in the INSERT INTO statement. So the connection will look like this:

FirstName ‘Tom’
LastName ‘Johnson’
HireDate ’20110101 12:00:00:000′
MgrId 21
Ssn 12345678
Salary 32000

Click the execute button and if you get a message that says “(1 row(s) affected)” you have successfully added an employee into the Employees table.

Thanks for reading and make sure to download the source files to get a better understanding of how the code works.

Leave a Reply

Your email address will not be published. Required fields are marked *