INSERT Statement

Inserts a single row or a selection of rows, from elsewhere in the current database, into the table. This command can also insert a selection of rows from another database into the table.

Quick Links:

Go to Parameters

Go to Examples

Go to Usage

Go to Standards

Go to Permissions

Syntax

Syntax 1

INSERTINTO ] [ owner.]table-name [ ( column-name [, …] ) ]
    ... VALUES ( [ expression | DEFAULT,… ) ]
or
   INSERTINTO ] [ owner.]table-name DEFAULT VALUES

Syntax 2

INSERTINTO ] [ owner.]table-name [ ( column-name [, …] ) ]
   ... insert-load-options insert-select-load-options
   ... select-statement

Syntax 3

INSERTINTO ] [ owner.]table-name[ ( column-name [, …] ) ]
    ... insert-select-load-options insert-select-load-options
  LOCATION 'servername.dbname'
   [ location-options ]
   ... { { select-statement } | ‘select statement’ }
insert-load-options - (back to Syntax 2) or (back to Syntax 3)
LIMIT number-of-rows ] 
   [ NOTIFY number-of-rows ] 
   [ SKIP number-of-rows ] 

insert-select-load-options - (back to Syntax 2) or (back to Syntax 3)WORD SKIP number ]
   [ IGNORE CONSTRAINT constraint-type [, …] ] 
   [ MESSAGE LOGstringROW LOGstring’ [ ONLY LOG logwhat [, …] ] ] 
   [ LOG DELIMITED BYstring’ ]

constraint-type - (back to insert-select-load-options)
   { CHECK integer 
   | UNIQUE integer 
   | NULL integer 
   | FOREIGN KEY integer 
   | DATA VALUE integer 
   } ALL integer 
   }

logwhat - (back to insert-select-load-options)
CHECK 
   | ALL 
   | NULL
   | UNIQUE
   | DATA VALUE
   | FOREIGN KEY
   | WORD 
   }

Parameters

(back to top)

Note: For additional information on the insert-select-load-options and location-options as well as the constraint-type and logwhat parameters, see the LOAD TABLE Statement.

Examples

(back to top)

Usage

(back to top)

Syntax 1 allows the insertion of a single row with the specified expression values. If the list of column names is not specified, the values are inserted into the table columns in the order they were created (the same order as retrieved with SELECT *). The row is inserted into the table at an arbitrary position. (In relational databases, tables are not ordered.)

Syntax 2 allows the user to perform a mass insertion into a table using the results of a fully general SELECT statement. Insertions are done in an arbitrary order unless the SELECT statement contains an ORDER BY clause. The columns from the select list are matched ordinally with the columns specified in the column list, or sequentially in the order in which the columns were created.

Note: The NUMBER(*) function is useful for generating primary keys with Syntax 2 of the INSERT statement.

Syntax 3 INSERT...LOCATION is a variation of Syntax 2 that allows you to insert data from an Adaptive Server or SAP Sybase IQ database. The servername.dbname specified in the LOCATION clause identifies the remote server and database for the table in the FROM clause. To use Syntax 3, the Adaptive Server or SAP Sybase IQ remote server to which you are connecting must exist in the SAP Sybase Open Client interfaces or sql.ini file on the local machine.

In queries using Syntax 3, you can insert a maximum of 2147483647 rows.

The SELECT statement can be delimited by either curly braces or straight single quotation marks.
Note: Curly braces represent the start and end of an escape sequence in the ODBC standard, and might generate errors in the context of ODBC or SAP Control Center. The workaround is to use single quotes to escape the SELECT statement.

The local SAP Sybase IQ server connects to the server and database you specify in the LOCATION clause. The results from the queries on the remote tables are returned and the local server inserts the results in the current database. If you do not specify a server name in the LOCATION clause, SAP Sybase IQ ignores any database name you specify, since the only choice is the current database on the local server.

When SAP Sybase IQ connects to the remote server, INSERT...LOCATION uses the remote login for the user ID of the current connection, if a remote login has been created with CREATE EXTERNLOGIN and the remote server has been defined with a CREATE SERVER statement. If the remote server is not defined, or if a remote login has not been created for the user ID of the current connection, SAP Sybase IQ connects using the user ID and password of the current connection.

Note: If you rely on the user ID and password of the current connection, and a user changes the password, you must stop and restart the server before the new password takes effect on the remote server. Remote logins created with CREATE EXTERNLOGIN are unaffected by changes to the password for the default user ID.

Creating a remote login with the CREATE EXTERNLOGIN statement and defining a remote server with a CREATE SERVER statement sets up an external login and password for INSERT...LOCATION such that any user can use the login and password in any context. This avoids possible errors due to inaccessibility of the login or password, and is the recommended way to connect to a remote server.

For example, user russid connects to the SAP Sybase IQ database and executes this statement:

INSERT local_SQL_Types LOCATION ‘ase1.ase1db’
{SELECT int_col FROM SQL_Types};

On server ase1, there exists user ID ase1user with password sybase. The owner of the table SQL_Types is ase1user. The remote server is defined on the IQ server as:

CREATE SERVER ase1 CLASS ‘ASEJDBC’
USING ‘system1:4100’;

The external login is defined on the IQ server as:

CREATE EXTERNLOGIN russid TO ase1 REMOTE LOGIN ase1user IDENTIFIED BY sybase;

INSERT...LOCATION connects to the remote server ase1 using the user ID ase1user and the password sybase for user russid.

