0

How to recognize the data type of anyelement inside function?

CREATE OR REPLACE FUNCTION test1(par1 int,**par2 anyelement**)
RETURNS BOOL
AS $$
DECLARE rc bool := true;
BEGIN
    -- ?
    RETURN rc;
END;
$$
LANGUAGE plpgsql;

1 Answer 1

3

Use pg_typeof(any):

create or replace function test(par anyelement)
returns text language plpgsql as $$
begin
    return pg_typeof(par)::text;
end $$;

select test(100::int), test('2012-12-12'::date);

  test   | test 
---------+------
 integer | date
(1 row) 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.