Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

MS SQL INSERT INTO Table VALUES: How-To Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert a row into a Microsoft SQL Server table with literal values, use the INSERT INTO ... VALUES statement: INSERT INTO dbo.Customers (FirstName, LastName, Email) VALUES ('Ada', 'Lovelace', 'ada@example.com');. That single statement adds one row. You can add several rows in one statement by separating each value list with a comma. Nothing needs to be installed beyond access to a SQL Server instance and a tool such as SQL Server Management Studio (SSMS) or Azure Data Studio, plus permission to insert into the target table.

What You'll Need

  • A connection to a SQL Server instance (on-premises, Azure SQL, or another hosted SQL Server).
  • A client tool: SQL Server Management Studio (SSMS) or Azure Data Studio on desktop. Labels in these tools can differ slightly by version.
  • The database name and the table name, including the schema (commonly dbo).
  • Permission to insert rows into that table (for example, membership in a role that has INSERT granted).
  • Knowledge of the table's column names, data types, and which columns allow NULL or have defaults.

Step-by-Step

  1. Connect and select the database. Open SSMS or Azure Data Studio, connect to your server, then either pick the database from the toolbar dropdown or run USE YourDatabaseName; followed by GO. This ensures the table is resolved in the right database.
  2. Inspect the table definition. In Object Explorer, expand the database, then Tables, right-click the target table and choose Design (or run sp_help 'dbo.YourTable';). Note the column names, data types, whether each column allows NULL, and any default constraints or identity columns. This prevents type mismatches and missing-value errors.
  3. Write the INSERT with an explicit column list. The safest form names the columns in the order you supply values: INSERT INTO dbo.Products (ProductName, Price, InStock) VALUES ('Widget', 9.99, 1);. The column list makes the statement resilient to column order changes and to new columns added later.
  4. Match values to data types. Put single quotes around text, dates, and times (for example '2024-05-01'), and leave numbers unquoted. Use NULL without quotes for a missing value where the column allows it. For bit columns, use 1 or 0.
  5. Insert multiple rows in one statement. Separate value lists with commas: INSERT INTO dbo.Products (ProductName, Price, InStock) VALUES ('Widget', 9.99, 1), ('Gadget', 19.50, 0), ('Doohickey', 4.25, 1);. SQL Server treats this as one statement and either inserts all listed rows or none if the statement fails.
  6. Run the statement. In SSMS, press F5 or click Execute. The Messages tab should report the number of rows affected, for example "(1 row affected)" or "(3 rows affected)".
  7. Verify the result. Run a SELECT filtered to the new rows, such as SELECT * FROM dbo.Products WHERE ProductName IN ('Widget','Gadget','Doohickey');. Check that values landed in the intended columns and that defaults or identity values were generated as expected.
  8. Handle identity columns. If the table has an identity column, do not supply a value for it in a normal INSERT; SQL Server generates it. Only if you must insert an explicit value do you need SET IDENTITY_INSERT dbo.YourTable ON; around the statement, then turn it OFF again. This is an advanced step and should be used sparingly.

Common Mistakes

  • Omitting the column list. INSERT INTO dbo.Products VALUES (...) requires a value for every column in table order, including identity and computed columns, and breaks when the schema changes.
  • Value count mismatch. The number of values must equal the number of columns listed. A mismatch raises error 110 or a similar "column name or number of supplied values does not match table definition" message.
  • Quoting numbers or not quoting text. Putting quotes around a numeric value can trigger implicit conversion, and leaving quotes off a text value causes "Invalid column name" errors because SQL Server reads the bare word as an identifier.
  • Exceeding column length. Inserting a string longer than a varchar(n) or nvarchar(n) column allows raises a truncation error (error 8152) and the row is rejected.
  • Violating constraints. Duplicate values in a primary key or unique index, NULLs in NOT NULL columns, or values rejected by a CHECK constraint all cause the insert to fail.
  • Forgetting the schema. If the default schema for your login is not dbo, an unqualified table name may resolve to the wrong table or fail to resolve at all.
  • Assuming partial success with multi-row VALUES. A single INSERT statement is atomic; if one row fails, none of the rows in that statement are inserted.

Tips

  • Always list columns explicitly in production scripts. It is the single biggest safeguard against schema drift.
  • Use N'text' for Unicode strings when inserting into nvarchar columns to avoid losing characters outside the code page.
  • Wrap a batch of inserts in a transaction (BEGIN TRANSACTION ... COMMIT) when you need all-or-nothing behavior across several statements, and use ROLLBACK if something looks wrong.
  • For large imports, prefer bulk methods such as BULK INSERT or the Import and Export Wizard over thousands of individual VALUES statements.
  • Test the statement inside a transaction with a SELECT afterward, then roll back, before running it against production data.
  • Keep a script of your inserts under version control so schema changes and data fixes are traceable.

Troubleshooting

If the statement fails, read the error number and message first; SQL Server is usually specific. For "Invalid object name", check the database context and schema. For "Cannot insert the value NULL into column", either supply a value or confirm the column has a default. For "String or binary data would be truncated", shorten the value or widen the column. For "Violation of PRIMARY KEY constraint", find the duplicate with a SELECT before retrying. For permission errors, confirm your login has INSERT on the table or schema. If the row count is zero with no error, check whether a trigger or an INSTEAD OF INSERT rule is intercepting the statement. If you are unsure, run the same INSERT inside a transaction, inspect the result with a SELECT, and roll back.

Frequently Asked Questions

Can I insert multiple rows in one statement?

Yes. Separate each set of values with a comma after the VALUES keyword. The whole statement succeeds or fails as a unit.

Do I have to list every column?

No. List only the columns you are supplying. Columns you omit must allow NULL or have a default constraint, otherwise the insert fails.

How do I insert the current date and time?

Use the GETDATE() function as a value in the VALUES list, or let a column default handle it if one is defined.

Why does my INSERT fail with a conversion error?

The value's type does not match the column's type. Quote dates and text, leave numbers unquoted, and check for regional date formats that SQL Server may interpret differently.

Can I insert into a view?

Only if the view is updatable and the insert maps to a single base table. Otherwise SQL Server returns an error and you must insert into the underlying table.

What does "rows affected" mean?

It is the count of rows the statement inserted. A zero with no error usually means a trigger or filter prevented the insert.