bringing the power of sql t...1

7
Administrator Backup and Recovery DB2 High Availability LPAR Networks Performance Security Systems Management Tivoli Trends AIX Linux Open Source What's New Tips & Techniques Application Development Systems Management Miscellaneous Case Studies Automotive Healthcare Manufacturing Miscellaneous Non-profit Retail Storage Disk Servers Software Tape Product News Buyer's Guide Administrator Backup and Recovery DB2 Domino High Availability LPAR Networks Performance Printing Security Systems Management WebSphere Windows Integration Developer General Java Modernization RPG WebSphere Trends IBM Announcements Linux Open Source SOA What's New Tips & Techniques Application Development Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht= 1 of 7 9/27/2010 10:23 AM

Upload: nairarung

Post on 17-Jul-2016

7 views

Category:

Documents


0 download

DESCRIPTION

as400

TRANSCRIPT

Page 1: Bringing the Power of SQL t...1

Administrator

Backup and Recovery

DB2

High Availability

LPAR

Networks

Performance

Security

Systems Management

Tivoli

Trends

AIX

Linux

Open Source

What's New

Tips & Techniques

Application Development

Systems Management

Miscellaneous

Case Studies

Automotive

Healthcare

Manufacturing

Miscellaneous

Non-profit

Retail

Storage

Disk

Servers

Software

Tape

Product News

Buyer's Guide

Administrator

Backup and Recovery

DB2

Domino

High Availability

LPAR

Networks

Performance

Printing

Security

Systems Management

WebSphere

Windows Integration

Developer

General

Java

Modernization

RPG

WebSphere

Trends

IBM Announcements

Linux

Open Source

SOA

What's New

Tips & Techniques

Application Development

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

1 of 7 9/27/2010 10:23 AM

Page 2: Bringing the Power of SQL t...1

Systems Management

Case Studies

Automotive

Banking/Finance

Healthcare

Insurance

Manufacturing

Miscellaneous

Non-profit

Retail

Storage

Disk

Optical

Servers

Tape

Product News

Product Reviews

ENDPGM Main Page

Administrator

Backup and Recovery

CICS

DB2

High Availability

IMS

LPAR

Migration

Networks

Performance

Security

Systems Management

Tivoli

Trends

Linux

Open Source

Security

SOA

What's New

z/OS

z/VM

Tips & Techniques

Application Development

Systems Management

Case Studies

Automotive

Banking/Finance

Healthcare

Insurance

Manufacturing

Miscellaneous

Retail

Storage

Disk

Servers

Software

Tape

Product News

Stop Run

Buyer's Guide Main Page

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

2 of 7 9/27/2010 10:23 AM

Page 3: Bringing the Power of SQL t...1

Business Strategy

Competitive Advantage

Consolidation

Executive Perspective

Green IT

Migration

Open Source

ROI

Infrastructure

Blades

Storage

Systems Management

Case Studies

Distribution

Healthcare

Manufacturing

Services

Web 2.0

Cloud

Social Media

Trends

Collaboration

IBM Announcements

IBM Research

Open Source

Social Media

What's New

Product News

AIX

MAINFRAME

POWER

Newsletters

About Us

Subscribe

Current Issue

Archive

IBM i

ALL EDITIONS

ADMINISTRATOR

DEVELOPER

TRENDS

TIPS & TECHNIQUES

CASE STUDIES

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

3 of 7 9/27/2010 10:23 AM

Page 4: Bringing the Power of SQL t...1

STORAGE

PRODUCT NEWS

ENDPGM

BUYER'S GUIDE

Developer > RPG

Presented by:

ASG

Bringing the Power of SQL to Your RPG Program

The basics of embedding SQL into RPG

August 2005 | by Susan Gantner and Jon Paris

Print Email

Many RPG programmers have used interactive SQL as a tool to quickly browse data or create test data scenarios but have stopped short of embedding it into their

RPG programs. If you fall into this category, this article will introduce you to the basics. Embedding SQL into your programs allows you to perform more powerful

data selection, sequencing and updating than is possible with either logical files or Open Query File (OPNQRYF).

You can embed many kinds of SQL statements into your programs: SELECT, UPDATE and DELETE, of course, for manipulating data from files, as well as

CREATE, DROP and even ALTER SQL statements for creating and managing database objects such as physical files.

Basic Syntax

