Thread: Function that returns Boolean
Hi, This is a simple function that returns a boolean .. create or replace function check_value( newValue IN VARCHAR, oldValue IN VARCHAR ) RETURN BOOLEAN as ' BEGIN IF ( newValue != oldValue) then return true; else return false; END IF; END; ' LANGUAGE 'plpgsql' But I get this error...what is wrong with this ? [Error] Script lines: 1-13 ------------------------- ERROR: syntax error at or near "RETURN" Line: 2 Thanks Sharmila
Its a typo..it should be "RETURNS BOOLEAN" and not "RETURN BOOLEAN" -Sharmila --- On Mon, 2/23/09, SHARMILA JOTHIRAJAH <sharmi_jo@yahoo.com> wrote: > From: SHARMILA JOTHIRAJAH <sharmi_jo@yahoo.com> > Subject: Function that returns Boolean > To: "General postgres mailing list" <pgsql-general@postgresql.org> > Date: Monday, February 23, 2009, 12:39 PM > Hi, > This is a simple function that returns a boolean .. > > create or replace function check_value( newValue IN > VARCHAR, > oldValue IN VARCHAR ) RETURN BOOLEAN > as > ' > BEGIN > IF ( newValue != oldValue) > then return true; > else > return false; > END IF; > END; > ' > LANGUAGE 'plpgsql' > > But I get this error...what is wrong with this ? > > [Error] Script lines: 1-13 ------------------------- > ERROR: syntax error at or near "RETURN" > Line: 2 > > Thanks > Sharmila
SHARMILA JOTHIRAJAH <sharmi_jo@yahoo.com> writes: > create or replace function check_value( newValue IN VARCHAR, > oldValue IN VARCHAR ) RETURN BOOLEAN Should be RETURNS BOOLEAN. You might want to fix whatever client code you are using so that it shows the error cursor, which would certainly have helped direct your attention to the right place instead of the wrong one. What I see in psql is ERROR: syntax error at or near "RETURN" LINE 2: oldValue IN VARCHAR ) RETURN BOOLEAN ^ regards, tom lane
----- "SHARMILA JOTHIRAJAH" <sharmi_jo@yahoo.com> wrote: > Hi, > This is a simple function that returns a boolean .. > > create or replace function check_value( newValue IN VARCHAR, > oldValue IN VARCHAR ) RETURN BOOLEAN ^^^^^^ RETURNS > as > ' > BEGIN > IF ( newValue != oldValue) > then return true; > else > return false; > END IF; > END; > ' > LANGUAGE 'plpgsql' > > But I get this error...what is wrong with this ? > > [Error] Script lines: 1-13 ------------------------- > ERROR: syntax error at or near "RETURN" > Line: 2 > > Thanks > Sharmila > > Adrian Klaver aklaver@comcast.net
On Mon, Feb 23, 2009 at 9:39 AM, SHARMILA JOTHIRAJAH <sharmi_jo@yahoo.com> wrote: > IF ( newValue != oldValue) One good piece of advice that Tom Lane pointed out to me was: IF ( newValue IS DISTINCT FROM oldValue ) is better due to (what may be) the unexpected results of equality testing when NULL values are thrown into the mix. -- Regards, Richard Broersma Jr. Visit the Los Angeles PostgreSQL Users Group (LAPUG) http://pugs.postgresql.org/lapug
On Mon, Feb 23, 2009 at 09:39:01AM -0800, SHARMILA JOTHIRAJAH wrote: > Hi, > This is a simple function that returns a boolean .. This should be an SQL function, as it doesn't do anything you need (or would even find convenient) for a more procedural language to do: CREATE OR REPLACE FUNCTION check_value(newValue TEXT, oldValue TEXT) RETURNS BOOLEAN LANGUAGE SQL AS $$ SELECT $1 IS DISTINCT FROM $2 $$; As others have pointed out, IS DISTINCT FROM covers the case where one or more of the arguments is a NULL. Cheers, David. > > create or replace function check_value( newValue IN VARCHAR, > oldValue IN VARCHAR ) RETURN BOOLEAN > as > ' > BEGIN > IF ( newValue != oldValue) > then return true; > else > return false; > END IF; > END; > ' > LANGUAGE 'plpgsql' > > But I get this error...what is wrong with this ? > > [Error] Script lines: 1-13 ------------------------- > ERROR: syntax error at or near "RETURN" > Line: 2 > > Thanks > Sharmila > > > > > -- > Sent via pgsql-general mailing list (pgsql-general@postgresql.org) > To make changes to your subscription: > http://www.postgresql.org/mailpref/pgsql-general -- David Fetter <david@fetter.org> http://fetter.org/ Phone: +1 415 235 3778 AIM: dfetter666 Yahoo!: dfetter Skype: davidfetter XMPP: david.fetter@gmail.com Remember to vote! Consider donating to Postgres: http://www.postgresql.org/about/donate