pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jonathan Gardner <jgardner@jonathangardner.net>
To: Terence Kearns <terencek@isd.canberra.edu.au>
To: pgsql-sql@postgresql.org
Subject: Re: parse error for function def
Date: Thu, 17 Jul 2003 14:37:01 -0700
Message-ID: <200307171437.02671.jgardner@jonathangardner.net> (raw)
In-Reply-To: <3F167E9D.2090709@cts.canberra.edu.au>
References: <3F167E9D.2090709@cts.canberra.edu.au>

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 17 July 2003 03:46, Terence Kearns wrote:
> CREATE FUNCTION base.fn_fkey_check(text, text, int4) RETURNS bool AS
> 'DECLARE
> BEGIN
>    RETURN (SELECT count($1) FROM $2 where $1 = $3)::bool;
> END;'
> LANGUAGE 'sql';
>
> produces this error
> ERROR:  parser: parse error at or near "RETURN" at character 20
>
> I'm trying to create a function to use on a trigger to check reference
> to views since pg does not support foreign keys referencing views.

If you want to write triggers, there is great documentation on that in the 
PL/pgSQL documentation.

I'll give this a shot:

CREATE FUNCTION base.fn_fkey_check(name, name, name, name) RETURNS TRIGGER AS 
'
BEGIN
    " $1 column1 (the table that has the foreign key column)
    " $2 - table2 $3 - column2 (the table/column that is the primary key being 
reference)
    EXECUTE ''SELECT $3 FROM $2 WHERE $3=NEW.$1'';
    IF FOUND
    THEN RETURN NEW
    
    RAISE ERROR ''Foreign key violation.'';
    RETURN;
END'
LANGUAGE 'plpgsql';

- -- 
Jonathan Gardner <jgardner@jonathangardner.net>
(was jgardn@alumni.washington.edu)
Live Free, Use Linux!
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE/Fxb9WgwF3QvpWNwRAh6tAJ9TxkqmKd8NrsQSwadV9FQ8PuSFIACg2sg9
6KPuw+msH/faa8F0xR+FSTI=
=Yo2a
-----END PGP SIGNATURE-----



view thread (5+ messages)

Message-ID: <200307171437.02671.jgardner@jonathangardner.net>
Permalink:  ../200307171437.02671.jgardner@jonathangardner.net/
Also on:    postgresql.org/message-id/200307171437.02671.jgardner@jonathangardner.net

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: jgardner@jonathangardner.net, terencek@isd.canberra.edu.au
  Subject: Re: parse error for function def
  In-Reply-To: <200307171437.02671.jgardner@jonathangardner.net>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox