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

推荐订阅源

L
LangChain Blog
C
Check Point Blog
月光博客
月光博客
Y
Y Combinator Blog
I
InfoQ
B
Blog RSS Feed
P
Proofpoint News Feed
腾讯CDC
博客园 - Franky
MyScale Blog
MyScale Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
V2EX
Hugging Face - Blog
Hugging Face - Blog
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
罗磊的独立博客
B
Blog
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
美团技术团队
大猫的无限游戏
大猫的无限游戏

IT Jungle

Finally: Some Pricing Information On The Power S1112 Entry Server - IT Jungle Rocket Automates Manual IBM i Tasks With AI - IT Jungle Guru: Where’s The Table? - IT Jungle LaserVault Goes iSCSI With Virtual Tape Library - IT Jungle IBM i PTF Guide, Volume 28, Number 31 - IT Jungle Will Power Chips Get A Converged Arm Instruction Set Like Z Mainframe CPUs? - IT Jungle Thinking About Moving IBM i To The Cloud? Don't Start With The Quote - IT Jungle Precisely To Add Ransomware Protection In MIMIX 11 - IT Jungle It’s D-Day For Cybersecurity, AI Firms Warn - IT Jungle IBM i PTF Guide, Volume 28, Number 30 - IT Jungle Oracle Dips A Toe Into IBM’s EBCDIC World - IT Jungle When Your Small IBM i Team Is Really A Team Of One - IT Jungle Guru: Putting Failure Handling In Its Place - IT Jungle Inside The Security Enhancements In ACS - IT Jungle IBM i PTF Guide, Volume 28, Number 28: A Crazy Number of Security Vulnerability Patches - IT Jungle IBM i PTF Guide, Volume 28, Number 29 - IT Jungle IBM i PTF Guide, Volume 28, Number 28: A Crazy Number of Security Vulnerability Patches - IT Jungle Inside The Encryption Key Management Changes In IBM i 7.6 - IT Jungle FalconStor Moved To The Blue Lagoon, And Is Poised For Growth Because Of It - IT Jungle Astera Makes Extracting Legacy Report Data an AI Specialty - IT Jungle IBM i PTF Guide, Volume 28, Number 27 - IT Jungle Welcoming The New IBM i Chief Architect And Other New Top Brass - IT Jungle A Deep Dive Into That Power S1112 Entry Power11 Server - IT Jungle Guru: Beyond Three-Part Naming – Running SQL Across Remote IBM i Systems - IT Jungle How IBM Bolstered IBM i Resilience In The Summer Tech Refreshes - IT Jungle IBM i PTF Guide, Volume 28, Number 26 - IT Jungle Power Systems Has A Great Quarter; System Z, Not So Much - 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: Claude’s SQL Tip - IT Jungle
Ted Holt · 2026-08-10 · via 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