Chapter 10: OpenESQL Assistant

This chapter describes the configuration and use of the OpenESQL Assistant.

Overview

The OpenESQL Assistant is an interactive tool that makes it easy for you to:

Setting OpenESQL Assistant Options

Before creating SQL queries using OpenESQL Assistant, you can set options that determine the behavior of the OpenESQL Assistant and specify how it generates SQL. Do this from the Options dialog box by expanding Micro Focus COBOL Tools > OpenESQL Assistant. On this dialog box, you can set the following options:

See your Visual Studio documentation for information on setting properties.

Using OpenESQL Assistant

Use the OpenESQL Assistant to easily design and build SQL queries and embed those queries into your COBOL code. As you make specifications for a query, OpenESQL Assistant generates the necessary code for you. You can also test the queries you create to ensure they are producing the proper results before you embed them into your programs. And, you can use OpenESQL Assistant to generate and embed the auxiliary code necessary to support your queries in COBOL.

Starting the OpenESQL Assistant

You can configure the OpenESQL Assistant's dockable window to appear when you start Visual Studio or you can simply invoke it when you need it.

Depending on how OpenESQL is configured, you might be prompted to supply a user name and password.

How to...

Connecting to a Data Source

Before you can use OpenESQL Assistant to generate SQL, you must first connect to the data source that contains the data you want to access. All data sources cataloged on your workstation are listed in the tree view of the OpenESQL Assistant. You can connect to one data source at a time. After you have connected, all of the table names in the data source show on the tree view.

If you are running in ODBC mode and have checked the Automatically catalog data source on CONNECT option for OpenESQL Assistant, all ODBC data sources on your workstation are automatically cataloged as an ODBC ADO.NET data provider. See your Microsoft documentation for information on setting up ODBC data sources.

Notes:

How to...

Building Queries

The process of building a query using OpenESQL includes at least some and potentially all of the following steps:

The following sections provide information about this process.

Selecting a Table and a Query Type

You can select one table at a time or you can select all of the tables in a data source. OpenESQL Assistant prompts you to select the type of query you would like to perform on the table or tables by presenting the Select Type of Query to Create dialog box. This dialog box contains a list of query types you can perform on your selection. Depending on the type of query you select, you can also change the default cursor name, specify that your query be generated as a Stored Procedure, and specify the stored procedure (SP) name.

Selected tables appear in the tree view with a check mark next to the table icon, .

Once you have selected a query, the COBOL code for the query you have selected is automatically generated and displayed underneath the Query tab. At the same time, a list of all the columns in the table is displayed underneath the table name.

For SELECT queries, a table alias is generated automatically by the OpenESQL Assistant. For example:

SELECT FROM Customer A

The first table selected generates an alias of the letter A. If you select a second table, the OpenESQL Assistant generates an alias of B and so on.

Each column name is prefixed by its alias (for example, A.CustID, A.Company). This enables you to distinguish the columns in one table from those in another.

Some databases use special characters in the names of system tables. For example, Oracle can use system tables whose names contain a dollar sign ($). When the OpenESQL Assistant encounters a special character in the name of system table that would render the generated table name illegal, it automatically encloses the column and table names in quotes.

If the name generated from the column name (including any prefixes and suffixes) is longer than 31 characters or contains illegal characters would be illegal in COBOL, OpenESQL generates a host variable using the column number (for example, COL005) rather than the column name.

How to...

Selecting Columns

Once you have your table and query type selected, you can select one or more columns to process in your query.

Selected columns appear in the tree view with a check mark next to the column icon, .

How to...

Specifying Aggregate Functions for Columns

You can specify an aggregate function for any column in a SELECT (cursor) or SELECT DISTINCT (cursor) query. An aggregate function retrieves certain information about the data returned from a column.

OpenESQL returns the following information for each group of returned data:

