Leverage SQL to Create IBM i Web Apps – FAST!

Whether you’re a seasoned SQL pro or just learning the ropes of SQL syntax on IBM i, the SQL parser built into Nitro Query App Builder is an invaluable tool you can leverage to quickly generate a web-based report for your users, or even a file maintenance application containing multiple joined files. SQL-based data sources were introduced in Valence 5.1 back in May 2017, and the feature was formally removed from Beta status this month.

Assuming you have a basic familiarity with your IBM i database, new functionality introduced in recent builds make it possible for you to prompt your way through file and field selection as you create an SQL-based data source. The SQL parser will also automatically “beautify” your SQL statements along the way, making it easy to cut-and-paste the most complicated SQL into Valence and have it magically become an easily edited piece of formatted code.  

This tip shows you how to put the Nitro Query SQL parser to work. To demonstrate the parser functionality in action, let’s walk throgh the process of assembling an SQL-based datasource that joins the Valence sample customer master file DEMOCMAST with the sample customer order file DEMOORDER. The first step is to launch the Nitro Query App Builder app and click the pink Plus icon in the lower right to create a new data source. To switch to SQL mode, click the “Enter a free form SQL Statement” link on the lower left, as depicted below in figure 1.

Figure 1 – Jump into SQL mode

Now we can enter the initial part of our SQL select statement.  For starters, we’ll just enter a basic “select all” statement for the customer master file and let the parser format it.  Do this by keying in “select * from democmast” and then hit the “FORMAT” button in the lower left, as demonstrated in figures 2 and 3…

Figure 2 – Enter a simple “select all” SQL statement
Figure 3 – Letting the SQL parser auto-format the initial statement

Notice that the SQL parser has detected the file name and is showing it as one of the “From Clause Files” in the right panel. Click on the tab to see the available fields in the file, all of which are selected at this point since we specified a generic “select *” in our statement.

Figure 4 – Viewing the available and selected fields in the file(s) specified in the SQL “From” clause

At this point we can clean up our data source by de-selecting the fields or columns we don’t need for the app we’re creating.  As soon as you begin unchecking any of the columns, you’ll notice the SQL select statement is automatically adjusted accordingly, as shown in figure 5:

Figure 5 – the SQL statement automatically adjusts to match the fields selected or de-selected

Now that we’ve got all the columns we need from the customer master, let’s move on to the customer order file.  You could of course start adding to the SQL statement manually at this point, but for sake of example we’ll use the lookup function to automatically do the initial legwork.  Click on the “Lookup File” tab on the right and enter the file name as shown in figure 6:

Figure 6 – using the Lookup File option to pick an additional file and fields for the SQL statement

With the file’s fields listed, we can now select the columns we’d like added to our statement and have them added in automatically, along with the initial part of the join syntax…

Figure 7 – Selecting columns from the second file and adding it to the SQL select statement

Now we’re at a point where we need to make some tweaks to the SQL statement directly. First off, the SQL parser is letting us know that there’s an ambiguity in the select statement that needs to be qualified. Specifically, the CUSNO field exists in both files, therefore we must clarify which file it should be pulled from. We also need to specify which fields we’re using on the join to the orders file. This is depicted below in figures 8 and 9.

Figure 8 – the SQL parser is highlighting a field ambiguity we need to address
Figure 9 – Correcting the select field ambiguity and adjusting the join syntax

Finally, we’ll use another feature of the SQL parser to help us find the proper columns for the Order By clause. Do this by adding “Order By ” to the end of the statement, then hit the F4 key. This will present you a list of all the fields available from the files specified in the From clause, then you can simply click on the desired field and it will be added to the statement.

Figure 10 – Using the F4 key to see available fields to add to the SQL statement
Figure 11 – The final SQL statement after completing the Order By clause

Finally, we hit the Next or Preview button to verify our SQL statement works, then save it.

Figure 12 – The end result… Our data source is now ready to feed a grid app!

And that’s all there is to it! We find most people can build out data sources much faster using the SQL parser, though of course you can still use the “Classic” approach to create the data source using the 7 Step breadcrumb process.

Note: If you’re new to Nitro Query, the next step to creating a grid app your users can execute is to (1) marry your data source to a grid widget, then (2) pop the widget into a Query app. Once your grid widget is created, you would then click the Queries tab, click the Plus icon to create a new Query App, add your newly created Grid Widget to the Query canvas, then click Save. A short video demonstrating that simple process is in production and will be posted soon.