The INSERT statement in SQL Server is used to add new rows to a table.


Single Row Insert:

  • Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
 
-- OR
INSERT INTO table_name
VALUES (value1, value2, ...);
-- You should write a +values for all columns in the table
  • Example:
INSERT INTO employees (id, name, age)
VALUES (1, 'John Doe', 30);

Multiple Rows Insert:

  • Syntax:
INSERT INTO table_name (column1, column2, ...)
VALUES 
	(value1a, value2a, ...),
	(value1b, value2b, ...);
  • Example:
INSERT INTO employees (id, name, age)
VALUES 
	(2, 'Jane Smith', 28),
	(3, 'Alice Brown', 35);

Why if the insert failed, the identity still increment with 1

Key Points:

  1. Specify column names to avoid errors due to column order.
  2. Use multiple rows insert for better performance compared to executing multiple single-row inserts.
  3. Ensure data types match the column definitions.