Aggregate Function Value for each group of returned data
Sum Sum of all values returned (numerical columns only)
Avg Average of all values returned (numerical columns only)
Max Maximum value returned
Min Minimum value returned
Count Number of records returned

How to...

Specifying Search Criteria

You can limit which rows are returned by a query by specifying search criteria on the OpenESQL Assistant Search Criteria tab. OpenESQL Assistant generates a WHERE clause based on the criteria you specify.

How to...

Specifying the Order in which Data is Retrieved

You can order the rows that are returned by a query by specifying which columns to sort using the OpenESQL Assistant Sort tab. OpenESQL Assistant generates an ORDER BY clause based on the order you specify.

How to...

Creating a Table Join

Where tables have at least one column in common, you can create a table join enabling you to retrieve information from more than one table at the same time. The OpenESQL Assistant creates a table join using the first matching column it finds. If OpenESQL does not find any matching columns, it cannot generate the join automatically. In this case, you can specify the join using the Search Criteria feature.

This feature is only available for SELECT queries.

How to...

Working with Queries

This section presents some features of OpenESQL Assistant and recommends some procedures that can enhance your ability to create queries

Display Column Details

The Details tab in the OpenESQL Assistant shows detailed information on the columns in a table. The columns on the Details tab describe the columns in the selected table as follows:

How to...

Disconnect from a Data Source

When you disconnect from a data source, OpenESQL discards the current query. You must disconnect from one data source before you can connect to another.

How to...

Close a Query

When you close a query, OpenESQL discards the generated code, enabling you to start a new query.

How to...

Remove a Table from a SELECT Query

Most queries you create using OpenESQL Assistant access one table only. If you remove that table from the query, OpenESQL closes the query and you lose the code you've generated for it. However, if you have created a SELECT query that contains one or more table joins, you can safely remove a table without closing the query. When you initiate the removal of a table from a SELECT query, you're prompted to confirm your decision. Upon confirmation, OpenESQL removes the generated code that pertains to the table from the query. It also removes any code pertaining to columns that belong to that table.

How to...

Changing a SELECT (cursor) query to do an Array FETCH

OpenESQL Assistant doesn't directly generate code for array fetches. However, you can modify the SELECT (cursor) code generated by OpenESQL Assistant to handle array fetches.

How to...

Testing Your SQL Queries

You can use OpenESQL Assistant to test the queries you create before committing them to program code. To do this, you simply run a query from the OpenESQL Assistant. The results for all SELECT statements appear automatically on the Results tab. Other query types return the following information in a message box:

Occasionally, the query you create will require additional specifications for it to execute correctly. If OpenESQL Assistant cannot execute the code, you are prompted to correct it.

Note: When you run a query in OpenESQL Assistant, any UPDATE, INSERT, and DELETE statements do execute against the database, potentially altering its contents.

How to...

Embedding SQL into a Program

Once you are happy with the SQL query that you have created, you can insert it into your COBOL program.

How to...

Auxiliary Code

The SQL queries you generate with OpenESQL often require additional code outside of the query itself in order to execute properly from your COBOL programs. For example, under DB2 UDB you cannot successfully execute an SQL query that accesses a database until you have inserted a CONNECT statement to connect to the data source. Once you have embedded your query, you can use OpenESQL Assistant to generate and embed this auxiliary code.

Generating EXEC SQL Auxiliary Code

OpenESQL Assistant can generate the following types of EXEC SQL auxiliary code:

How to...

Generating EXEC ADO Auxiliary Code

If you have enabled OpenESQL Assistant to generate EXEC ADO code by checking the Generate EXEC ADO option when you set options for OpenESQL, you can generate auxiliary code for both the EXEC ADO disconnected and connected models.

OpenESQL Assistant embeds the dataset name you specify in the Dataset name field on the Auxiliary Code tab into all generated statements that cite a dataset.

Note: You can mix EXEC ADO and EXEC SQL statements in the same program providing that you compile your COBOL program with the directive SQL(DBMAN=ADO).

