IBM Books

Net.Data Programming Guide


Appendix A. Dynamic Query Sample

This sample application shows a list of employees. The application user can select an employee's name to view details. This application uses the SQL language environment and uses the Net.Data variables LOGIN and PASSWORD to authenticate the user to the database.



%{************************ Dynamic Query Sample *****************************
*   FileName = sqlsamp1.mac                                                 * 
*   Description:                                                            *
*     This Net.Data macro file ...                                          *
*        - creates a dynamic selection list from the database               *
*        - uses a default table to show the SQL result                      *
*                                                                           *
****************************************************************************%}
%{***************************************************************************
*   Includes -                                                              *
****************************************************************************%}
%INCLUDE "sqlsamp1.hti"
%{****************************************************************************
*  Function:  queryDB              Language Environment: SQL                *
*  Description: Queries the database and creates a selection list           *
*               from the result.  The database name and table name are      *
*               variables defined in the above include file.                *
****************************************************************************%}
%FUNCTION(DTW_SQL) queryDB() {
 SELECT * FROM $(myTable)
%MESSAGE {
    -204:  {<p><b>ERROR -204: Table $(myTable) not found. </b>
   <p>Be sure the correct include file is being used.</b>
    %} : exit
    +default: "WARNING $(RETURN_CODE)" : continue
    -default: "Unexpected ERROR $(RETURN_CODE)" : exit
%}
 
%REPORT {
<select name=emp_name>
%ROW{
<option>$(V2)
%}
</select>
%}
%}
 
%{****************************************************************************
*  Function block                                                            *
*  Description: Sends SQL query the database for the selected first name and *
*               generates a default table                                    *
******************************************************************************%}
%FUNCTION(DTW_SQL) fname(){
SELECT FIRSTNME, MIDINIT, LASTNAME, PHONENO, JOB FROM $(myTable) WHERE FIRSTNME='$(emp_name)'
%MESSAGE {
  -204: "Error -204: Table not found "
  -104: "Error -104: Syntax error"
   100: "Warning 100: No records" : continue
  +default: "Warning $(RETURN_CODE)" : continue
  -default: "Unexpected SQL error" : exit
%}
%}
 

%{***************************************************************************
*  HTML block: INPUT            Title: Dynamic Query Selection            *
*                                                                           *
*  Description: Queries the database to                                     *
*               create the selection list for the employees                 *
****************************************************************************%}
%HTML(INPUT) {
<html><head><title>Dynamic Query Selection</title></head><body><h3>$(exampleTitle)</h3>
<p>This example queries a database and dynamically uses the results to create
a selection list using the <em>%REPORT</em> block.
<hr>
<p>The following selection list is built dynamically by looking into the database
<form method="post" action="report">
@queryDB()<input type="submit" value="Select Employee">
</form>
<hr>
</body>
</html>
%}
 
%{***************************************************************************
*  HTML block:    REPORT     Title: Dynamic Query Selection               *
*  Description: Result page - Calls the SQL function to show the selected   *
*               employee information from the previous page                 *
****************************************************************************%}
%HTML(REPORT) {
<html>
<head>
<title>Dynamic Query Selection</title>
</head>
<body>
<h3>You selected employee name = $(emp_name)</h3>
<p>Here is the information for that employee:
<PRE>
------------------------ Database query results ------------------------------
@fname()
</PRE>
<hr><a href="input">Return to previous page</a>
</body>
</html>
%}
 
%{     End of Net.Data macro 1 %}
===========================================================================
%{**************************** Include File **********************************
*   FileName = sqlsamp1.hti                                                 *
*   Description:                                                            *
*     This include file provides global DEFINEs for                         *
*      sqlsamp1.mac Net.Data macro.                                              *
*                                                                           *
*                                                                           *
****************************************************************************%}
%define {
    DATABASE  ="SAMPLE"
    LOGIN     ="USERID"
    PASSWORD  ="PASSWORD"
    emp_name   ="a name"
    exampleTitle = "SQL Example"
    myTable = "EMPLOYEE"
%}
 
%{     End of include file  %}


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]