The basic syntax for embedding SQL into RPG looks like this:

C/EXEC SQL

C+ SELECT CusNam, City, State

C+ INTO :CustName, :City, :State

C+ FROM CustMast

C+ WHERE CusID = :CustInput

C/END-EXEC

The /EXEC SQL and /END-EXEC statements surrounding the SQL statement itself and the + signs in column seven for each line of the SQL statement are required

entries. The SQL statement itself is completely free format and can be typed anywhere between columns eight and 80, so the following is also perfectly valid syntax:

C/EXEC SQL

C+ SELECT CusNam, City, State INTO :CustName, :City, :State

C+ FROM CustMast WHERE CusID = :CustInput

C/END-EXEC

Those experienced with interactive SQL may be wondering about two aspects of the preceding SELECT statement:

1. What is the INTO clause?

2. Why are there colons (:) in front of some of the field names?

In interactive SQL, when you enter an SQL SELECT statement, the selected data typically appears on the screen. When you embed a SELECT statement into a

program, the data must go into RPG fields in your program. That's what the INTO clause does-it identifies the RPG fields for the values selected from the database

file. The names immediately following the SELECT verb are the database names; those following the INTO clause are the names of the program variables that will

receive the values. The two sets of names are mapped one-to-one by position in the list. The colons in front of the names identify them as "host variables" (i.e., they

represent RPG program variable names). (Note: As this example illustrates, the RPG variable names may match the external database field names but arent

required to do so.)

Since the SQL statement provides the name of the file from which the data is retrieved, no F specification is used for files that will be accessed by SQL statements.

Therefore, it's important to remember that the external field names from the file wont automatically be defined in the RPG program as they would if an F specification

were included. Therefore, you must define the fields in the RPG program, typically on the D specifications. You may find the use of externally described Data

Structures (DS) as a useful way of getting the appropriate field definitions into your program. This is particularly true if you're fond of using the "SELECT *" syntax.

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

4 of 7 9/27/2010 10:23 AM

Page 5: Bringing the Power of SQL t...1

Those of you who hate typing may want to take note of the following example which uses an externally described DS in the SQL statement:

D CustRec E DS ExtName(Customer)

C/EXEC SQL

C+ SELECT * INTO :CustRec

C+ FROM CUSTMAST WHERE CUSTID = :CustInput

C/END-EXEC

Embedding SQL into your programs allows you to perform more powerful data selection, sequencing and updating than is possible with

either logical files or Open Query File (OPNQRYF).

Next page: >>

Page 1 2 3 4

Susan Gantner is a technical editor with IBM Systems Magazine and co-owner of Partner400.

More Articles From Susan Gantner

Jon Paris is a technical editor with IBM Systems Magazine and co-owner of Partner400.

More Articles From Jon Paris

Advertisement

WEBINAR

Thursday, September 30 – 2pm (ET) from LANSA

IBM i Enterprise Web Development ---- Fact or Fiction?

Browse products and services for Developer.

Advertisement

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

5 of 7 9/27/2010 10:23 AM

Page 6: Bringing the Power of SQL t...1

Maximize your IT investment with monthly information from THE source...IBM Systems Magazine EXTRA & Marketplace eNewsletters.

SUBSCRIBE NOW.

View past IBM i EXTRAs here

Related Articles

Overcoming the Challenges of Embedding SQL into RPG Programs

Developer

Top 10 Hidden iDoctor Gems

E-Newsletter Exclusive | Save time and get more information from iDoctor with these tips and tricks.

Free-Format SQL and Other ILE RPG Precompiler Enhancements

Tips & Techniques

IBM i

AIX

MAINFRAME

POWER

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

6 of 7 9/27/2010 10:23 AM

Page 7: Bringing the Power of SQL t...1

Homepage

About Us

Contact Us

Subscriptions

Editorial Calendar

Advertise With Us

Reprints

Privacy Policy

Terms of Service

Sitemap

IBM Systems Magazine is a trademark of International Business Machines Corporation. The editorial content of IBM Systems Magazine is placed on

this website by MSP TechMedia under license from International Business Machines Corporation.

© 2010 MSP Communications, Inc. All rights reserved

Bringing the Power of SQL to Your RPG Program | IBM i | IBM Systems ... http://www.ibmsystemsmag.com/ibmi/august05/features/8695p1.aspx?ht=

7 of 7 9/27/2010 10:23 AM