SQL programming / SQL basics |
An implicit database connection is made with the DATABASE instruction used before MAIN; use SCHEMA to avoid the implicit connection.
The DATABASE statement can be used in two distinct ways, depending on the context of the statement within its source module:
Typically used in a GLOBALS module, to define variables with the DEFINE ... LIKE, but it is also used for the INITIALIZE and VALIDATE statements. Using the DATABASE statement in this way results in that database being opened automatically at run time.
In MAIN or in a FUNCTION, used to connect to a database. A variable can be used in this context ( DATABASE varname ).
DATABASE stock_dev -- Default database, used at compile time DEFINE p_cust RECORD LIKE customer.* MAIN -- Connection to default database occurs at MAIN DEFINE dbname CHAR(30) LET dbname = "stock1" DATABASE dbname -- Real database used in production ... END MAIN
SCHEMA stock_dev -- Schema specification only DEFINE p_cust RECORD LIKE customer.* MAIN -- No default connection occurs... DEFINE dbname CHAR(30) LET dbname = "stock1" DATABASE dbname END MAIN
This instruction will define the database schema for compilation only, and will not make an implicit connection at runtime.