Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Postgres | IF statement with OR conditional

I have a stored procedure which is used to validate an incoming variable. If the variable is not 'maker' or 'member', then it should throw an error. If the variable is of the two above mentioned, it should continue normal processing.

The current issue with the below code is that it always returns false, therefore always throwing an error, no mater if the variable is of 'maker', 'member', or something else.

Any ideas on where I may be looking at my logic structure wrongly?

CREATE OR REPLACE FUNCTION validate_creator_type (
  "creator_type" VARCHAR(25)         -- $1
) RETURNS VOID AS $$
DECLARE
  _maker VARCHAR(25) := 'maker';
  _member VARCHAR(25) := 'member';
BEGIN

  -- Validate the creator type is allowed
  IF ($1 <> _maker) OR ($1 <> _member) THEN
    RAISE EXCEPTION 'Invalid creator type ---> %', $1
      USING HINT = 'The creator type can only be member or maker';
  END IF;

END $$
SECURITY DEFINER
LANGUAGE plpgsql;
like image 651
Bill Bensing Avatar asked Feb 17 '26 16:02

Bill Bensing


1 Answers

Change OR to AND and your problem should gone

    CREATE OR REPLACE FUNCTION validate_creator_type (
      "creator_type" VARCHAR(25)         -- $1
    ) RETURNS VOID AS $$
    DECLARE
      _maker VARCHAR(25) := 'maker';
      _member VARCHAR(25) := 'member';
    BEGIN

      -- Validate the creator type is allowed
      IF ($1 <> _maker) AND ($1 <> _member) THEN
        RAISE EXCEPTION 'Invalid creator type ---> %', $1
          USING HINT = 'The creator type can only be member or maker';
      END IF;

    END $$
    SECURITY DEFINER
    LANGUAGE plpgsql;
like image 111
Livius Avatar answered Feb 20 '26 08:02

Livius



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!