Use the ENCRYPTED PASSWORD parameter to specify the use of Open Client Library default password encryption when connecting to a remote server. If ENCRYPTED PASSWORD is specified and the remote server does not support Open Client Library default password encryption, an error is reported indicating that an invalid user ID or password was used.

When used as a remote server, SAP Sybase IQ supports TDS password encryption. The SAP Sybase IQ server accepts a connection with an encrypted password sent by the client. For information on connection properties to set for password encryption, see Software Developer's Kit 15.5 > Open Client Client-Library/C Reference Manual > Client-Library Topics > Security features > Adaptive Server security features > Security handshaking: encrypted password for Open Server 15.5.

Note: Password encryption requires Open Client 15.0. TDS password encryption requires Open Client 15.0 ESD #7 or later.

When INSERT...LOCATION is transferring data between an SAP Sybase IQ server and a remote SAP Sybase IQ or Adaptive Server server, the value of the INSERT...LOCATION TDS PACKETSIZE parameter is always 512 bytes, even if you specify a different value for PACKETSIZE.

Note: If you specify an incorrect packet size (for example 933, which is not a multiple of 512), the connection attempt fails with an Open Client ct_connect “Connection failed” error. Any unsuccessful connection attempt returns a generic “Connection failed” message. The Adaptive Server error log might contain more specific information about the cause of the connection failure.

SAP Sybase IQ does not support the Adaptive Server data type TEXT, but you can execute INSERT...LOCATION (Syntax 3) from both an IQ CHAR or VARCHAR column whose length is greater than 255 bytes, and from an ASE database column of data type TEXT. ASE TEXT and IMAGE columns can be inserted into columns of other SAP Sybase IQ data types, if SAP Sybase IQ supports the internal conversion. By default, if a remote data column contains over 2GB, SAP Sybase IQ silently truncates the column value to 2GB.

Warning!   SAP Sybase IQ does not support the Adaptive Server data types UNICHAR, UNIVARCHAR, or UNITEXT. An INSERT...LOCATION command from UNICHAR or UNITEXT to CHAR or CLOB columns in the ISO_BINENG collation may execute without error; if this happens, the data in the columns may be inconsistent. An error is reported in this situation, only if the conversion fails.

Users must be specifically licensed to use the large object functionality of the Unstructured Data Analytics Option.

Note: If you use INSERT...LOCATION to insert data selected from a VARBINARY column, set ASE_BINARY_DISPLAY to OFF on the remote database.

INSERT...LOCATION (Syntax 3) does not support the use of variables in the SELECT statement.

Inserts can be done into views, provided the SELECT statement defining the view has only one table in the FROM clause and does not contain a GROUP BY clause, an aggregate function, or involve a UNION operation.

Character strings inserted into tables are always stored in the case they are entered, regardless of whether the database is case-sensitive or not. Thus, a string “Value” inserted into a table is always held in the database with an uppercase V and the remainder of the letters lowercase. SELECT statements return the string as 'Value.' If the database is not case-sensitive, however, all comparisons make 'Value' the same as 'value,' 'VALUE," and so on. Further, if a single-column primary key already contains an entry Value, an INSERT of value is rejected, as it would make the primary key not unique.

Whenever you execute an INSERT...LOCATION statement, SAP Sybase IQ loads the localization information needed to determine language, collation sequence, character set, and date/time format. If your database uses a nondefault locale for your platform, you must set an environment variable on your local client to ensure that SAP Sybase IQ loads the correct information.

If you set the LC_ALL environment variable, SAP Sybase IQ uses its value as the locale name. If LC_ALL is not set, SAP Sybase IQ uses the value of the LANG environment variable. If neither variable is set, SAP Sybase IQ uses the default entry in the locales file.

Use the (DEFAULT), DEFAULT VALUES or VALUES() clauses to insert rows with all default values. Assuming that there are 3 columns in table t2, these examples are semantically equivalent:

INSERT INTO t2 values (DEFAULT, DEFAULT, DEFAULT);
INSERT INTO t2 DEFAULT VALUES;
INSERT INTO t2() VALUES();

INSERT...VALUES also supports multiple rows. The following example inserts 3 rows into table t1:

CREATE TABLE t1(c1 varchar(30));
INSERT INTO t1 VALUES  ('morning'),('afternoon'),
    ('evening');

SAP Sybase IQ treats all load/inserts as full-width inserts. Columns not explicitly specified on the load/insert statement, the value loaded will either be the column’s DEFAULT value (if one is defined) or NULL (if no DEFAULT value is defined for the column).

SAP Sybase IQ supports column DEFAULT values for INSERT...VALUES, INSERT...SELECT, and INSERT...LOCATION. If a DEFAULT value is specified for a column, this DEFAULT value is used as the value of the column in any INSERT (or LOAD) statement that does not specify a value for the column.

An INSERT from a stored procedure or function is not permitted, if the procedure or function uses COMMIT, ROLLBACK, or some ROLLBACK TO SAVEPOINT statements.

The result of a SELECT…FROM may be slightly different from the result of an INSERT…SELECT…FROM due to an internal data conversion of an imprecise data type, such as DOUBLE or NUMERIC, for optimization during the insert. If a more precise result is required, a possible workaround is to declare the column as a DOUBLE or NUMERIC data type with a higher precision.

Standards

(back to top)

  • SQL—ISO/ANSI SQL compliant.
  • SAP Sybase Database product—Supported by Adaptive Server (excluding the insert-load-options).

Permissions

(back to top)

Requires INSERT privilege on the table.

Related reference
CREATE EXTERNLOGIN Statement
DELETE Statement
LOAD TABLE Statement