Issuing Commands and Requests

If you do not issue the SET SQLENGINE command, you must specify the target RDBMS in any SQL command you want to pass directly to the RDBMS.

Including a target RDBMS in your command overrides the SET SQLENGINE command. For example, you can specify your RDBMS and override an existing OFF setting. If you do not specify a target RDBMS (either in your command or with the SET SQLENGINE command), the SQL keyword invokes the Translator.

In this section:

Displaying the Effects of UPDATE and DELETE Commands

Issuing Data Adapter Environmental Commands

Issuing Native SQL Commands (Non-SELECT)

Issuing SQL SELECT Commands

The SQLOUT Master File

Creating a FOCUS View With Direct SQL Passthru

How to:

Issue a Direct SQL Passthru Request


Top of page

Syntax: How to Issue a Direct SQL Passthru Request

The following is a request syntax summary for native SQL commands, including SELECT statements, and for data adapter environmental commands; subsequent sections provide examples

SQL [sqlengine]
command [;]
[TABLE FILE SQLOUT]
[options]
END

where:

sqlengine
Indicates the target RDBMS. Acceptable values are DB2, SQLDS, SQLDBC, SQLIDMS, or SQLORA. Omit if you previously issued the SET SQLENGINE command.

command
Is one SQL command, or one or more data adapter SET commands.

;
For SQL SELECT requests only, the semicolon is required if you specify additional FOCUS report options.

TABLE FILE SQLOUT
For SQL SELECT requests only, allows you to specify additional FOCUS report options or subcommands. To create a Master File you can use throughout the FOCUS session, see Creating a FOCUS View With Direct SQL Passthru.

options
For SQL SELECT requests only, are report formatting options or operations.

END
Terminates the request. Is optional for data adapter SET commands, the SQL commands COMMIT WORK and ROLLBACK WORK, the DB2 CONNECT command, and the data adapter parameterized Passthru commands BEGIN SESSION, END SESSION, and PURGE (see Parameterized SQL Command Summary). Required for all other commands.

Note:


Top of page

Displaying the Effects of UPDATE and DELETE Commands

You can use the SET PASSRECS command to display the number of rows affected by a successfully executed Direct SQL Passthru UPDATE or DELETE command. The syntax is

SQL [sqlengine] SET PASSRECS {OFF|ON}

where:

sqlengine
Indicates the target RDBMS. Acceptable values are DB2, SQLDS, SQLDBC, SQLIDMS, or SQLORA. Omit if you previously issued the SET SQLENGINE command.

OFF
Is the default. As in previous releases, the data adapter provides no information as to the number of records affected by a successfully executed Direct SQL Passthru UPDATE or DELETE command.

ON
Provides the following message after the successful execution of a Direct SQL Passthru UPDATE or DELETE command:

(FOC1364) ROWS AFFECTED BY PASSTHRU COMMAND: #/operation

For example, a DELETE command that executes successfully and affects 20 rows generates the following message:

(FOC1364) ROWS AFFECTED BY PASSTHRU COMMAND: 20/DELETE

In addition to this message, the data adapter updates the &RECORDS system variable with the number of rows affected. You can access this variable using Dialogue Manager, and display it with the ? STAT query.

Note:


Top of page

Issuing Data Adapter Environmental Commands

You can issue one or more data adapter SET commands in a request using Direct SQL Passthru. Data adapter SET commands are not passed to the RDBMS; the data adapter maintains them in memory.


Top of page

Example: Issuing Data Adapter Environmental Commands

The following example specifies the DB2 RDBMS as the target RDBMS, issues four data adapter SET commands, and issues the SQL ? query command to display the updated parameters. Notice that the request does not include the environmental qualifiers TSO, MVS, or CMS.

