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!
Join the DZone community and get the full member experience.
Join For FreeIn 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:
Opinions expressed by DZone contributors are their own.
Comments