IBM i SQL Stored Procedure : Check if Rows exist in Table

  • Home
  • /
  • Blog
  • /
  • IBM i SQL Stored Procedure : Check if Rows exist in Table

May 1, 2020

IBM i SQL Stored Procedure : Check if Rows exist in Table

To check if there are rows in a table within an IBM i SQL stored procedure, you can use the EXISTS or NOT EXISTS condition in an IF statement. Here’s an example of how you might structure your stored procedure to perform this check:

CREATE PROCEDURE CheckRowsInTable()
BEGIN
    -- Check if there are any rows in your table
    IF EXISTS (SELECT 1 FROM your_table)
    THEN
        -- Code to execute if rows exist
        CALL ProcedureIfRowsExist();
    ELSE
        -- Code to execute if no rows exist
        CALL ProcedureIfNoRowsExist();
    END IF;
END;

In this example, your_table is the name of the table you’re checking. The SELECT 1 is a simple way to test for the existence of rows without retrieving any data. If rows are present, the procedure ProcedureIfRowsExist is called; otherwise, ProcedureIfNoRowsExist is called.

Remember to replace your_table, ProcedureIfRowsExist, and ProcedureIfNoRowsExist with the actual names relevant to your database schema.

NickLitten


IBM i Software Developer, Digital Dad, AS400 Anarchist, RPG Modernizer, Shameless Trekkie, Belligerent Nerd, Englishman Abroad and Passionate Eater of Cheese and Biscuits.

Nick Litten Dot Com is a mixture of blog posts that can be sometimes serious, frequently playful and probably down-right pointless all in the space of a day.

Enjoy your stay, feel free to comment and remember: If at first you don't succeed then skydiving probably isn't a hobby you should look into.

Nick Litten

related posts:

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Subscribe NOW
7-day free trial

Take This Course with ALL ACCESS

Unlock your Learning Potential with instant access to every course and all new courses as they are released.
 [ For Serious Software Developers only ]

Online Learning for IBM i Software Technology Professionals

“The more that you read, the more things you will know. The more that you learn, the more places you’ll go.” – Dr. Seuss

>