set sqlengine=db2
> > sql set autoclose on fin
> > sql set ssid dsn
> > sql set plan p7029910
> > sql set dbspace public.space0
> > sql ?
(FOC1440) CURRENT SQL INTERFACE SETTINGS ARE :
(FOC1442) CALL ATTACH FACILITY IS - : ON
(FOC1447) SSID FOR CALL ATTACH IS - : DSN
(FOC1448) ACTIVE PLAN FOR CALL ATTACH IS - :
(FOC1459) USER SET PLAN FOR CALL ATTACH IS - :
(FOC1460) INSTALLATION DEFAULT PLAN IS - : P7029910
(FOC1503) SQL STATIC OPTION IS - : OFF
(FOC1444) AUTOCLOSE OPTION IS - : ON FIN
(FOC1496) AUTODISCONNECT OPTION IS - : ON FIN
(FOC1499) AUTOCOMMIT OPTION IS - : ON COMMAND
(FOC1449) CURRENT SQLID IS - : SYSTEM DEFAULT
(FOC1424) ISOLATION LEVEL FOR DB2 TABLE INTERFACE IS :
(FOC1441) WRITE FUNCTIONALITY IS - : ON
(FOC1445) OPTIMIZATION OPTION IS - : ON
(FOC1420) OPTIMIZATION OF ALL=ON AS LEFT JOIN - : OFF
(FOC1763) IF-THEN-ELSE OPTIMIZATION IS - : OFF
(FOC1484) SQL ERROR MESSAGE TYPE IS - : DBMS
(FOC1497) SQL EXPLAIN OPTION IS               -  : OFF
(FOC1552) INTERFACE DEFAULT DATE TYPE - : NEW
(FOC1446) DEFAULT DBSPACE IS - : PUBLIC.SPACE0
(FOC1453) DEFAULT INDEX SPACE PARAMETERS - : USING STOGROUP PMSSTGRP
> >

See Environmental Commands, for data adapter SET commands.


Top of page

Issuing Native SQL Commands (Non-SELECT)

When the data adapter identifies SQL commands, it passes them to the RDBMS for immediate execution.

With Direct SQL Passthru, one SQL command can span several lines without any prefix or continuation characters. You must complete the command or request with the END keyword.


Top of page

Example: Issuing Native SQL Commands (Non-SELECT)

This example specifies the DB2 RDBMS as the target RDBMS, creates the DPBRANCH table, and inserts rows.

SET SQLENGINE=DB2

SQL CREATE TABLE DPBRANCH
(BRANCH_NUMBER INTEGER NOT NULL,
BRANCH_NAME CHAR(5) NOT NULL,
BRANCH_MANAGER CHAR(5) NOT NULL,
BRANCH_CITY CHAR(5) NOT NULL)
IN PUBLIC.SPACE0 ;
END

SQL INSERT INTO DPBRANCH VALUES (1,'WEST','PIAF','NY') ;
END

SQL INSERT INTO DPBRANCH VALUES (2,'EAST','SMITH','NY') ;
END

Consult File Descriptions and Tables, for sample tables.


Top of page

Issuing SQL SELECT Commands

When you issue an SQL SELECT request using the Direct SQL Passthru facility, the data adapter passes the request directly to the RDBMS. FOCUS does not examine it. The RDBMS evaluates the request and sends storage requirements to FOCUS for future request results (answer sets).

Example:

Issuing SQL SELECT Commands

