Using VALUES to Insert Columns

From InterBase

Go Up to Inserting Data (Embedded SQL Guide)


Use the VALUES clause to add a row of specific values to a table, or to add values entered by a user at run time. The list of values that follows the keyword can come from either from host-language variables, or from hard-coded assignments.

For example, the following statement adds a new row to the DEPARTMENT table using hard-coded value assignments:

EXEC SQL
INSERT INTO DEPARTMENT (DEPT_NO, DEPARTMENT)
VALUES (7734, 'Marketing');

Because the DEPARTMENT table contains additional columns not specified in the INSERT, NULL values are assigned to the missing fields.

The following C code example prompts a user for information to add to the DEPARTMENT table, and inserts those values from host variables:

. . .
EXEC SQL
BEGIN DECLARE SECTION;
char department[26], dept_no[16];
int dept_num;
EXEC SQL
END DECLARE SECTION;
. . .
printf("Enter name of department: ");
gets(department);
printf("\nEnter department number: ");
dept_num = atoi(gets(dept_no));
EXEC SQL
INSERT INTO COUNTRIES (DEPT_NO, DEPARTMENT)
VALUES (:dept_num, :department);

When host variables are used in the values list, they must be preceded by colons (:) so that SQL can distinguish them from table column names.

Advance To: