Re: SQL function

From: Michael Glaesemann <grzm(at)myrealbox(dot)com>
To: "Prasad dev" <esteem3300(at)hotmail(dot)com>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: SQL function
Date: 2005-07-28 03:23:47
Message-ID: 498368E8-5ACE-4A9D-A281-ED0EE9DC902C@myrealbox.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice


On Jul 28, 2005, at 12:13 PM, Prasad dev wrote:

> I want an SQL function a rough structure below-
>
> CREATE FUNCTION ins_both(int,int,int,int) RETURNS boolean AS '
> BEGIN
> INSERT INTO inv2 values($2,$3,$4);
> INSERT INTO inv1 values($1,$2,$3);
> COMMIT
> ' LANGUAGE SQL;
>
>
> but we cannot include Begin and commit in an SQL function
> so i came out with the following which is not right

The function is implicitly wrapped in a transaction. Either both
INSERTS will occur, or they'll both fail. You don't need to add START
TRANSACTION, END TRANSACTION, or COMMIT in the function body (nor can
you, which you've already discovered).

This should do what you want:

CREATE FUNCTION ins_both(int,int,int,int)
RETURNS boolean
LANGUAGE SQL AS $$
INSERT INTO inv2 values($2,$3,$4);
INSERT INTO inv1 values($1,$2,$3);
$$;

Michael Glaesemann
grzm myrealbox com

In response to

Browse pgsql-novice by date

  From Date Subject
Next Message Tom Lane 2005-07-28 05:22:32 Re: Index help
Previous Message Prasad dev 2005-07-28 03:13:17 SQL function