Function Syntax Help

Started by Dennisalmost 12 years ago7 messagesgeneral
Jump to latest
#1Dennis
dennis@kabonkulator.com

I having trouble with correct syntax to get this trigger function to compile. I have tried every combination of removing the ‘;’ characters but the function will not compile. Can someone tell me what I am doing wrong, I am stumped. I will be adding addition when clauses the case statement once I get this simplified version to compile.

I am using 9.3.4, both postgres and psql.

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

ERROR: syntax error at or near ";"
LINE 7: END;

In reply to: Dennis (#1)
Re: Function Syntax Help

On 25/06/2014 23:19, Dennis Ryan wrote:

I having trouble with correct syntax to get this trigger function to
compile. I have tried every combination of removing the �;� characters
but the function will not compile. Can someone tell me what I am doing
wrong, I am stumped. I will be adding addition when clauses the case
statement once I get this simplified version to compile.

I am using 9.3.4, both postgres and psql.

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);

The problem is the semi-colon after (NEW.*). There isn't one inside a
CASE construct.

Ray.

END;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

ERROR: syntax error at or near ";"
LINE 7: END;

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#3Pavel Stehule
pavel.stehule@gmail.com
In reply to: Dennis (#1)
Re: Function Syntax Help

Hello

You are using PLpgSQL CASE statement

this start by CASE keyword and finishing by END CASE keywords

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END;
RETURN NULL;
END CASE; -- <<<<<<<<<<<<<
$$ LANGUAGE plpgsql;

2014-06-26 0:19 GMT+02:00 Dennis Ryan <dennis@kabonkulator.com>:

Show quoted text

I having trouble with correct syntax to get this trigger function to
compile. I have tried every combination of removing the ‘;’ characters but
the function will not compile. Can someone tell me what I am doing wrong,
I am stumped. I will be adding addition when clauses the case statement
once I get this simplified version to compile.

I am using 9.3.4, both postgres and psql.

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

ERROR: syntax error at or near ";"
LINE 7: END;

#4Shaun Thomas
sthomas@optionshouse.com
In reply to: Dennis (#1)
Re: Function Syntax Help

On 06/25/2014 05:19 PM, Dennis Ryan wrote:

CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END;

You can't just have a bare CASE statement in plpgsql. Try this:

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.period = 201001 THEN
INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

--
Shaun Thomas
OptionsHouse, LLC | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604
312-676-8870
sthomas@optionshouse.com

______________________________________________

See http://www.peak6.com/email_disclaimer/ for terms and conditions related to this email

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#5Pavel Stehule
pavel.stehule@gmail.com
In reply to: Shaun Thomas (#4)
Re: Function Syntax Help

2014-06-26 18:28 GMT+02:00 Shaun Thomas <sthomas@optionshouse.com>:

On 06/25/2014 05:19 PM, Dennis Ryan wrote:

CASE

WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END;

You can't just have a bare CASE statement in plpgsql. Try this:

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
IF NEW.period = 201001 THEN

INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);
END IF;

RETURN NULL;
END;
$$ LANGUAGE plpgsql;

or use a plpgsql case
http://www.postgresql.org/docs/9.3/static/plpgsql-control-structures.html#PLPGSQL-CONDITIONALS

Regards

Pavel

Show quoted text

--
Shaun Thomas
OptionsHouse, LLC | 141 W. Jackson Blvd. | Suite 800 | Chicago IL, 60604
312-676-8870
sthomas@optionshouse.com

______________________________________________

See http://www.peak6.com/email_disclaimer/ for terms and conditions
related to this email

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

In reply to: Raymond O'Donnell (#2)
Re: Function Syntax Help

On 26/06/2014 17:26, Raymond O'Donnell wrote:

On 25/06/2014 23:19, Dennis Ryan wrote:

I having trouble with correct syntax to get this trigger function to
compile. I have tried every combination of removing the �;� characters
but the function will not compile. Can someone tell me what I am doing
wrong, I am stumped. I will be adding addition when clauses the case
statement once I get this simplified version to compile.

I am using 9.3.4, both postgres and psql.

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);

The problem is the semi-colon after (NEW.*). There isn't one inside a
CASE construct.

Whoops - Pavel is of course correct - this is a pl/pgsql CASE, not an
SQL one. Sorry - my mistake.

Ray.

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general

#7Pavel Stehule
pavel.stehule@gmail.com
In reply to: Raymond O'Donnell (#2)
Re: Function Syntax Help

2014-06-26 18:26 GMT+02:00 Raymond O'Donnell <rod@iol.ie>:

On 25/06/2014 23:19, Dennis Ryan wrote:

I having trouble with correct syntax to get this trigger function to
compile. I have tried every combination of removing the ‘;’ characters
but the function will not compile. Can someone tell me what I am doing
wrong, I am stumped. I will be adding addition when clauses the case
statement once I get this simplified version to compile.

I am using 9.3.4, both postgres and psql.

CREATE OR REPLACE FUNCTION sn_dm_b.pm_insert_trigger()
RETURNS TRIGGER AS $$
BEGIN
CASE
WHEN NEW.period = 201001
THEN INSERT INTO sn_dm_b.pm201001 VALUES (NEW.*);

The problem is the semi-colon after (NEW.*). There isn't one inside a
CASE construct.

no, using INSERT inside SQL CASE is not possible (with or without
semicolon), but inside PL/pgSQL it is ok. But PL/pgSQL statement uses END
CASE like others END .. END IF, END LOOP,

Regards

Pavel

Show quoted text

Ray.

END;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

ERROR: syntax error at or near ";"
LINE 7: END;

--
Raymond O'Donnell :: Galway :: Ireland
rod@iol.ie

--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general