Inserting selected rows from the database

To insert data from other tables in the current database, or from a database that is defined as a Specialty Data Store to Sybase IQ, use Syntax 2 for the INSERT statement, described in Reference: Statements and Options.

This form of the INSERT statement lets you insert any number of rows of data, based on the results of a general SELECT statement.

For maximum efficiency, insert as many rows as possible in one INSERT statement. To insert additional sets of rows after the first insert, use additional INSERT statements.

Like other SQL databases, Sybase IQ inserts data by matching the order in which columns are specified in the destination column list and the select list; that is, data from the first column in the select list is inserted into the first destination column, and so on. For both INSERT SELECT and INSERT VALUES, if you omit destination column names, Sybase IQ inserts data into columns in the order in which they were created.

The tables you are inserting into must exist in the database you are currently connected to. Sybase IQ inserts the data into all indexes for the destination columns.

The columns in the table in the select-list and in the table must have the same or compatible data types. In other words, the selection’s value must be, or must be able to be converted to, the data type of the table’s column. See “Converting data on insertion” for more information about data types and conversion options.

With this form of the INSERT statement you can specify any of the insert-load-options.

For an explanation of all of these options, see “Specifying load options”.

Example

This example shows an insert from one table, partsupp, to another, lineitem, within the same database. The data from the source column l_quantity is inserted into the destination column ps_availqty.

INSERT INTO partsupp(ps_availqty)
SELECT l_quantity FROM lineitem