INSERT statement in SQL Server

The INSERT statement in SQL Server (Transact-SQL) is used to insert one or more records into a table.

INSERT command syntax

Insert a record

In its simplest form, the INSERT command syntax inserts a record with the VALUES keyword

 INSERT INTO  bang 
(cot1, cot2, …)
VALUES
(bieuthuc1, bieuthuc2, …),
(bieuthuc1, bieuthuc2, …),
. ;

The full syntax of the INSERT command inserts a record using VALUES keyword

 INSERT INT O bang 
(cot1, cot2, …)
VALUES
( DEFAULT | NULL | bieuthuc1,
DEFAULT | NULL | bieuthuc2,

);

Or the syntax to insert a record using the keyword DEFAULT VALUES

 INSERT INT O bang 
(cot1, cot2, …)
DEFAULT VALU ES;

Insert multiple records

In the simplest form, the syntax for inserting multiple records is by sub-select

 INSERT I NTO bang 
(cot1, cot2, …)
SELECT bieuthuc1, bieuthuc2, …
FROM bang_nguon
[WHERE dieuk ien];

Full syntax for inserting multiple desks with sub-select

 INSERT [TOP ( tri_dau) [PERCENT] ] 
INTO bang
(cot1, cot2, …)
SELECT bieuthuc1, bieuthuc2, …
FROM bang_nguon
[WHERE dieukien];

Variable name or variable value

state

Table to insert records into.

cot1, cot2

Columns in the table to insert values

bieuthuc1, bieuthuc2

Value to specify in the column in the table. cot1 will be assigned the value of bieuthuc1, cot2 will be assigned the value of bieuthuc2 .

TOP (giatri_dau)

Option. If specifically, it will insert the first value of the row based on giatri_dau. For example, TOP (10) will insert the first 10 rows from the result set.

PERCENT

Option. If specified, the first rows are based on the percentage of giatri_dau of the result set. For example, TOP (10) PERCENT will insert 10% of the first value in the result set.

bang_nguon

Source table (original table) to insert data from another table.

WHERE dieukien

Option. Sending conditions are met so that the record is inserted.

Note

  1. When inserting records into a table with the INSERT statement, you must provide values ​​for NOT NULL columns.
  2. You can remove columns from the INSERT statement if the column allows NULL values.

For example - use the keyword VALUES

 INSERT INT O nhanvien 
(nhanvien_id, ten, ho)
VALUE
(10, 'Anderson', 'S arah');

This INSERT statement will result in 1 record being inserted into the table. This record must have nhanvien_id of 10, they are Anderson and their name is Sarah.

This syntax can be used to insert more than 1 record at a time. Eg:

 INSERT INTO anvien 
(nhanvien_id, ho, ten)
VALUES
(19, 'Anderson', 'Sarah'),
(11, 'Johnson', 'Dale') ;

For example, the INSERT statement above shows that it is possible to insert more than 1 record with the VALUES keyword. In this example, 2 records are inserted into the table. The first record has 10 minutes, they are Anderson and their name is Sarah. The second record has nhanvien_id is 11, they are Johnson and the name is Dale.

The above command is equivalent to the INSERT statements below.

 INSERT INTO anvien 
(nhanvien_id, ho, ten)
VALUES
(10, 'Anderson', 'Sa rah');
 INSERT INTO anvien 
(nhanvien_id, ho, ten)
VALUES
(11, 'Johnson', 'Dal e');

For example - use the keyword DEFAULT VALUES

 INSERT IN TO nhanvien 
(nhanvien_id, ho, ten)
DEFAULT VA LUES;

This INSERT statement inserts a record into the table. New records are created by default values ​​for employee ID, family and name information fields.

For example - use the SELECT command

It is possible to create a more complex INSERT statement with the SELECT statement as shown below.

 INSERT INTO da nhba 
(danhba_id, ho, ten)
SELECT nhanvien_id, ho, ten
FROM nhanvien
WHERE nhanvien_id <= 100;

By placing the SELECT statement in the INSERT command, you can make more inserts faster.

With this type of insert, you may want to check the number of rows to be inserted, determine the number of rows to be inserted by running the SELECT command before inserting.

 SELECT c ount (*) 
FROM nhanvien
WHERE nha nvien_id <= 100;

Previous article: BETWEEN conditions in SQL Server

The following article: UPDATE command in SQL Server

4.5 ★ | 2 Vote

May be interested

  • How to Write an Artist's StatementHow to Write an Artist's Statement
    it is quite hard to write a really good artist statement, even if you're a good writer. the statement accompanies a piece or set of work you've painted, drawn, photographed, created, and it must sum up such elements as motivation,...
  • WHERE clause in SQL ServerWHERE clause in SQL Server
    in sql server) t-sql), the where clause is used to filter results from select, insert, update, or delete statements.
  • NULLIF function in SQL ServerNULLIF function in SQL Server
    this article will show you in detail how to use the nullif function handler in sql server with specific syntax and examples to better visualize and capture functions.
  • The difference between Truncate and Delete in Microsoft SQL ServerThe difference between Truncate and Delete in Microsoft SQL Server
    in the following article, we will help you distinguish some basic differences between two delete syntax and truncate table in microsoft sql server application. basically, both of these statements help us to remove the data, but in essence it is not so.
  • ISNULL function in SQL ServerISNULL function in SQL Server
    in sql server, the isnull function allows you to return an alternate value when an input expression is null.
  • GROUP BY clause in SQL ServerGROUP BY clause in SQL Server
    the group by clause in sql server (transact-sql) is used in the select statement to retrieve data from multiple records and result groups into 1 or more columns.
  • ISNUMERIC function in SQL ServerISNUMERIC function in SQL Server
    the isnumeric function in sql server checks whether the value of the passed expression is a valid numeric value, if there is isnumeric returns 1, otherwise it returns 0.
  • The difference between web server and app serverThe difference between web server and app server
    you have probably seen that the terms web server and app server are often used interchangeably as if they are related to the same thing and also facilitate the website to function properly. but in reality, they are not the same.
  • How to use the Insert key to insert copied text in Word?How to use the Insert key to insert copied text in Word?
    the insert key is also used as a shortcut to insert text to copy or cut text content at the current cursor position. if you don't know how to use the insert key to insert copied text in word, please refer to the following article of
  • JOIN in SQL ServerJOIN in SQL Server
    join is used to retrieve data from multiple tables, occurring when two or more tables are connected together in an sql statement.