惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
量子位
阮一峰的网络日志
阮一峰的网络日志
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
美团技术团队
J
Java Code Geeks
Apple Machine Learning Research
Apple Machine Learning Research
腾讯CDC
V
Visual Studio Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
IT之家
IT之家
博客园 - 【当耐特】
L
LangChain Blog
A
About on SuperTechFans
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
博客园_首页
WordPress大学
WordPress大学
博客园 - Franky
Engineering at Meta
Engineering at Meta
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
M
MIT News - Artificial intelligence

Comments for IT Jungle

Guru: Where’s The Table? - IT Jungle Will Power Chips Get A Converged Arm Instruction Set Like Z Mainframe CPUs? - IT Jungle Oracle Dips A Toe Into IBM’s EBCDIC World - IT Jungle Guru: Putting Failure Handling In Its Place - IT Jungle Inside The Security Enhancements In ACS - IT Jungle Guru: Assertions, Take 2 - IT Jungle Guru: Beyond Three-Part Naming – Running SQL Across Remote IBM i Systems - IT Jungle Does AI Mark The End Of The ERP Era? - IT Jungle Guru: Deterministic Application Development With AI - IT Jungle What IBM’s Got Cooking In Db2 For i In The Summer TRs - IT Jungle Guru: Creating Excel Spreadsheets With Python - IT Jungle The Power11 IBM i P05 Entry Machine Finally Arrives - IT Jungle Guru: Analyzing User Session Statistics, Part 1 - IT Jungle Your IBM i Jobs Don’t Live On An Island Anymore - IT Jungle Present Timestamps in the Local Time Zone - IT Jungle Set Your Library List From A Job Description - IT Jungle GenAI Is The Death Of Deterministic Project Budgeting - IT Jungle Guru: Single Threading A Program Execution - IT Jungle Guru: Where’s The Table? - IT Jungle Big Blue Unveils Bob Premium Pack For IBM i - IT Jungle Guru: SQL Sequences In RPG Let Db2 Handle The Counting - IT Jungle DB2 for i 7.2 Features and Fun, Part 1 - IT Jungle Guru: DateTime Rules Of Thumb - IT Jungle Spring IBM i Tech Refreshes Will Come A Bit Later This Year - IT Jungle As I See It: The Surgical Years - IT Jungle After A Few Short Years, VS Code Passes Rational Developer for i - IT Jungle Guru: Managing The Lifecycle Of Your Service Programs – Updates Without Chaos - IT Jungle Where We Are And Where We Are Headed With AI On IBM i - IT Jungle And Then There Were Two: Big Blue Withdraws IBM i 7.4 - IT Jungle Guru: When Attention Turns To You – Writing Your Own ATTN Program - IT Jungle
Guru: Claude’s SQL Tip - IT Jungle
Ted Holt · 2026-08-10 · via Comments for IT Jungle

August 10, 2026

Today’s tip comes to us from Claude, as in the AI chatbot created by Anthropic. I don’t need to introduce him. Many readers of this august publication are far better acquainted with him than I am. I have heard reports of IBM i programmers who have quit writing source code, telling Claude to write it for them. Some folks seem to be right proud of that fact. Furthermore, I’m told that some places no longer allow humans to write source code.

I had a few ideas in mind for this article and was weighing the merits of each one when it occurred to me that I should consult Claude. If he can sling code, evidently better than I can, surely he could suggest a good topic for me to write about. He did better than that. He wrote the tip.

I began by telling Claude, “I need an IBM i-related technical tip. Good topics are RPG and SQL.” He gave me four categories to select from and the option to request more categories. I selected Embedded SQL and RPG. That’s always a good one. Here is what he wrote:

Embedded SQL in RPG – Using Cursors Efficiently

One of the most common mistakes RPG developers make with embedded SQL is using a cursor when they don’t need one, or not using one when they do. Here’s the core pattern to understand:

When to use SELECT INTO vs. a Cursor

Single-row fetch – use SELECT INTO:

exec sql
  SELECT cusname, balance
    INTO :cusName, :balance
    FROM customers
    WHERE cusno = :custNumber;

Clean, simple, no cursor needed. If the query returns more than one row, SQL will throw +01403 or -00811 – so only use this when you’re fetching exactly one row.

Multi-row results – use a Cursor:

exec sql
  DECLARE c1 CURSOR FOR
    SELECT cusno, cusname, balance
      FROM customers
      WHERE status = :activeStatus
      ORDER BY cusname;

