Using SQL Translator Commands

In this section:

How to:

Reference:

The SQL command may be used to report from any supported data source or set of data sources. Standard TABLE phrases for formatting reports can be appended to the SQL statements to take advantage of a wide range of report preparation options.

Note: If you need to join data sources for your request, you have two options: use the JOIN command before you issue any SQL statements, or use the WHERE predicate in the SQL SELECT statement to join the required files dynamically. See SQL Joins.


Top of page

x
Syntax: How to Use SQL Translator Commands
SQL 
sql statement;
[ECHO|FILE]
[TABLE phrases]
END

where:

SQL

Is the SQL command identifier, which invokes the SQL Translator.

Note: The SQL command components must appear in the order represented above.

sql statement

Is a supported SQL statement. The statement must be terminated by a semicolon (;). It can continue for more than one line. See Supported SQL Statements.

Within the SQL statement, field names are limited to 48 characters (an ANSI standard Level 2 limitation). View names generated through the SQL CREATE VIEW statement are limited to 18 characters and subqueries can be nested up to 15 levels deep. Correlated subqueries are not supported by FOCUS and other non-relational data sources.

ECHO

Are optional debugging phrases that capture the generated TABLE request. These options are placed after the SQL statement.

FILE [name]

Writes the translated TABLE phrases to the named procedure. If you do not supply a file name, a default name is assigned when the request runs. The file is then deleted.

TABLE phrases

Are optional TABLE formatting phrases. See TABLE Formatting Phrases in SQL Requests.

END or QUIT

Is required to terminate the procedure.



Example: Using SQL Translator Commands

The following request contains an SQL statement and TABLE formatting commands:

SQL
SELECT BODYTYPE, AVG(MPG), SUM(SALES)
FROM CAR
WHERE RETAIL_COST > 5000
GROUP BY BODYTYPE;
TABLE HEADING CENTER
"AVERAGE MPG AND TOTAL SALES PER BODYTYPE"
END

Top of page

x
Reference: TABLE Formatting Phrases in SQL Requests

You can include TABLE formatting phrases in an SQL request, subject to the following rules:


Top of page

x
The SQL SELECT Statement

The SQL SELECT statement translates into one or more TABLE PRINT or TABLE SUM commands, depending on whether individual field display or aggregation is applied in the request. See Displaying Report Data.

The SQL statement SELECT * translates to a PRINT of every field in the Master File, and uses all of the fields of the Cartesian product. This is a quick way to display a file, provided it fits in a reasonable number of screens for display, or provided you use ON TABLE HOLD or ON TABLE PCHOLD to retain retrieved data in a file for reuse. See Saving and Reusing Your Report Output.

SQL functions (such as COUNT, SUM, MAX, MIN, AVG) are supported in SELECT lists and HAVING conditions. Expressions may be used as function arguments.

The function COUNT (*) translates to a count of the number of records produced by printing all fields in the Master File. This is the same as counting all rows in the Cartesian product that results from a SELECT on all fields.

Whenever possible, expressions in the SQL WHERE predicate are translated into corresponding WHERE criteria in the TABLE request. Expressions in SELECT lists generate virtual fields. The SQL HAVING clauses also translate into corresponding WHERE TOTAL criteria in the TABLE request. The SQL LIKE operator is translated directly into the corresponding LIKE operator in the WHERE criteria of the TABLE request. For details on record selection in TABLE requests, see Selecting Records for Your Report.

Only subqueries based on equality, when the WHERE expression is compared to a subquery by using an equal (=) sign, are supported. For example: WHERE field = (SELECT ...).

The SQL UNION operator translates to a TABLE request that creates a HOLD file for each data source specified, followed by a MATCH command with option HOLD OLD-OR-NEW, which combines records from both the first (old) data source and the second (new) data source. See Merging Data Sources.

For related information, see Supported SQL Statements and How to Use SQL Translator Commands.


Top of page

x
SQL Joins

How to:

Reference:

When performing SQL joins, the formats of the joined fields must be the same. Join fields need not be indexed, and non-equijoins are supported.

Recursive, outer, and inner joins are supported. Inner join is the default.



x
Syntax: How to Create an Inner Join

Two syntax variations are supported for inner joins.

Variation 1

SQL
SELECT fieldlist FROM file1 [alias1], file2 [alias2]
[WHERE where_condition];
END

Variation 2

SQL
SELECT fieldlist FROM file1 [alias1] INNER JOIN file2 [alias2]
ON join_condition [INNER JOIN ...]
[WHERE where_condition];
END

where:

fieldlist

Identifies which fields are retrieved from which data sources.

Joined fields in the SQL WHERE predicate must be qualified if the names are not unique. Specify them with their corresponding file names or file aliases. For example:

{file1|alias1}.field1, {file2|alias2}.field2
FROM

Introduces the data sources to be joined.

file1, file2

Are the data sources to be joined.

alias1, alias2

Are optional alternate names for the data sources to be joined.

where_condition

Is an optional selection condition for the joined answer set. Joined rows that do not satisfy this condition are eliminated from the returned answer set. If omitted in Variation 1, the answer set is the Cartesian product of the two data sources.

join_condition

Is the join condition.



x
Syntax: How to Create an Outer Join
SQL
SELECT fieldlist FROM file1 {LEFT|RIGHT} JOIN file2 
ON join_condition [{LEFT|RIGHT} JOIN ...]
WHERE where_condition 
END

where:

fieldlist

Identifies which fields are to be retrieved from which data sources.

Joined fields in the SQL WHERE predicate must be qualified if the names are not unique. Specify them with their corresponding file names or file aliases. For example:

