I'd like to run an insert statement only if a certain condition is met. Is there a way to do that in pure sql?
An example of what I'd like to do in pl/pgsql:
CREATE OR REPLACE FUNCTION set_search_lock (
userid IN integer,
)
RETURNS boolean AS $body$
DECLARE
BEGIN
PERFORM 'A' FROM LOCK_TABLE WHERE USERID = userid LIMIT 1;
IF NOT FOUND THEN
INSERT INTO LOCK_TABLE (USERID) VALUES (userid);
RETURN true;
ELSE
RETURN false;
END IF;
END;
$body$
LANGUAGE PLPGSQL;