Thread: KEYEXISTS function

KEYEXISTS function

From
April L
Date:
In an old DOS database program I programmed in for years, there was a handy
function called

KEYEXISTS("value",index)

value of course was only in quotes if it was a string.

I'm imagining this could be pretty easily created as a function for
postgres, and I'm wondering if someone has already done this?

The recent discussion here on the Novice list sparked my memory of how very
often a KEYEXISTS type of function is useful. I know in my own work, an
attempt to insert a record where the primary key would be duplicated,
results in an error to the user, so I have to error trap and cannot "try to
insert and do something else if insert doesn't work" - I have to look for
the existence of a conflicting record and not insert if there is one.

Thank you,

- April



Re: KEYEXISTS function

From
Tom Lane
Date:
April L <april@i-netco.com> writes:
> In an old DOS database program I programmed in for years, there was a handy
> function called
> KEYEXISTS("value",index)

There's a perfectly standard EXISTS predicate in SQL ... why not
use that?
    EXISTS(SELECT 1 FROM table WHERE column = 'value')

            regards, tom lane