INSERT (Transact‑SQL)
The INSERT statement adds new rows to a table or view in a SQL Server database.
Syntax
INSERT [TOP (expression) [PERCENT]]
[INTO] <object> [<column_list>]
{ DEFAULT VALUES |
<values_list> |
<select_statement> }
[;]
Parameters
| Parameter | Description |
|---|---|
TOP (expression) [PERCENT] |
Limits the number of rows inserted. |
INTO |
Optional keyword that indicates the target table. |
<object> |
Name of the table or view to receive the new rows. |
<column_list> |
Comma‑separated list of columns to receive values. If omitted, values must be supplied for all columns. |
DEFAULT VALUES |
Inserts a row with default values for all columns. |
<values_list> |
One or more VALUES clauses that specify the data to insert. |
<select_statement> |
A SELECT statement that returns the rows to be inserted. |
Examples
Insert a single row using VALUES
INSERT INTO dbo.Products (ProductName, UnitPrice, Discontinued) VALUES (N'Gadget Pro', 199.99, 0);
Insert multiple rows
INSERT INTO dbo.Employees (FirstName, LastName, HireDate)
VALUES
(N'John', N'Doe', '2023-01-15'),
(N'Jane', N'Smith', '2023-02-20');
Insert rows from a SELECT statement
INSERT INTO dbo.ArchiveOrders (OrderID, CustomerID, OrderDate) SELECT OrderID, CustomerID, OrderDate FROM dbo.Orders WHERE OrderDate < '2022-01-01';
Remarks
- When inserting data, ensure column data types match the values provided.
- Use
IDENTITY_INSERTif you need to provide explicit values for anIDENTITYcolumn. - Transactions can be used to guarantee atomicity for multiple
INSERTstatements. - For large data loads, consider
BULK INSERTor thebcputility.