You are on page 1of 4

System iNetwork Head Nav Subscribe Log In Contact Us Advertise User login Username: * Password: * Request new password

Search Primary links Forums Archives Code Blogs Podcasts Webcasts e-Learning Guides Newsletters About Us Contact Us About the Network Tech Editor Profiles Editorial Calendar Writers Kit Advertise Join Network Categories RPG Programming Other Languages Application Development Database/SQL Availability Security Systems Management Networking IT Mgmt/Careers Site Links Solutions Store Events UK Centre Jobs System iPortal Home Content Embedded SQL -- Better than Ever! Article ID: 56938Posted July 17th, 2008 in RPG Programming By:Bob Cozzi Embedded SQL -- Better than Ever! It was back in the 1980s when I first used SQL embedded in RPG. Embedded SQL, as it is called, has had the same syntax as my old RPG III programs did for more t han 20 years, and it's been ugly for all those years. Today with i5/OS in most s hops, there's a new embedded SQL in town, but I continue to see shops coding in old-style, or dare I say "traditional," embedded SQL. Let's fix that problem rig ht now. Traditional Embedded SQL Here's a basic embedded SQL statement that mimics the CHAIN opcode, more or less . D custDS D custno C E DS S EXFMT AskUser extname(custmast) qualified Like(custds.cstnbr)

C if FKey <> F3 C MOVE DSPCUST CUSTNO C endif C/EXEC SQL C+ select * into :custDS C+ from custmast where cstnbr = :custno C/END-EXEC If you're experienced at using embedded SQL, you probably don't use the SELECT s tatement in this way too often, but it was the easiest way to illustrate my poin t. If you're new to embedded SQL, then I've got some explaining to do. The starting C/EXEC SQL is a compiler directive. Technically it is an SQL Prepro cessor directive that lets the preprocessor know you are about to begin an SQL s tatement. The second and third Calc specs (in this example) contain the "C+" characters. T his tells the preprocessor that you've specified SQL statements on these lines. Some people prefer to begin the SQL statements on the same line as the /EXEC SQL directive, but they and the 15 others who do it that are rare. However, either way works it's all a matter of style. The preprocessor was migrated to RPG IV back in the early to mid-1990s and has s erved those who used it well. But then in i5/OS V5R1, IBM introduced free- forma t RPG IV, and look what happened to our code: D custDS D custno E DS S extname(custmast) qualified Like(custds.cstnbr)

/FREE EXFMT AskUser; if FKey <> F3; custno = DspCust; endif; /END-FREE C/EXEC SQL C+ select * into :custDS C+ from custmast where cstnbr = :custno C/END-EXEC Every time we need to do embedded SQL, we have to Terminate Free Format Start Embedded SQL Code the SQL Statement Terminate Embedded SQL Start Free Format Rinse and Repeat This made coding with embedded SQL, in a word, ugly! Enter i5/OS Version 5 Release 4 After years of pleading and begging by IBM Canada (who own RPG IV), it seems IBM Rochester (who owns embedded SQL) realized that they needed a much cleaner synt ax for embedded SQL. Well, with V5R4, IBM hit a home run. The embedded SQL prepr ocessor appears (to the RPG IV programmer) as just another opcode that works in free format. No more /EXEC SQL followed by lines with goofy plus signs, followed by an /END-EXEC statement. Today the SQL preprocessor is as easy to use as the CHAIN opcode, for example: D custDS D custno E DS S extname(custmast) qualified Like(custds.cstnbr)

/FREE EXFMT AskUser; if FKey <> F3; custno = DspCust; endif; EXEC SQL select * into :custDS from custmast where cstnbr = :custno; /END-FREE RPG TnT: 101 Dynamic Tips 'n Techniques with RPG IV by Bob Cozzi is available no w. The latest book from author Bob Cozzi is 300-pages long and contains 101 exampl e RPG IV Tips and Techniques for everyday programming tasks, from date calculati ons, to regular express searches, to using APIs. Cozzi wrote down every cool tec hnique he's found over the years, updated them, and consolidated them into this compact book that is a great desktop companion -- and it includes full example s ource code. Order your copy today. The big change here is that the "EXEC SQL" directive is more opcode-like and the re's no more pesky "/END-EXEC"; instead, you specify a semicolon just like you d o to terminate any other RPG IV free-format statement. What about two embedded S QL statements? How about this: D custDS D custno E DS S extname(custmast) qualified Like(custds.cstnbr)

/FREE EXFMT AskUser; if FKey <> F3; custno = DspCust; endif; EXEC SQL select * into :custDS from custmast where cstnbr = :custno; if (FKey = F11); // Delete customer requested? exec sql delete from custmast where cstnbr = :custno; endif; /END-FREE The same syntax is used for all embedded SQL statements including but not limite d to DECLARE, PREPARE, OPEN, and FETCH. Lastly, if you haven't yet moved to free -format RPG IV, you can still use this syntax by coding (gulp) the following: D custDS D custno E DS S extname(custmast) qualified Like(custds.cstnbr)

C EXFMT AskUser C if FKey <> F3 C MOVE DSPCUST CUSTNO C endif C/FREE exec sql select * into :custDS from custmast where cstnbr = :custno; /END-FREE This example shows using standard, fixed-format RPG IV with V5R4's support for f ree-format, embedded SQL. Bob Cozzi is the host of RPG World Live, a live, weekly two-hour show on System i developments. It is aired each Friday from 10:00 AM to Noon, Central time on u stream.tv and on RPGWorld.com. Bob also produces RPG World, the most popular RPG IV developer conference of the year. Bookmark/Search this post with:

Login to post comments Email this page Printer-friendly version Relate d Links The Quest for Application Flexibility Read and Write LOBs from an RPG Pointer Field Tips for Programming and Development, Part 2 Tips for Programming and Development, Part 1 Accessing Database Members with SQL ProVIP Sponsors ProVIP Sponsors

Featured Links Sponsored Links Trevor Perry selects WebSmart PHP & Nexus Portal for KMR Systems - See Why! Barcode400 - Fully integrate & automate your labeling process. snap your back-end applications into any other application. FREE Paper: From WDSC to RDi - Making Software Change Easier with MKS. Footer Site Links Home Subscribe Now Advertise Contact Us Feedback Terms & Conditions Trademarks P rivacy Policy Copyright Penton Media

You might also like