Sei sulla pagina 1di 1

52 Chapter 2: Inserting

2.2.2 INSERT Named Values


If you want to use a VALUES list but dont want to list every single value, or
you want to rearrange the order of the values, you can add a column name list to
the INSERT.
<insert_named_values> ::= INSERT [ INTO ]
[ <owner_name> "." ] <target_table_name>
"(" <column_name_list> ")"
[ <on_existing> ]
VALUES "(" <named_values_list> ")"
<column_name_list> ::= <column_name> { "," <column_name> }
<column_name> ::= <identifier>
<named_values_list> ::= <value_list> -- for the named columns
When you use this kind of INSERT, the expressions in the VALUES list are
applied to the columns in the order they are specified in the column name list,
and the number of values must match the number of column names. Missing
columns are assigned their default values.

Note: All columns have default values whether or not you code explicit
DEFAULT clauses when creating the tables; the default DEFAULT is NULL.

Here is an example where 'A' is placed in col_2 and 'B' in col_3, out of order,
and all the other columns are assigned their default values:
CREATE TABLE t1 (
key_1 INTEGER NOT NULL DEFAULT AUTOINCREMENT,
col_2 VARCHAR ( 100 ) NOT NULL DEFAULT 'X',
col_3 VARCHAR ( 100 ) NOT NULL DEFAULT 'Y',
updated_by VARCHAR ( 128 ) NOT NULL DEFAULT LAST USER,
last_updated TIMESTAMP NOT NULL DEFAULT TIMESTAMP,
PRIMARY KEY ( key_1 ) );

INSERT t1 ( col_3, col_2 ) VALUES ( 'B', 'A' );


In the CREATE TABLE above, every single column has a DEFAULT value.
That means the following insert will work just fine, and you can execute it over
and over again without error:
INSERT t1 ( key_1 ) VALUES ( DEFAULT );
However, thats as easy as it gets; you cannot write INSERT t1() VALUES().

Tip: If you have a feature youd like to see in SQL Anywhere, dont be afraid
to make the suggestion. Thats how ON EXISTING got added to the INSERT
statement a request was posted in the public newsgroup called sybase.pub-
lic.sqlanywhere.product_futures_discussion, which is located on the NNTP server
at forums.sybase.com. You can post to this newsgroup with NNTP client software
like Forte Agent, or use your web browser to go to www.ianywhere.com/devel-
oper and click on Newsgroups. Not every suggestion is implemented, but every
suggestion is taken seriously by the folks responsible for product development.

Potrebbero piacerti anche