object: public.fn_set_now_as_updated_at | type: FUNCTION --
-- DROP FUNCTION IF EXISTS public.fn_set_now_as_updated_at() CASCADE;
CREATE FUNCTION public.fn_set_now_as_updated_at ()
RETURNS trigger
LANGUAGE plpgsql
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
PARALLEL UNSAFE
COST 1
AS $$
NEW.updated_at = NOW();
RETURN NEW;
$$;
looks like you forgot to add begin an end clause documentation
CREATE FUNCTION public.fn_set_now_as_updated_at ()
RETURNS trigger
LANGUAGE plpgsql
VOLATILE
CALLED ON NULL INPUT
SECURITY INVOKER
PARALLEL UNSAFE
COST 1
AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$;
Related
i made this trigger to prevent data modification in psql
but it prevent insert data too , how i can resolve this problem ?
CREATE OR REPLACE FUNCTION public.Check_Modification()
RETURNS trigger
LANGUAGE 'plpgsql'
COST 100
VOLATILE NOT LEAKPROOF
AS $BODY$
DECLARE
BEGIN
RAISE EXCEPTION ' Sorry u can not modifict the data ' ;
END IF;
RETURN NEW;
END;
$BODY$;
ALTER FUNCTION public.check_verwachte_bezetting()
OWNER TO postgres;
CREATE TRIGGER Check_Modification_trigger
BEFORE UPDATE
ON public.table
FOR EACH ROW
EXECUTE FUNCTION public.Check_Modification();
So I have the following function that the trigger calls when I insert rows into a table called 'auxi'. It seems not to work since I have the value 'id_usuario' with NULL on my table and the trigger is supposed to avoid that.
CREATE OR REPLACE FUNCTION firstRestriction() RETURNS Trigger
AS $$
DECLARE
operation boolean = false;
BEGIN
RAISE NOTICE 'HERE %',operation;
operation = operation OR isNULL(new.periodo, 'periodo');
operation = operation OR isNULL(new.id_usuario, 'id_usuario');
operation = operation OR isNULL(new.fecha_hora_retiro, 'fecha_hora_retiro');
operation = operation OR isNULL(new.origen_estacion, 'origen_estacion');
operation = operation OR isNULL(new.destino_estacion, 'destino_estacion');
operation = operation OR isNULL(new.tiempo_uso, 'tiempo_uso');
/*operation = operation OR isLessThanZero(new.tiempo_uso, 'tiempo_uso');*/
IF operation THEN
raise notice 'No se pudo insertar % % % % % %',new.periodo, new.id_usuario, new.fecha_hora_retiro,new.origen_estacion
,new.destino_estacion, new.tiempo_uso;
return NULL;
ELSE
return new;
END IF;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER checkFirstRestriction
BEFORE INSERT ON auxi
FOR EACH ROW
EXECUTE PROCEDURE firstRestriction();
And then I do
CREATE OR REPLACE FUNCTION cond1()
RETURNS VOID AS $$
BEGIN
INSERT INTO auxi
SELECT *
FROM datos_recorrido;
END;
$$ LANGUAGE PLPGSQL;
Also isNull is the following function
CREATE OR REPLACE FUNCTION isNULL(field anyelement, information TEXT) RETURNS boolean
AS $$
BEGIN
IF field IS NULL THEN
raise notice 'El campo % es NULL', information;
return true;
ELSE
return false;
END IF;
END;
$$ LANGUAGE plpgsql;
And then at certain point I call my funciton cond1().
I think my trigger is never being called. Could that be possible?
I'm trying to write a function with plpgsql for some reason i'm getting a syntax error at $$ language plpgsql;
This is the func i'm writing:
create or replace function hello() returns trigger as $$
begin
if new.my_sc!= null and new.his_sc!= null
then update people p
set p.score=p.score+1
where p.id = new.id;
end if;
return null;
$$ language plpgsql;
my Trigger:
after insert on game
execute procedure hello();
my syntax error comes in at the last line.
any idea?
Thankx
I have a trigger function.
I wish to check if an status is one of (5,6,4,7)
CREATE OR REPLACE FUNCTION a()
RETURNS trigger AS
$BODY$
begin
if old.status = (5,6,4,7) then
do something.
end if;
return old;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
how do I change it to proper syntax?
Use IN operator:
...
if old.status IN (5,6,4,7) then
do something.
end if;
...
This is reference of this link.
It only work with postgresql 9.x
CREATE OR REPLACE FUNCTION a()
RETURNS trigger AS
$BODY$
begin
if old.status = ANY (VALUES (5),(6),(4),(7))) then
do something.
end if;
return old;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
i am using postgres schemas to group views and functions and to keep multiple versions of them in a database (sometimes needed for backwards compatibility) since there are multiple versions of the same function in different schemas i cannot simply reference it by name but need to write the full qualified name "schema.funcname" to access it.
when referencing functions within schemaA from another function in schemaA i always have to write schemaA.funcname. this bugs me when i rename the schema later - is there a qualifier indicating that the function in the same schema schould be used? maybe like the "this" qualifier in java?
hope it can be understood what i am meaning
thx
You can use set schema to change search path for current session:
create schema test1;
create schema test2;
create function test1.f() returns int as $body$ begin return 1; end; $body$ language plpgsql;
create function test2.f() returns int as $body$ begin return 2; end; $body$ language plpgsql;
set schema 'test1';
select f();
set schema 'test2';
select f();
You can also add search_path to function definition:
create or replace function test1.x() returns int as $body$ begin return 1; end; $body$ language plpgsql;
create or replace function test1.f() returns int as $body$ begin return x(); end; $body$ language plpgsql set search_path = 'test1';
create or replace function test2.x() returns int as $body$ begin return 2; end; $body$ language plpgsql;
create or replace function test2.f() returns int as $body$ begin return x(); end; $body$ language plpgsql set search_path = 'test2';
select test1.f();
select test2.f();