exec sql OPEN c1;

dow sqlcode = 0;
  exec sql
    FETCH NEXT FROM c1
      INTO :cusNo, :cusName, :balance;

  if sqlcode = 0;
    // process row
  endif;
enddo;

exec sql CLOSE c1;

Pro Tip – Check SQLSTATE, not just SQLCODE

After every SQL statement, check SQLSTATE for portable, readable status handling:

if sqlstate = '02000';   // no data found — normal end of fetch
  leave;
endif;

if sqlstate >= '02000' and sqlstate < '03000';  // warning range
  // handle warning
endif;

SQLCODE = 100 and SQLSTATE = ‘02000’ both mean no more rows, but SQLSTATE is the SQL standard and more self-documenting.

Bonus — Use SET OPTION at the top of your program

exec sql
  SET OPTION
    COMMIT    = *NONE,
    CLOSQLCSR = *ENDMOD,
    DATFMT    = *ISO;

This controls commitment control, cursor lifespan, and date format at the source level – much cleaner than fiddling with job settings and avoids hard-to-trace runtime surprises.

Bottom line: Use SELECT INTO for single-row lookups, cursors for sets, always check SQLSTATE after fetches, and use SET OPTION to make your SQL behavior explicit and portable. Want to go deeper on any of these – like scrollable cursors or blocked fetching for performance?

That concludes Claude’s technical tip. I didn’t answer his concluding question.

I have to say that I’m somewhat impressed. This is a decent tip. I’ve worked on many programs where the programmer opened a cursor, executed one FETCH, and closed the cursor. In such situations I much prefer SELECT INTO. I infer that Claude does, too. And it was good of him to mention SET OPTION.

I will end with a couple of observations. First, Claude advised the use of SQLSTATE rather than SQLCODE. I whole-heatedly concur. I never use SQLCODE. However, his example used SQLCODE.

Second, I would have added that the SQL SET is an alternative to SELECT INTO.

exec sql set (:cusName, :balance) =
 (SELECT cusname, balance          
    FROM customers                 
    WHERE cusno = :custNumber);

Does this mean that Claude is going to write my articles from now on? It would surely ease my life. Claude seemingly does in fractions of a second what sometimes takes me days to accomplish.

Nah.

Ted Holt is the original, the one and only, chief of the Four Hundred Gurus. We are glad he is back with us writing technical material that helps IBM i programmers. He is a self-employed, independent programmer living near Tupelo, Mississippi, who is old enough to retire but is not ready to do so. He still enjoys programming and is available to help others as needed. He welcomes your comments, questions, and suggestions.

RELATED STORIES

Guru: Where’s The Table?

Guru: DateTime Rules Of Thumb

Guru: Load A Varying-Dimension Array With One SQL Fetch

The Four Hundred Guru Retires

Guru: Dynamic Arrays Come To RPG

Guru: Dynamic Arrays Come To RPG – The Next Part Of The Story

Guru: Dynamic Arrays Come To RPG – Limitations, Circumventions, And More

Guru: Global Variables in Modules

Guru: Assertions, Take 2

Guru: Using Mixed Lists To Add “Data Structures” To CL Commands

Guru: TryIT – You’ll Like It

Guru: Aliases — Underused and Unappreciated

Guru: Beware of SQL Precompiler Variables

Guru: The SND-MSG Op Code And Message Subfiles

Guru: The CALL I’ve Been Waiting For

The Four Hundred Guru Retires

Guru: Global Variables in Modules

Guru: Abstract Data Types and RPG

Guru: Flexible Interfaces

Guru: Quick And Handy RPG Output, Take 2

Guru: What Is Constant Folding And Why Should I Care About It?

Guru: Alternate SQL Row-Selection Criteria Revisited Revisited

Guru: Another Red Flag – Joining On Expressions

Guru: Set Beats A Loop

Guru: The Deception of Fractional Labeled Durations

Guru: Elapsed Time For Human Beings

Guru: One-Shot Requests and Quoted Column Names

Guru: Use SQL To Replace Reports

Guru: Date Format Confusion

Guru: Compare Pieces Of Source Members

Guru: Stub Testing And SQL

Guru: QCMDEXC Makes A Good CPP

Guru: SELECT INTO And Arrays

Guru: I’m A Number, You’re A Number, Everybody’s A Number

Guru: SQL PL, WHILE And REPEAT Loops