postgresql function inserts new records with the pass-in timestamp parameter problem

32 views Asked by At

I have two text boxes where the user can specify the fromtime and totime. My applicaton has many code like -

SELECT *
FROM event_transition
-- first ? is fromtime and second ? is totime
WHERE begintime >= ? and endtime <= ?

they all work fine.

Now, I am writing a funtion to insert records into the event_transition table using the user specified fromtime -

    drop function XYZ(fromtime timestamp with time zone, totime timestamp with time zone);

    create or replace function XYZ(fromtime timestamp with time zone, totime timestamp with time zone)
        returns VOID
        language plpgsql
        as
    $$
    DECLARE
        colnames smallint[]; var smallint; i smallint;
    begin
         colnames := ARRAY(select distinct system_id
            from event_transition);
            -- where begintime >= fromtime and endtime <= totime);
         foreach var in array colnames loop
             INSERT INTO event_transition (system_id, wait_reason_id, begintime, endtime)
             VALUES
                -- none of the three lines below inserts records
                (var, i, fromtime, fromtime);
                --(var, i, TO_CHAR(fromtime, 'YYYY-MM-DD HH:MM:SS'), TO_CHAR(fromtime, 'YYYY-MM-DD HH:MM:SS'));
                -- (var, i, TO_CHAR(fromtime, 'YYYY-MM-DD HH:MM:SSTZH'), TO_CHAR(fromtime, 'YYYY-MM-DD HH:MM:SSTZH'));
         end loop;
    end; $$;

    select XYZ(?, ?);
    -- the line below works
    -- select XYZ('2024-02-01 12:15:50+02', '2024-02-15 13:15:50+02');

For some reason, no records were inserted by the INSERT clause. Your help is much appreciated.

I then thought that the '?' might be text data type and tried something like -

create or replace function XYZ(fromtime text, totime text)
....


select XYZ(?, ?);

It complained that '?' were not defined.

0

There are 0 answers