Multiple Database Implementation
Go Up to Using Strings or host-language Variables
To use a database specified by the user as a host-language variable in a CONNECT
statement in multi-database programs, follow these steps:
- Declare a database handle using the following
SET DATABASE
syntax:EXEC SQL SET DATABASE handle = COMPILETIME 'dbname';
Here, <handle> is a hard-coded database handle supplied by the programmer, <dbname> is a quoted, hard-coded database name used bygpre
during preprocessing. - Prompt the user for a database to open.
- Store the database name entered by the user in a host-language variable.
- Use the handle to open the database, associating the host-language variable with the handle using the following
CONNECT
syntax:EXEC SQL CONNECT :variable AS handle;
The following C code illustrates these steps:
. . . char fname[125]; . . . EXEC SQL SET DATABASE DB1 = 'employee.ib'; printf("Enter the desired database name, including node and path):\n"); gets(fname); EXEC SQL CONNECT :fname AS DB1; . . .
In this example, SET DATABASE
provides a hard-coded database file name for preprocessing with gpre
. When a user runs the program, the database specified in the variable, fname,
is used instead.