Home > AS/400 Tips > iSeries administrator tips > Stored procedures allow you to balance network loads – Part II
iSeries 400 Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ISERIES ADMINISTRATOR TIPS

Stored procedures allow you to balance network loads – Part II


Ron Turull
02.14.2007
Rating: -4.00- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


SQL stored procedures are a great feature. They provide an excellent tool to help optimize client/server applications. By shifting key portions of the processing onto the server, network data flows can be reduced substantially. All this without bogging down the server, which is the reason you went to client/server in the first place.

Why use SQL stored procedures

In Part 1, we discussed how stored procedures are used primarily to increase efficiency in two ways:

  • Eliminating the downloading of temporary data and tables
  • Reducing the amount of overhead on the network.

    We also considered the case of relocating a series of SQL statements to decrease network message flows (see Case 1 in Part 1). Now let's consider another case.

    Case 2: Implementing unsupported functions on the server decreases data flows
    More on SQL tools:
    SQL Built-In Functions and Stored Procedures -- Chapter 2

    Squirrel: The universal SQL client

    Because of SQL's language limitations, a client application must retrieve the entire result table of a remote SELECT statement request to determine the actual data required. This is inefficient because all the data must travel across the network even if only a single result row satisfies the client application's conditions.

    For example, suppose a client application allows the user to search for neighborhoods whose median price range for a house is greater than a user-specified value. The client program transmits the following Select statement to the server:

    Select * From ucg/homesales Order By nbrhood

    The server must then transmit each record from the homesales file to the client application which, in turn, must calculate the median value for each neighborhood (nbrhood field) and compare it against the user-specified value.

    A much more efficient way of handling this is to embed the above Select statement in a program (i.e. stored procedure) on the server which will accept the user-specified minimum median value as a parameter. The program will then read each record resulting from the Select statement as the client program previously did. The client program code that calculates the medians and compares them to the user-specified value is likewise relocated to the server program. The server program writes a record to a temporary file for each neighborhood meeting the median price criteria. Finally, the server program returns the name of the temporary file to the client program.

    To run the stored procedure, the client program can execute the following SQL statement:

    Call ServerPgm (:minval, :tmpfil)

    where ServerPgm is the name of the program on the server, minval is a host variable containing the user-specified minimum median value (and will be passed as parameter 1), and tmpfil is a host variable that will be passed as parameter 2 and return with the name of the temporary file created by the stored procedure.

    After the Call statement, the client program can build and execute a Select statement over the temporary file. Each record retrieved from this file will meet the user's criteria for median home price.

    Note that it is also possible to combine these two steps into one large (and rather complicated) compound SQL statement on the server.

    New feature makes stored procedures even more convenient

    As of V5R3, it is possible to have dynamic result sets returned by stored procedures as open cursors. The calling program can then issue fetches (and other functions) against the open cursor. Here is an example:

    CREATE PROCEDURE ront/custmast2(IN inlastname CHAR(15)) LANGUAGE SQL
    DYNAMIC RESULT SETS 1
    BEGIN
    DECLARE C1 CURSOR FOR SELECT * FROM ront/custmast
    WHERE upper(cstlnm) like (upper(TRIM(inlastname)) || '%');
    OPEN C1;
    RETURN;
    END

    The Declare Procedure statement

    The Declare Procedure SQL statement is used when it is necessary to override the basic assumptions the Call statement makes about the stored procedure's name, the number and type of parameters, and whether or not the parameters are input, output, or both. Simple procedures, especially ones that do not have parameters, generally will not require the use of a Declare Procedure statement. However, using it will increase the readability of your code.

    The Declare Procedure statement is used to explicitly specify the program name (as it is known on the server), the name you will use on the CALL statement (if different), and the attributes of each parameter. You also specify the language in which the program was written.

    About the author: Ron Turull is editor of Inside Version 5. He has more than 20 years' experience programming for and managing AS/400-iSeries systems.

    Rate this Tip
    To rate tips, you must be a member of Search400.com.
    Register now to start rating these tips. Log in if you are already a member.




    Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



    RELATED CONTENT
    iSeries SQL commands and statements
    Enhancing RPG with external SQL stored procedures
    Tracking data changes on IBM i with triggers
    Introduction to SQLRPGLE on IBM i: Making a report
    Making the most of RPG data handling on IBM i
    When is the YES option for 'reuse deleted files' function the best choice?
    Monitoring members 'stuck' within a physical file on an EDI system
    Creating a host variable of the 'where in' statement in SQL
    SQL server error message -321
    Choose which column names are returned via ODBC when working with DB2 files
    Convert a numeric physical file to a character in SQL without leading zeros
    iSeries SQL commands and statements Research

    iSeries administrator tips
    Analyze the health of your IBM i server with iScore
    Researching high availability for your System i shop
    Translating Linux for IBM i admins: Using GUI to make it easy
    Translating Linux for IBM i admins: Working with jobs and networking
    OpenOffice: What to know before making the transition from Microsoft Office
    OpenOffice: An enterprise open source solution
    Database performance comparisons on IBM i
    Translating Linux for IBM i admins: User profile commands
    Modern System i reports using Client Access
    Tips for installing Lotus Domino server on a System i partition

    RELATED RESOURCES
    2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
    Search Bitpipe.com for the latest white papers and business webcasts
    Whatis.com, the online computer dictionary

    DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



  • iSeries Security - Security Tools, Physical Security and System Security
    HomeNewsTopicsITKnowledge ExchangeTipsBlogsAsk the ExpertsMultimediaWhite PapersProducts
    About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
    SEARCH 
    TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

    TechTarget Corporate Web Site  |  Media Kits  |  Site Map




    All Rights Reserved, Copyright 1999 - 2009, TechTarget | Read our Privacy Policy
      TechTarget - The IT Media ROI Experts