Load data using the 'Insert Statement' in SQL-Server
The simplest way to load data into a database is to use the insert statement.
The insert statement can either insert one row or multiple rows into a table.
The simplified syntax of the insert statement is shown in the following MouseOver:
table_name The name of the table into which you will insert data.
column_list A comma-separated list of the columns to insert data into. If this list is not provided, you will insert data into all columns in the table.
VALUES Indicates that you are going to provide one row of data.
DEFAULT Uses the default value from the table definition for the associated column.
NULL Sets the associated column to NULL.
expression Sets the associated column to the expression.
select_statement Any select statement that does not use the compute clause.
execute_statement You can execute any stored procedure that returns data.
DEFAULT VALUES Used to specify that you want to use the default for every column in the table
Column List Parameter
When you are inserting data into a table, use the column list parameter to specify the columns in the table in which you want to insert data. If you do not specify a specific column from the table, then SQL Server will generate an error and abort the insert, EXCEPT in the following cases:
If the existing columns in the table are identity columns, the next identity value will be inserted into the column.
If the existing columns in the table have a default, the default value will be inserted into the column.
If the existing columns datatype is timestamp, SQL Server will provide it with the current timestamp value.
If the existing column is nullable, it will get a null value.
You cannot insert a value into derived columns or into identity columns .
Derived columns:A calculated column. SQL Server allows you to define derived columns as part of the table definition.
By executing a stored procedure or having a select statement as part of the insert statement,
you can insert multiple rows of data into an existing table.
The following example illustrates how to insert one row into a table.
INSERT INTO MyTable ( FirstName, LastName, PhoneNumber)
VALUES ('John','Doe','111-111-1111')
The following example illustrates how to insert multiple rows into a table.
INSERT INTO MyTable (FirstName, LastName, PhoneNumber)
SELECT Fname, Lname, HomePhone
FROM Employee
INSERT INTO MyTable (FirstName, LastName, PhoneNumber)
EXECUTE GetPerson @SearchFor = 'A%'
SQL Server provides you the means of creating a table and adding rows to the table in one step. The next lesson will cover using the select into statement to perform this task.