Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7070903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:37:22+00:00 2026-05-28T05:37:22+00:00

This question is about Postgresql 8.3 . I’ve got a table with a field

  • 0

This question is about Postgresql 8.3.

I’ve got a table with a field containing conditions like ‘lastcontact is null’. In code, I want to loop through this table and for each record, I want to check ‘if condition then’, like in this example:

FOR myrec IN
    SELECT * FROM tabel ORDER BY colorlevel, volgnummer
LOOP
    if (myrec.conditie) then
        raise notice 'Condition % is true', myrec.conditie;
    else
        raise notice 'Condition % is false', myrec.conditie;
    end if;
END LOOP;

The table which I have called ‘tabel’ in this example:

ID | Conditie             | Colorlevel | Volgnummer | Code | Description
1  | lastcontact is null  | 1          | 1          | ...  | ...
2  | lastchanged is null  | 1          | 2          | ...  | ...
3  | lastmodified is null | 1          | 3          | ...  | ...

Is it possible to do the check I desire? The code above results in the following error:

ERROR:  invalid input syntax for type boolean: "lastcontact is null"

New section containing the result of Erwin’s function

I have used this function:

CREATE OR REPLACE FUNCTION foo(lastcontact timestamptz)
  RETURNS void AS
$BODY$
DECLARE
  myrec record;
  mycond boolean;
BEGIN

FOR myrec IN
    SELECT * FROM tabel ORDER BY colorlevel, volgnummer
LOOP
    EXECUTE 'SELECT ' || myrec.conditie || ' FROM tabel' INTO mycond;

    IF mycond then
        RAISE NOTICE 'Condition % is true', myrec.conditie;
    ELSE
        RAISE NOTICE 'Condition % is false', COALESCE(myrec.conditie, 'NULL');
    END IF;
END LOOP;

END;
$BODY$
language 'plpgsql' volatile
cost 100;

I get this error:

ERROR:  column "lastcontact" does not exist
LINE 1: SELECT lastcontact is null FROM tabel
           ^
QUERY:  SELECT lastcontact is null FROM tabel
CONTEXT:  PL/pgSQL function "foo" line 9 at EXECUTE statement1

I tried to find an explanation myself, but to no avail. It obviously is trying to run the statement against the database, but it should understand that ‘lastcontact’ is the variable that’s been given as a function parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:37:22+00:00Added an answer on May 28, 2026 at 5:37 am

    From the comments I finally think I understand. You need dynamic SQL:

    CREATE OR REPLACE FUNCTION foo(lastcontact timestamptz)
      RETURNS void AS
    $func$
    DECLARE
       myrec  record;
       mycond boolean;
    BEGIN
    
    FOR myrec IN
        SELECT * FROM tabel ORDER BY colorlevel, volgnummer
    LOOP
        IF myrec.conditie ~~ '%lastcontact %' THEN   -- special case for input param
            myrec.conditie := replace (myrec.conditie
                            , 'lastcontact '
                            , CASE WHEN lastcontact IS NULL THEN 'NULL '
                              ELSE '''' || lastcontact::text || ''' ' END);
        END IF;
    
        EXECUTE 'SELECT ' || myrec.conditie || ' FROM tabel' INTO mycond;
    
        IF mycond then
            RAISE NOTICE 'Condition % is true', myrec.conditie;
        ELSE
            RAISE NOTICE 'Condition % is false', COALESCE(myrec.conditie, 'NULL');
        END IF;
    END LOOP;
    
    END
    $func$  LANGUAGE plpgsql;
    

    Be aware, however, that this setup is wide open for SQL injection. Only use verified input.
    A function works in PostgreSQL 8.3 as well (no DO statements, yet).

    You cannot refer to parameters inside dynamic SQL (EXECUTE statement). You have to put the value into the query string.

    In PostgreSQL 8.4 or later you have the superior commodity of the USING clause. Alas, not in version 8.3. You should consider upgrading if you can.

    I put in a workaround for your old version. You have to take special care of the NULL value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question about Timers for windows services got me thinking: Say I have (and
Follow up to this question about GNU make : I've got a directory, flac
Followed this question about delayed_job and monit Its working on my development machine. But
This question is a follow up to my previous question about getting the HTML
I was reading this question about how to parse URLs out of web pages
I just came across this question about initializing local variables. Many of the answers
I have seen this question about deploying to WebSphere using the WAS ant tasks.
I have a question about this question . I posted a reply there but
This is sort of the Java analogue of this question about C# . Suppose
As kind of a follow up to this question about prefixes , I agree

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.