May 1


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

By NickLitten

May 1, 2020

SQL, Stored Procedures

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:

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

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.

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

Join the IBM i Community for FREE Presentations, Lessons, Hints and Tips