Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Data Bases > Pgsql Novice > how to create a...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 1 Topic 3160 of 3323
Post > Topic >>

how to create a function in plpgsql which picks the name of a variable which is NULL out of a list of variables?

by public@[EMAIL PROTECTED] (Miernik) Jul 4, 2008 at 09:17 AM

Hello. I am trying to create a function in plpgsql, but I got stuck on
one issue, looking for some clues.

Inside the function I have 4 variables of type boolean:

  a1 boolean;
  a2 boolean;
  a3 boolean;
  a4 boolean;

Their values can be TRUE, FALSE or NULL;

Now I need this function to return the name of a randomly picked
NULL-valued variable out of these four, or return NULL in case none of
the 4 variables are NULL.

So if for example a1 is TRUE, a2 and a3 are NULL and a4 is FALSE, then
the function should return string 'a2' or 'a3' (randomly picked).

I written this part, but looking for any clues how to proceed:



CREATE OR REPLACE FUNCTION pick_random_a(thisid integer) RETURNS
varchar(2) AS $BODY$
DECLARE
  a1 boolean;
  a2 boolean;
  a3 boolean;
  a4 boolean;
  this_a varchar(2);
BEGIN
  SELECT va1, va2, va3, va4 INTO a1, a2, a3, a4 FROM tmp_a WHERE id =
thisid;


--
-- so here should be some block which gives the variable this_a
-- a string or NULL value, how to do it?
--


  RETURN this_a;
END;
$BODY$ LANGUAGE plpgsql;




-- 
Miernik
http://miernik.name/


-- 
Sent via pgsql-novice mailing list (pgsql-novice@[EMAIL PROTECTED]
)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice
 




 1 Posts in Topic:
how to create a function in plpgsql which picks the name of a va
public@[EMAIL PROTECTED]   2008-07-04 09:17:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Dec 5 5:26:13 CST 2008.