|
|
|
|
- Overview
- SQL query with external join to a data file (query editor)
- SQL query with external join through programming
How to create a query with external join?
In SQL language, an external join is used to exclude or include file records with or without correspondences in the linked files. By default, when creating a query between 2 linked data files, only the records with a correspondence between the 2 files are processed. To process the records without correspondence, an external join must be added. Let's see how to perform an external join by using: SQL query with external join to a data file (query editor) This query is used to select the records found in a data file (main file) with a correspondence (join) to another data file (fsecondary file or linked file), knowing that all the records found in the main file do not necessarily have a correspondence in the secondary file. In this example, we are going to select the records found in CUSTOMER file (main file) with the sum of orders in ORDER file (secondary file) for each customer. Some customers may have no order. Therefore, an external join must be added into the query to process the customer without orders. The different steps for creating this SQL query with external join are as follows: - Click
in the quick access buttons. - In the window that is displayed, click "Queries".
- The query creation wizard starts.
- Specify that you want to create a select query ("Select" option).
Go to the next step of the wizard. - The query description window appears.
- Give a name and a caption to the query:
- In the left section of the description window, choose the file items that will be used (in our example, the CUSTOMER file and the CustomerName, Company and City items then the ORDER file and the TOTALBT item).
- Double-click the names of the items to add them to the list of query items:
- To perform the sum of orders, click the "Sum" button in the right section of the editor. The following window is displayed in the editor:
- Validate. The query description window is updated:
- Validate the query description window. The following query is displayed in the editor:
- To add the external join, double-click the "Join" area.
- The description window of the join is displayed.
- To select the direction for the external join, check the "include..." box corresponding to the requested direction. In our example, the records found in CUSTOMER file without orders are included.
- Validate. The query description window is updated.
- Save the query (Ctrl + S).
- Press F2 to see the SQL code:
- Run the query test (GO in the quick access buttons).
- The query can be run in the program by HExecuteQuery.
SQL query with external join through programming The SQL queries can be directly written through programming in the WLanguage code. To do so, you must: - Create a variable of type Data source to represent the query at runtime.
- Create a Character String variable to contain the SQL code of the query and write the SQL code in this variable.
- Run the SQL query with HExecuteSQLQuery.
- Browse the result with the HReadXXX functions.
Code example
Src1 is Data Source sSQLCode is string // Select the products whose price is greater than ... sSQLCode = [ SELECT CUSTOMER.CustomerName AS CustomerName, CUSTOMER.Company AS Company, CUSTOMER.City AS City, SUM(ORDERS.TotalBT) AS the_sum_TotalBT FROM CUSTOMER LEFT OUTER JOIN ORDERS ON CUSTOMER.CustNum = ORDERS.CustNum GROUP BY CUSTOMER.CustomerName, CUSTOMER.Company, CUSTOMER.City ] HExecuteSQLQuery(Src1, hQueryDefault, sSQLCode) FOR EACH Src1 Trace(Src1.CustomerName, Src1.Company, Src1.the_sum_TotalBT) END
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|