Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

How to Use the INSERT Command in SQL Server 2017

DZone's Guide to

How to Use the INSERT Command in SQL Server 2017

The INSERT INTO statement is used to insert new records in a table. Read on to learn three techniques for using this command!

· Database Zone ·
Free Resource

MariaDB TX, proven in production and driven by the community, is a complete database solution for any and every enterprise — a modern database for modern applications.

In this article, you will learn how to insert data into a table in SQL Server. The INSERT INTO statement is used to insert new records in a table.

Specifying Column Names and Values to Be Inserted

 Syntax:

INSERT INTO TABLE_NAME (column1, column2, column3,...columnN) VALUES (value1,value2, value3,...valueN);

Example:

INSERT INTO Customers(CustomerName,Country,City,Age)
VALUES('Valiveti Sekhar','India','Bangalore',35);

Note: SQL requires single quotes around text values. However, numeric fields should not be enclosed in quotes.

Specifying Only the Values to Be Inserted

If you are adding values for all the columns of a table, you do not need to specify the column names in the SQL query. However, make sure that the order of the values is in the same order as the columns in the table.

Syntax:

INSERT INTO TABLE_NAME VALUES (value1, value2, value3,...valueN);

Example:

INSERT INTO Customers VALUES('Valiveti Sekhar','India','Bangalore',35);

Using Another Table

You can insert data into a table through the SELECT statement over another table, provided the other table has a set of fields.

Syntax:

INSERT INTO FIRST_TABLE_NAME (column1, column2, column3,...columnN)
SELECT column1, column2, ...columnN FROM SECOND_TABLE_NAME; 

Example:

INSERT INTO Customers_New(CustomerName,Country,City,Age)
SELECT CustomerName,Country,City,Age FROM Customers; 

Here's a video to clarify you anything you may be confused about:

And here are some additional resources:

MariaDB AX is an open source database for modern analytics: distributed, columnar and easy to use.

Topics:
sql server 2017 ,ms sql ,database ,tutorial ,insert

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}