I am trying to create function in postgres
CREATE OR REPLACE FUNCTION cms_inventory_proc()
RETURNS TRIGGER
AS
$BODY$
BEGIN
IF NEW.permanent = 'YES' THEN
INSERT INTO cms_inventory(id, version, scheme_id, register_num, cms_reference, sensor_id)
VALUES (NEW.id, 0, NEW.scheme_id, NEW.register_num, NEW.cms_reference, NEW.sensor_id);
END IF;
RETURN NEW;
END;
$BODY$
LANGUAGE plpgsql;
ERROR: syntax error at or near "IF"
LINE 9: IF NEW.permanent ='YES' THEN
While it is working in online compiler
Related
I have the following 2 functions:
CREATE or replace FUNCTION func_return_query() RETURNS SETOF varchar AS
$BODY$
BEGIN
RETURN QUERY SELECT ename from emp;
IF NOT FOUND THEN
RAISE EXCEPTION 'No flight at %.', $1;
END IF;
raise notice 'hello world,return query';
END;
$BODY$
LANGUAGE plpgsql;
select func_return_query()
When I run this code, I see a message hello world, return query.
But for this function here, I don't see that message hello world, return:
CREATE or replace FUNCTION func_return() RETURNS integer AS
$BODY$
BEGIN
return 10;
raise notice 'hell world,return';
END;
$BODY$
LANGUAGE plpgsql;
select func_return()
Both of them are using return, why return query doesn't stop the following code execution, when return does?
I made a code for update trigger. Whenever I update, I want to fill "price" column of table "new_book" with 1000 if "price" is blank, or 5% discount if "price" is not blank. My postsql code is
create function test()
returns trigger
as
$$
declare pr1 numeric ;
begin
case when old.price is null then pr1=1000
else pr1=pr1*0.95 end
end;
return new;
end
$$
language plpgsql;
but if i run this code, PGAdmin shows an error like that
ERROR: syntax error at or near "else"
LINE 8: else pr1=pr1*0.95 end
^
SQL state: 42601
Character: 117
How can I solve this problem?
You want an IF statement. And if you want to change the value that is stored in the table, you need to modify the new record:
create function test()
returns trigger
as
$$
begin
if new.price is null then
new.new_book := 1000;
else
new.new_book := new.price * 0.95
end if;
return new;
end
$$
language plpgsql;
In order for the assignment to actually be persisted, you have to use a BEFORE trigger:
create trigger set_new_book_trigger
before insert or update on the_table
for each row
execute procedure test();
I'm trying to create a trigger function in the PostgreSQL database with pgAdmin 4. The function should perform pg_notify and return newly inserted data in JSON. But I'm getting the error and can't figure out where is a mistake.
Code:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE 'plpgsql'
NOT LEAKPROOF
AS $BODY$ CREATE OR REPLACE FUNCTION weather_notify_func()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$$;$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
Error:
enter code here
ERROR: syntax error at or near "CREATE"
LINE 5: AS $BODY$ CREATE OR REPLACE FUNCTION weather_notify_func()
^
The solution is:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE 'plpgsql'
NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
You nested the function definition again into the create function statement:
CREATE FUNCTION ba_weather.weather_notify_func()
RETURNS trigger
LANGUAGE plpgsql
NOT LEAKPROOF
AS $BODY$
BEGIN
PERFORM pg_notify('weather_insert', row_to_json(NEW));
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION ba_weather.weather_notify_func()
OWNER TO me;
I am getting an error for the below FOR loop with cursor in a function:
ERROR: syntax error at or near "AS"
CREATE OR REPLACE FUNCTION functionName(custom varchar(15)) RETURNS INTEGER AS $$
DECLARE
...
BEGIN
...
FOR loop AS cursor CURSOR FOR
SELECT column FROM table
DO
...
END FOR;
RETURN someValue;
END;
$$
LANGUAGE plpgsql;
This is wrong syntax - Postgres doesn't support declaration of CURSOR inside FOR statement. See documentation:
CREATE OR REPLACE FUNCTION foo()
RETURNS void AS $$
DECLARE r record;
BEGIN
FOR r IN SELECT xx,yy FROM some_tab
LOOP
RAISE NOTICE 'row data: %', r;
END LOOP;
END;
$$ LANGUAGE plpgsql;
It looks so you are using ANSI SQL PSM syntax. PL/pgSQL is based on PL/SQL syntax (Oracle/ADA).
I have the following trigger function:
CREATE OR REPLACE FUNCTION update_modelname_function()
RETURNS trigger AS
$BODY$
BEGIN
IF tg_op = 'INSERT' THEN
new.model_name := upper(new.model_name);
RETURN new;
END IF;
IF tg_op = 'UPDATE' THEN
old.model_name := upper(old.model_name);
RETURN new;
END IF;
END
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
What I'm trying to achieve is for the value of the column model_name to always be uppercased when it's persisted in the table. However nothing seems to happen. Any ideas?
You accidentally updated OLD instead of NEW. Try:
CREATE OR REPLACE FUNCTION update_modelname_function()
RETURNS trigger
LANGUAGE plpgsql AS
$func$
BEGIN
IF TG_OP = 'INSERT' THEN
NEW.model_name := upper(NEW.model_name);
RETURN NEW;
ELSIF TG_OP = 'UPDATE' THEN
NEW.model_name := upper(NEW.model_name); -- !
RETURN NEW;
END IF;
END
$func$;
If the example shows the whole code, and the actual trigger(s) only fires on INSERT and/or UPDATE, further simplify:
CREATE OR REPLACE FUNCTION update_modelname_function()
RETURNS trigger
LANGUAGE plpgsql AS
$func$
BEGIN
NEW.model_name := upper(NEW.model_name);
RETURN NEW;
END
$func$;