A SELECT clause that specifies the storing of selected values in host variables.
Synopsis
INTO :var1 [,:var2]...
Arguments
:var1 A host variable that has been declared in the host language. When specified in an INTO clause, the variable name is preceded by a colon (:). Multiple variables can be specified as a comma-separated list. Subscripted variables can be used.
Description
The INTO clause of a SELECT statement uses the field values retrieved (or calculated) by the SELECT to set a corresponding set of host variables. This optional INTO clause appears after the select-item list and before the FROM clause.
Note:
The INTO clause and host variables are only used in Embedded SQL. They are not used in Dynamic SQL. In Dynamic SQL, similar functionality for output variables is provided by %Library.ResultSet.
The following rules apply when using host variables and the INTO clause:
If you specify an INTO clause, SQL does not display selected field values.
For important restrictions on the use of host variable values in the containing program, refer to Host Variable.
Examples
The following example uses embedded SQL within a Caché ObjectScript program. The SQL selects three fields from the first record in the table (embedded SQL always retrieves a single record), and uses INTO to set three corresponding host variables. These variables are then used by the Caché ObjectScript WRITE commands. It is considered good program practice to immediately test the SQLCODE variable upon returning from embedded SQL.
   NEW SQLCODE
   WRITE !,"Going to get the first record"
   &sql(SELECT Home_State, Name, Age 
        INTO :state, :name, :age   
        FROM Sample.Person)
   IF SQLCODE=0 {
     WRITE !,"  Name=",name
     WRITE !,"  Age=",age
     WRITE !,"  Home State=",state }
   ELSE {
     WRITE !,"SQL error ",SQLCODE  }
The following embedded SQL example passes a host variable (today) into the SELECT statement, where a calculation results in the INTO clause variable value (:tomorrow). This host variable is passed out to the containing program.
   SET today=$HOROLOG
   &sql(SELECT :today+1
        INTO :tomorrow   
        FROM Sample.Person)
   IF SQLCODE=0 {
        WRITE !,"Tomorrow is: ",$ZDATE(tomorrow) }
   ELSE {
        WRITE !,"SQL error ",SQLCODE  }
For restrictions on the use of input and output host variable values, see Host Variable.
The following example uses embedded SQL within a Caché ObjectScript program to count the records in two tables. The SQL counts the records using the COUNT aggregate function, then specifies an INTO clause to set a subscripted variable for each count. These variables are then used by the Caché ObjectScript WRITE commands.
   WRITE !,"Counting the records"
   &sql(SELECT COUNT(*)
        INTO :total(1)
        FROM Sample.Person)
   &sql(SELECT COUNT(*)
        INTO :total(2)
        FROM Sample.Employee)
   IF SQLCODE=0 {
        WRITE !,"Total Person records=",total(1)
        WRITE !,"Total Employee records=",total(2) }
   ELSE {
        WRITE !,"SQL error ",SQLCODE  }   
See Also