11 SQL Statements for Flat-File Drivers : Insert Statement

Insert Statement
The Insert statement is used to add new rows to a database table. With it, you can specify either of the following options:
The form of the Insert statement is:
INSERT INTO table_name [(col_name, ...)]
{VALUES (expr, ...) | select_statement}
table_name can be a simple table name or a full path name. A table name is preferred for portability to other SQL data sources.
col_name is an optional list of column names giving the name and order of the columns whose values are specified in the Values clause. If you omit col_name, the value expressions (expr) must provide values for all columns defined in the file and must be in the same order that the columns are defined for the file.
expr is the list of expressions giving the values for the columns of the new record. Usually, the expressions are constant values for the columns. Character string values must be enclosed in single (’) or double (") quotation marks, date values must be enclosed in braces {}, and logical values that are letters must be enclosed in periods (for example, .T. or .F.).
An example of an Insert statement that uses a list of expressions is:
INSERT INTO emp (last_name, first_name, emp_id, salary, hire_date)
VALUES ('Smith', 'John', 'E22345', 27500, {4/6/1999})
Each Insert statement adds one record to the database table. In this case a record has been added to the employee database table, emp. Values are specified for five columns. The remaining columns in the table are assigned a blank value, meaning NULL.
select_statement is a query that returns values for each col_name value specified in the column name list. Using a Select statement instead of a list of value expressions lets you select a set of rows from one table and insert it into another table using a single Insert statement.
An example of an Insert statement that uses a Select statement is:
INSERT INTO emp1 (first_name, last_name, emp_id, dept, salary)
SELECT first_name, last_name, emp_id, dept, salary from emp
WHERE dept = 'D050'
In this type of Insert statement, the number of columns to be inserted must match the number of columns in the Select statement. The list of columns to be inserted must correspond to the columns in the Select statement just as it would to a list of value expressions in the other type of Insert statement. That is, the first column inserted corresponds to the first column selected; the second inserted to the second, and so forth.
The size and data type of these corresponding columns must be compatible. Each column in the Select list should have a data type that the driver accepts on a regular Insert/Update of the corresponding column in the Insert list. Values are truncated when the size of the value in the Select list column is greater than the size of the corresponding Insert list column.
The Select statement is evaluated before any values are inserted. This query cannot be made on the table into which values are inserted.