T-SQL INSERT INTO Table VALUES: Step-by-Step Guide
In this article
To insert rows into a SQL Server table using literal values, use the INSERT INTO statement with a VALUES clause: specify the table, list the columns you are supplying, and provide one set of values per row. For example, INSERT INTO dbo.Customers (FirstName, LastName, Email) VALUES ('Ada', 'Lovelace', 'ada@example.com'); adds one row. The column list is optional but strongly recommended because it keeps the statement working when the table structure changes and makes the order of values explicit. Before you start, you need a connection to a SQL Server instance (for example through SQL Server Management Studio or Azure Data Studio) and an existing table with appropriate data types.
What You'll Need
- A SQL Server instance and a login with INSERT permission on the target table.
- A client tool such as SQL Server Management Studio (SSMS) or Azure Data Studio, or any application that can send T-SQL.
- The table name, including the schema (commonly
dbo). - The column names and their data types, which you can check with
sp_help 'dbo.TableName'or by scripting the table in Object Explorer. - Values that match the column types and any constraints (NOT NULL, foreign keys, check constraints, unique indexes).
Step-by-Step
- Confirm the table structure. Run
sp_help 'dbo.YourTable';or expand the table in Object Explorer to see column names, data types, nullability, identity columns, defaults and constraints. This prevents type mismatches and missing required values. Menu labels in your client can differ slightly by version, device or region, but the T-SQL itself is the same. - Write the INSERT statement with an explicit column list. Start with
INSERT INTO dbo.YourTable (Column1, Column2, Column3). Put the columns in any order you like, as long as the VALUES list follows the same order. Omitting the column list is legal only when you supply a value for every column in the table's defined order, which is fragile and easy to break. - Add the VALUES clause. Use
VALUESfollowed by parentheses containing one value per listed column, separated by commas. String and date values go in single quotes; numbers do not. For example:VALUES ('Widget', 12.50, '2024-01-15');. Use unambiguous date formats such as'YYYY-MM-DD'or'YYYYMMDD'to avoid regional interpretation. - Insert multiple rows in one statement. Separate each row's parentheses with a comma:
VALUES ('A', 1), ('B', 2), ('C', 3);. SQL Server accepts up to 1,000 rows per VALUES clause; for larger loads, use multiple statements, a bulk insert method, orINSERT ... SELECT. - Handle identity and default columns. If the table has an IDENTITY column, do not include it in the column list; SQL Server generates the value. If a column has a DEFAULT, you can omit it from the list, or use the keyword
DEFAULTin the VALUES list to apply the default explicitly. To insert an explicit value into an identity column you must first runSET IDENTITY_INSERT dbo.YourTable ON;, insert, then set it back to OFF; only one table per session can have this on at a time. - Execute the statement and check the result. The Messages tab should report the number of rows affected. Verify with a quick
SELECTfiltered on the values you inserted, or useOUTPUT inserted.*in the INSERT to return the new rows immediately. - Wrap multi-statement inserts in a transaction when appropriate. Use
BEGIN TRANSACTION, the INSERT statements, thenCOMMIT; if something fails,ROLLBACKundoes the work. This matters when several related rows must succeed or fail together.
Common Mistakes
- Omitting the column list. The statement then depends on the table's physical column order and breaks when a column is added or reordered.
- Mismatched value count. The number of values must equal the number of listed columns; SQL Server raises error 110 (“There are fewer columns in the INSERT statement than values specified”) or similar.
- Quoting numbers or not quoting strings.
12.50is numeric;'12.50'is a string that may be implicitly converted. Text values must be in single quotes, and embedded apostrophes must be doubled ('O''Brien'). - Ignoring NOT NULL and foreign keys. Missing required values or referencing a parent row that does not exist causes constraint errors.
- Assuming the insert succeeded. Without checking rows affected or querying back, silent issues such as triggers modifying data can go unnoticed.
- Forgetting IDENTITY_INSERT cleanup. Leaving it ON blocks other sessions from inserting explicit identity values and can confuse later work.
Tips
- Use
OUTPUT inserted.*to see exactly what was written, including generated identity values and defaults. - Keep a consistent column order in your statements to make code review easier.
- Prefer parameterized statements from applications to avoid SQL injection and repeated ad-hoc parsing.
- For repeated test data, script the table and data from Object Explorer rather than hand-writing many VALUES rows.
- Use
SET NOCOUNT ON;in scripts to suppress per-statement row-count messages when they are not needed.
Troubleshooting
If the INSERT fails, read the error number and message first; it usually names the column or constraint involved. Common fixes:
- String or binary data would be truncated: a value is longer than the column allows. Shorten the value or widen the column if the schema permits.
- Cannot insert the value NULL into column ...: a required column was omitted or explicitly set to NULL. Supply a value or add a default.
- Violation of PRIMARY KEY or UNIQUE constraint: a duplicate key exists. Check existing rows and choose a new key or update instead.
- Foreign key conflict: insert the parent row first or correct the referencing value.
- Conversion failed: a value cannot be converted to the column's data type. Check date formats and numeric precision.
- Permission denied: the login lacks INSERT rights on the table or schema. Ask an administrator to grant the needed permission.
If the statement runs but no row appears, confirm you are querying the same database and schema, check whether a trigger rolled back the insert, and verify you committed any open transaction.
Frequently Asked Questions
Can I insert into a table without listing columns?
Yes, but only if you provide a value for every column in the table's defined order. This is fragile and not recommended; always list columns explicitly.
How do I insert multiple rows at once?
Use one VALUES clause with multiple parenthesized row sets separated by commas, up to 1,000 rows per statement. For larger volumes, use INSERT ... SELECT or a bulk method.
How do I insert a NULL value?
Write the keyword NULL in the VALUES list for that column, provided the column allows nulls. Omitting the column entirely also results in NULL unless a default exists.
Why does my identity column reject my value?
IDENTITY columns generate their own values. To supply one explicitly, enable SET IDENTITY_INSERT dbo.YourTable ON; for the session, insert, then turn it off.
What does OUTPUT inserted do?
It returns the rows that were inserted, including generated identity and default values, so you can confirm the result without a separate query.
Can I insert the result of a query?
Yes, use INSERT INTO dbo.Target (Col1, Col2) SELECT ColA, ColB FROM dbo.Source;. The selected columns must match the target list in number and compatible types.