Generated EXEC ADO Statements

OpenESQL Assistant can generate the following types of EXEC ADO auxiliary code. For a description of each, see the section Using Embedded EXEC ADO Statements in the chapter Using OpenESQL with ADO.NET.

Connected or Disconnected Models
Disconnected Models
Connected Models
EXEC ADO Wizards

Certain EXEC ADO statements require a significant amount of user input to build them correctly. When you use the OpenESQL Assistant Auxiliary Code tab to generate one of these statements, you invoke a wizard that walks you through the various options for that statement.

The statements generated by these wizards are very similar to the EXEC ADO DELETE, INSERT, SELECT, and UPDATE statements except that you cannot execute them from the OpenESQL Assistant. Because the Wizards need table and column names to work correctly, you must create a query first.

OpenESQL Assistant provides the following wizards for use in generating auxiliary code:

How to...

Embedding Auxiliary Code

You embed auxiliary code in the same manner as you embed your SQL queries.

How to...

Stored Procedures

This section provides information on generating stored procedures and testing them using OpenESQL Assistant.

Note: Multiple types of SQL statements can exist within a stored procedure. OpenESQL Assistant is not designed to generate stored procedures with multiple types of SQL statements. You need tools, such as the ones provided by Microsoft, to do this.

The intention of OpenESQL Assistant is to allow you to quickly learn how to generate simple stored procedures and the COBOL code required to invoke those stored procedures.

Generating a Stored Procedure Query

Microsoft provides tools to create, edit, and test stored procedures. It is then the task of the programmer to generate the code to invoke these stored procedures from a COBOL client program. The OpenESQL Assistant makes this process easier.

When using Microsoft SQL Server data sources, you have the option to use OpenESQL Assistant to generate the query as a stored procedure. OpenESQL Assistant generates both the SQL statements to create the stored procedure and the client code to invoke it.

OpenESQL Assistant generates two SQL statements that drop the stored procedure and then create it. You typically place these SQL statements in a server program because they only need to be executed once.

OpenESQL Assistant also generates the code that goes into the client program and invokes the stored procedure. Depending on the type of query you select, OpenESQL Assistant might generate a DECLARE CURSOR statement that invokes the stored procedure or it might generate SQL CALL statements only.

How to...

Testing a Stored Procedure Query

Once you have built the stored procedure query, you can test it in much the same manner as you test other queries. However, because multiple SQL statements are created by OpenESQL Assistant for a stored procedure, you need to run the query multiple times. Each time you run the query, you execute the next SQL statement.

The first time you run the query, the DROP PROCEDURE statement is executed, and the following message appears:

Cannot drop the procedure 'spname,' because it does not exist...

This message is expected and will change if you execute the DROP PROCEDURE multiple times.

The second time you run the query, the CREATE PROCEDURE statement is executed. A message appears indicating that the statement executed successfully.

To execute a stored procedure CALL statement with search criteria that requires you to provide a literal value, you are prompted to provide that literal value.

When all SQL statements have been executed, OpenESQL shows the results on the Results tab.

You can repeat this process until you are satisfied with the results.

How to...

Embedding a Stored Procedure into a Program

This procedure is also similar to that of embedding a standard SQL query into a program.

OpenESQL Assistant generates statements for both client and server programs. You can delete the statements which are not needed. If your program is the client program, the DROP and CREATE PROCEDURE statements are not necessary.

Note: If you test the query as outlined above, you do not need to run either the DROP or CREATE PROCEDURE statements in a program as the stored procedure has already been created. If the stored procedure has not already been created, then you need to either add a SQL COMMIT statement or add directive SQL(AUTOCOMMIT) to the program that issues the CREATE PROCEDURE. Otherwise the stored procedure will not be saved to the data source.

How to...


Copyright © 2009 Micro Focus (IP) Ltd. All rights reserved.