To insert new rows into a table in SQL Server, you can use the
INSERT INTO statement.
INSERT INTO Syntax:
The basic syntax for this statement is as follows:
INSERT INTO table_name VALUES (value1, value2, ...)
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
For example, if you have a table called "customers" with columns "id", "name", and "email", you can insert a new row into the table like this:
INSERT INTO customers (id, name, email)
VALUES (1, 'John Smith', 'firstname.lastname@example.org');
You can also use the
SELECT statement to insert data from one or more tables into a table.
INSERT INTO table1 (column1, column2, column3, ...)
SELECT column1, column2, column3, ...
You can also use the DEFAULT keyword to insert a default value into a column if you don't want to provide a specific value.
INSERT INTO table1 (column1, column2, column3)
VALUES (1, DEFAULT, 'example');
It's important to note that the order of the columns in the
INSERT INTO statement must match the order of the columns in the table, and you must provide a value for every non-nullable column in the table.
In the first example, insert into the table is done by inserting a new row without writing the columns. When the columns are not written, the values written will be inserted in the order in which the columns were created.
INSERT INTO store VALUES (5, 600, 'E');
In the second example, inserting into the table is done by writing the columns. Inserting the values will be done respecting the given order.
INSERT INTO store(object_id, price, name)
VALUES (6, 400, 'F');