{file1|alias1}.field1, {file2|alias2}.field2
FROM

Introduces the data sources to be joined.

file1, file2

Are the data sources to be joined.

alias1, alias2

Are optional alternate names for the data sources to be joined.

join_condition

Is the join condition. The condition must specify equality. For example, T1.A=T2.B.

where_condition

Is an optional selection condition for the joined answer set. Joined rows that do not satisfy this condition are eliminated from the returned answer set.



x
Reference: Join Name Assignments From the SQL Translator

Joins issued by the SQL Translator are assigned names in the format:

SQLJNMnn

where:

SQLJNM

Is the SQL Translator join prefix.

nn

Is a number between 01 and 16 assigned in the order in which the joins are created (FOCUS supports a maximum of 16 joins). The first join has the AS name SQLJNM01, the second join is named SQLJNM02, and so on, up to SQLJNM16.

All joins are automatically created and cleared by the SQL Translator. No user-specified joins are affected.



Example: Using Qualified Field Names in SQL Joins

In the following statement, T.A and U.B are qualified field names:

SQL
 SELECT T.A, T.B
 FROM T, U
 WHERE T.A = U.B;
END


Example: Using Recursive SQL Joins

In the following statement, A and B are aliases for the same data source, CAR. The output from CAR is pairs of B values that have the same A values:

SQL
   SELECT A.SEATS, B.SEATS
   FROM CAR A, CAR B
   WHERE A.MODEL = B.MODEL;
END

Note that all field names in the SELECT clause must be unique or qualified.



x
Reference: SQL Join Considerations

For related information about index optimization and optimized join statements, see your Server documentation.


Top of page

x
SQL CREATE TABLE and INSERT INTO Commands

Reference:

SQL Translator supports the commands CREATE TABLE and INSERT INTO table:

These commands enable you to create tables to enhance reporting efficiency.

Note: When applications are enabled, the Master File and data source are written to the APPHOLD directory. When applications are disabled, the Master File and data source are written to the TEMP directory.



x
Reference: Usage Notes for CREATE TABLE and INSERT INTO Commands


Example: Creating a Table With Single-Record Insert

The following shows a single-record insert, creating the table U with one record:

-* Single-record insert example.
-*
SQL
CREATE TABLE U (A INT, B CHAR(6), C CHAR(6), X INT, Y INT);
END
SQL
INSERT INTO U (A,B,C,X,Y) VALUES (10, '123456','654321', 10, 15);
END

Top of page

x
SQL CREATE VIEW and DROP VIEW Commands

How to:

A view is a transient object that inherits most of the characteristics of a table. Like a table, it is composed of rows and columns:

Tip: To use a view, issue a SELECT from it. You cannot issue a TABLE request against the view because the view is not extracted as a physical FOCUS data source. To create a HOLD file for extracted data, specify ON TABLE HOLD after the SQL statements. For details on creating HOLD files, see Saving and Reusing Your Report Output.



x
Syntax: How to Create a View

The SQL Translator supports the following SQL statement:

CREATE VIEW viewname AS subquery ;

where:

viewname

Is the name of the view.

subquery

Is a SELECT statement that nests inside:

  • A WHERE, HAVING, or SELECT clause of another SELECT.
  • An UPDATE, DELETE, or INSERT statement.
  • Another subquery.


Example: Creating and Reporting From an SQL View

The following example creates a view named XYZ:

SQL
CREATE VIEW XYZ
 AS SELECT CAR, MODEL
 FROM CAR;
END

To report from the view, issue:

SQL
 SELECT CAR, MODEL
 FROM XYZ;
END

According to normal SQL data definition syntax, each CREATE VIEW statement must terminate with a semicolon.



Example: Dropping an SQL View

The following request removes the XYZ view:

SQL
 DROP VIEW XYZ;
END

Top of page

x
Cartesian Product Style Answer Sets

The SQL Translator automatically generates Cartesian product style answer sets unless you explicitly turn this feature off. However, it is advisable to leave the CARTESIAN setting on, since turning it off does not comply with ANSI standards. For details on the SET CARTESIAN command, see Merging Data Sources.


Top of page

x
Continental Decimal Notation (CDN)

Continental Decimal Notation displays numbers using a comma to mark the decimal position and periods for separating significant digits into groups of three. This notation is available for SQL Translator requests.



Example: Using CDN to Separate Digits

The following example creates a column defined as 1.2 + SEATS:

SET CDN=ON
SQL
   SELECT SEATS + 1,2
   FROM CAR;
END

Top of page

x
Specifying Field Names in SQL Requests

Specify fields in an SQL request using:



Example: Specifying a Field Name With a Delimited Identifier

The following field identifier can be included in a request:

"COUNTRY.NAME"


Example: Qualifying a Delimited Field Name

To qualify the delimited field name COUNTRY.NAME with its file name, use:

CAR."COUNTRY.NAME"

Top of page

x
SQL UNION, INTERSECT, and EXCEPT Operators

The SQL UNION, INTERSECT, and EXCEPT operators generate MATCH logic. The number of files that can participate is determined by the MATCH limit. UNION with parentheses is supported.

Match logic merges the contents of your data sources. See Merging Data Sources.


Top of page

x
Numeric Constants, Literals, Expressions, and Functions

The SQL SELECT list, WHERE predicate, and HAVING clause can include numeric constants, literals enclosed in single quotation marks, expressions, and any scalar functions. Internally, a virtual field is created for each of these in the SELECT list. The value of the virtual field is provided in the answer set.


WebFOCUS