Based on the storage requirements, FOCUS creates an internal Master File named SQLOUT (discussed in The SQLOUT Master File. The SQLOUT Master File functions as a template for reading and formatting the request results.

The SQLOUT Master File resides in memory; you cannot edit or print it. It exists only for the duration of the request; subsequent requests cannot reference it. To create an internal Master File that exists for the entire FOCUS session, see Creating a FOCUS View With Direct SQL Passthru.

After FOCUS prepares the storage area and the SQLOUT Master File, the RDBMS executes the SQL SELECT request, retrieves the rows, and returns the answer set to FOCUS. The answer set is, in effect, a default report. FOCUS performs minimal additional formatting. When the report is complete, the internal SQLOUT Master File is discarded.

The RDBMS reads data once for SELECT requests using Direct SQL Passthru. FOCUS does not hold or re-read data locally, except for some types of TABLE subcommands (for example, to re-sort or summarize rows). For performance reasons, you should incorporate as many operations as possible (particularly, sorting and aggregation operations) in your SELECT statement and rely on FOCUS for formatting and operations not available through the RDBMS.


Top of page

Example: Issuing SQL SELECT Commands

The following example illustrates a SELECT statement and its default report. The SELECT statement retrieves, from the inventory table, the total number of individual units of each vendor's products for all branches located in New York. The subquery retrieves New York branch numbers. The request does not specify additional report formatting:

SQL DB2
SELECT VENDOR_NUMBER, PRODUCT, SUM(NUMBER_OF_UNITS)
FROM DPINVENT
WHERE BRANCH_NUMBER IN
(SELECT BRANCH_NUMBER
FROM DPBRANCH
WHERE BRANCH_CITY = 'NY')
GROUP BY VENDOR_NUMBER, PRODUCT
ORDER BY VENDOR_NUMBER, PRODUCT;
END

The output is:

PAGE     1

VENDOR_NUMBER PRODUCT __SUM(NUMBER_OF_UNITS)
------------- ------- ----------------------
1 RADIO 15
3 MICRO 9

To produce the preceding default report, the Direct SQL Passthru facility implicitly issues the following TABLE request against the SQLOUT Master File:

TABLE FILE SQLOUT
PRINT *
END

Internally, the process produces an SQLOUT Master File (described in The SQLOUT Master File) that you can use for FOCUS report formatting commands.

For an example of a SELECT request that includes FOCUS report formatting commands, see The SQLOUT Master File.


Top of page

The SQLOUT Master File

To give you access to FOCUS report formatting facilities, the data adapter generates the SQLOUT Master File for each SQL SELECT query. The SQLOUT Master File supports read-only access. The data adapter creates this internal Master File in memory based on information from the RDBMS. You cannot edit or print the SQLOUT Master File; it exists only for the immediate request, so subsequent requests cannot reference it.

How to:

Alter Length and Scale of Numeric Columns Returned

Control the Precision of the Oracle NUMBER Data Type

Example:

Sample SQLOUT Master File

Altering the Length and Scale of Numeric Columns Returned

Customizing Output of a Direct SQL Passthru Report Request

Reference:

SQLOUT Formats for DB2, DB2 for VM, and IDMS/SQL

SQLOUT Formats for Teradata

SQLOUT Formats for Oracle

Note: The data adapter does not generate an associated Access File, since the SQL statement is stored in memory.

The SQLOUT Master File describes the answer set. Each field represents one data element in the outermost SELECT list, reflecting the flat row that the RDBMS returns.


Top of page

Example: Sample SQLOUT Master File

The following is the SQLOUT Master File created for the example in Issuing SQL SELECT Commands:

FILENAME=SQLOUT, SUFFIX=SQLDS, $
SEGNAME=SQLOUT, SEGTYPE=S0, $
FIELD='VENDOR_NUMBER' , E01, USAGE=I11 ,ACTUAL=I4 ,MISSING=OFF, $
FIELD='PRODUCT' , E02, USAGE=A5 ,ACTUAL=A5 ,MISSING=OFF, $
FIELD='__SUM(NUMBER_OF_UNITS)',
E03, USAGE=I11 ,ACTUAL=I4 ,MISSING=OFF, $

The FILENAME and the SEGNAME values are SQLOUT. The SUFFIX is SQLDS, SQLDBC, SQLIDMS, or SQLORA depending on the target engine for the request. The SEGTYPE is S0. These values are constant.

The DB2, DB2 for VM, and IDMS/SQL Data Adapters use the SQL DESCRIBE function to obtain column information:

The data adapter calculates USAGE and ACTUAL formats based on the column data type and length. The following charts outline these calculations.


Top of page

Reference: SQLOUT Formats for DB2, DB2 for VM, and IDMS/SQL

 

 

ACTUAL Formats MISSING=

Data Type:

USAGE Format:

OFF

ON

DATE

YYMD

DATE

same

TIME

HHIS

HHIS

same

TIMESTAMP

HYYMDm

HYYMDm

same

INTEGER

I11

I4

same

SMALLINT

I6

I2

I4

DECIMAL

Pp.s

P((p+1)/2)

P((p+1)/2) if p>15, P8 if p£15

Where p is precision and s is scale. Precision in the USAGE format includes the decimal point and sign; precision in the ACTUAL format excludes them.

FLOAT (4 byte)

F9.2

F4

same

FLOAT (8 byte)

D12.2

D8

same

VARCHAR(n)

An

An

same

Where n £ 254 characters.

VARCHAR(n)

TX50

TX

same

Where 254 < n £ 4094 characters. Note: VARCHAR strings > 4094 characters are not supported.

CHAR(n)

An

An

same

GRAPHIC(n)

A(2n+2)

Kn

same

Where n £ 127 characters.

VARGRAPHIC(n)

A(2n+2)

Kn

same

Where n £ 127 characters Note: VARGRAPHIC(n) where n >127 characters is not supported.

Note: Results of expressions are also one of these data types.


Top of page

Reference: SQLOUT Formats for Teradata

 

 

ACTUAL Formats MISSING=

Data Type:

USAGE Format:

OFF

ON

DATE

YYMD

DATE

same

INTEGER

I11

I4

same

SMALLINT

I6

I2

I4

DECIMAL

Pp.s

P((p+1)/2)

P((p+1)/2) if p>15, P8 if p£15

Where p is precision and s is scale. Precision in the USAGE format includes the decimal point and sign; precision in the ACTUAL format excludes them.

FLOAT (8 byte)

D12.2

D8

same

VARCHAR(n)

An

An

same

CHAR(n)

An

An

same

BYTE

An

An

same

BYTEINT

I6

I2

I4

VARBYTE

An

An

same

GRAPHIC(n)

A(2n+2)

Kn

same

Where n £ 127 characters.

VARGRAPHIC(n)

A(2n+2)

Kn

same

Where n £ 127 characters Note: LONG VARGRAPHIC (VARGRAPHIC(n) where n >127 characters) is not supported.

Note: Results of expressions are also one of these data types.


Top of page

Reference: SQLOUT Formats for Oracle

 

 

ACTUAL Formats MISSING=

Data Type:

USAGE Format:

OFF

ON

DATE

HYYMDS

HYYMDS

same

NUMBER(p,s)

I11
Pp.s
D12.2

I4
P((p+1)/2)
D8

same, if p=38,s=0
P8 if p£15
same, if p>15

Where p is precision and s is scale. Precision in the USAGE format includes the decimal point and sign; precision in the ACTUAL format excludes them.

VARCHAR(n)

An

An

same

Where n £ 254 characters.

LONG(n)

TX50

TX

same

Where 254 < n £ 4094 characters. Note: VARCHAR strings > 4094 characters are not supported.

CHAR(n)

An

An

same

RAW(n)

A2n

A2n

same

Where n £ 128 characters Note: Oracle converts RAW data to and from CHAR data. The data is represented as 1 hexadecimal character, which is equivalent to 2 alphanumeric characters.

Note: Results of expressions are always floating point.


Top of page

Syntax: How to Alter Length and Scale of Numeric Columns Returned

You can use the SET CONVERSION command to alter the length and scale of numeric columns displayed from a SELECT request. That is, you can control the USAGE attribute in the dynamically created Master File.

SQL [sqlengine] SET CONVERSION {RESET|dtype} [RESET|PRECISION {value|MAX}]

where:

sqlengine
Indicates the target RDBMS. Acceptable values are DB2, SQLDS, SQLDBC, SQLIDMS, or SQLORA. Omit if you previously issued the SET SQLENGINE command.

RESET
Returns precision and scale values that you previously altered back to the data adapter defaults. If you specify RESET immediately following the SET CONVERSION command, all data types return to the defaults. If you specify RESET following a particular data type, only columns of that data type are reset.

dtype
Applies the command only to columns of a specific data type. Valid datatypes are:

INTEGER
INTEGER (and, for Oracle, SMALLINT).

DECIMAL
DECIMAL.

REAL
Single precision floating point. Not supported for Oracle or Teradata.

FLOAT
Double precision floating point.

value
Is the precision in the following form:

nn [mm]
where:

nn
Must be greater than 1 and less than the maximum allowable value for the data type. (See description of MAX.)

mm
Is the scale. Valid with DECIMAL, REAL, and FLOAT data types. If you do not specify a value for scale, the current scale setting remains in effect.

MAX
Sets the precision to the maximum allowable value for the indicated data type:

DATA TYPE

MAX Precision

INTEGER

11

REAL

9

FLOAT

20

DECIMAL

33

Note:

You must include space for the decimal point and for a negative sign (if applicable) in your precision setting.


Top of page

Example: Altering the Length and Scale of Numeric Columns Returned

To set the precision for all INTEGER fields to 7:

SQL DB2 SET CONVERSION INTEGER PRECISION 7

To set the precision for all DOUBLE PRECISION fields to 14 and the scale to 3:

SQL DB2 SET CONVERSION FLOAT PRECISION 14 3

To set the precision for all INTEGER fields to the default:

SQL DB2 SET CONVERSION INTEGER RESET

To set the precision and scale for all fields to the defaults:

SQL DB2 SET CONVERSION RESET

Top of page

Syntax: How to Control the Precision of the Oracle NUMBER Data Type

You can use the ORANUMBER setting to override the default mapping of a NUMBER data type with precision between 32 and 38.

SQL [SQLORA] SET ORANUMBER {COMPAT|DECIMAL}

where:

SQLORA
Indicates the Oracle Data Adapter. You can omit this value if you previously issued the SET SQLENGINE command.

COMPAT
Indicates that the NUMBER data type with precision between 32 and 37 will be mapped to format D20.2.This is the default.

DECIMAL
Indicates that the NUMBER data type with precision between 32 and 37 will be mapped to format P33.2.


Top of page

Example: Customizing Output of a Direct SQL Passthru Report Request

The following example illustrates how to customize the default report output from the example in Issuing SQL SELECT Commands. The example adds the TABLE FILE SQLOUT command to the SELECT statement, and follows it by a report heading and AS phrases that rename column headings. The primary purpose of the TABLE FILE extension is for report formatting:

SQL DB2
.
.
.
ORDER BY VENDOR_NUMBER, PRODUCT;
TABLE FILE SQLOUT
"Number of Units of Each Vendor's Products"
" in New York Branches "
" "
PRINT E01 AS 'Vendor,Number'
E02 AS 'Product,Name'
E03 AS 'Total,Units'
END
>
>
NUMBER OF RECORDS IN TABLE= 2 LINES= 2

PAGE 1

Number of Units of Each Vendor's Products
in New York Branches

Vendor Product Total
Number Name Units
------ ------- -----
1 RADIO 15
3 MICRO 9

Top of page

Reference: Usage Notes for Customizing a Direct SQL Passthru Report

When customizing a report, standard FOCUS report request syntax applies, subject to the following rules:


Top of page

Creating a FOCUS View With Direct SQL Passthru

You can create a named, internal Master File (FOCUS view) for a particular SELECT statement with the data adapter SQL PREPARE command. Unlike the SQLOUT Master File, you can generate reports with this FOCUS view for the entire FOCUS session.

In any FOCUS session, you can describe an unlimited number of FOCUS views.

How to:

Create a FOCUS View With Direct SQL Passthru

Example:

Creating a FOCUS View

Reference:

Usage Notes for Creating a FOCUS View


Top of page

Syntax: How to Create a FOCUS View With Direct SQL Passthru

SQL [sqlengine] PREPARE view_name FOR
SELECT....[;]
END

where:

sqlengine
Indicates the target RDBMS. Acceptable values are DB2, SQLDS, SQLDBC, SQLIDMS, or SQLORA. Omit if you previously issued the SET SQLENGINE command.

view_name
Names the Master File (FOCUS view). The name can be eight characters long and must conform to FOCUS naming conventions for Master Files (see Describing Tables to FOCUS).

SELECT...
Is any SELECT statement.


Top of page

Reference: Usage Notes for Creating a FOCUS View

The keywords and default aliases in the generated Master File are the same as those for the SQLOUT Master File (see The SQLOUT Master File). Only its file or member name and the FILENAME value reflect the view name you specify in the PREPARE command.

With Direct SQL Passthru, the PREPARE command only creates an internal Master File. The RDBMS does not return data until you execute a TABLE request referencing the FOCUS view. PREPARE does not generate an Access File; you supply the table names in SELECT statement FROM clauses.

Master Files created with PREPARE reside in memory, so you cannot edit or print them. They function like any other FOCUS Master File; for example, you can specify them with TableTalk. You can assign DEFINE fields to them or use them in MATCH FILE commands. You can also issue the ?F query in TABLE requests to list the field names of the FOCUS view.


Top of page

Example: Creating a FOCUS View

In this example, the DB2 RDBMS is the target RDBMS. The SQL PREPARE command creates a view named TOTPROD:

SET SQLENGINE=DB2
>
SQL PREPARE TOTPROD FOR
SELECT VENDOR_NUMBER, PRODUCT, SUM(NUMBER_OF_UNITS)
FROM DPINVENT
WHERE BRANCH_NUMBER IN
(SELECT BRANCH_NUMBER
FROM DPBRANCH
WHERE BRANCH_CITY = 'NY')
GROUP BY VENDOR_NUMBER, PRODUCT
ORDER BY VENDOR_NUMBER, PRODUCT;
END

After the TOTPROD view is created, you can assign a virtual HI_STOCK field to the TOTPROD Master File and specify the virtual field in a report request:

>
DEFINE FILE TOTPROD
HI_STOCK/A2 = IF (E03 GT 10) THEN '**' ELSE ' ' ;
END
>
TABLE FILE TOTPROD
"Number of Units of Each Vendor's Products"
" in New York Branches "
" "
PRINT E01 AS 'Vendor,Number'
E02 AS 'Product,Name'
E03 AS 'Total,Units'
HI_STOCK AS ''
FOOTING
"** = Too many units in stock,"
" reevaluate purchasing. "
END
>

NUMBER OF RECORDS IN TABLE= 2 LINES= 2

PAGE 1

Number of Units of Each Vendor's Products
in New York Branches

Vendor Product Total
Number Name Units
------ ------- -----
1 RADIO 15 **
3 MICRO 9

** = Too many units in stock,
reevaluate purchasing.

The one restriction on FOCUS views created with Direct SQL Passthru involves using them with the FOCUS JOIN command. You cannot join two FOCUS views or a view with another FOCUS-readable source. You can, however, create a HOLD file of data extracted from the FOCUS view and use the HOLD file in the join.


Information Builders