PostgreSQL functions and triggers - function

I am trying out functions and triggers int postgreSQL, however i am having a problem, when the function is triggered it is giving me an error
ERROR: control reached end of trigger procedure without RETURN
this particular procedure is only executing an insert into command so i do not see why it needs a return
this is the script:
CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$
BEGIN
insert into Audit values('k',124,'l');
END;
$tree_stamp$
LANGUAGE plpgsql;
create trigger forest_aud_ins after insert on forest
for each row execute procedure forest_aud_func()
insert into forest values('Blue',1600,'Malta','Health Ltd')

The error message tells you all. You need to do a RETURN from the trigger function:
CREATE OR REPLACE FUNCTION forest_aud_func() returns trigger as $tree_stamp$
BEGIN
insert into Audit values('k',124,'l');
return new;
END;
$tree_stamp$
LANGUAGE plpgsql;
From the manual:
A trigger function must return either NULL or a record/row value having exactly the structure of the table the trigger was fired for.

Related

Trigger to create record from JSON object in PostgreSQL

Concept: a trigger what creates a new record in a table after a new JSON object has been created in another table. I don't want to make any modifications yet, just to "convert" JSON objects to records with a trigger.
Use the function jsonb_populate_record() in the trigger function, e.g.
create or replace function json_input_trigger()
returns trigger language plpgsql as $$
begin
insert into main_table
select *
from jsonb_populate_record(null::main_table, new.data);
return new;
end $$;
Fully working example.

mysql: use a procedure inside a trigger

I need to call a procedure that modifies root password inside a trigger.
This is the procedure
delimiter //
create procedure foobar ()
begin SET PASSWORD FOR 'root'#'localhost' = 'foobar';
end//
delimiter ;
and this is the trigger
delimiter //
create trigger rootpass
after delete on map
for each row
begin
call foobar;
end //
delimiter ;
It seems the syntax is correct but when I launch a delete I get this error
ERROR 1445 (HY000): Not allowed to set autocommit from a stored function or trigger
So I think I'm trying to do a forbidden thing, is there any way this could work in mysql?
I know the whole thing sounds weird/horrible but this is the way for my application to understand that something has been deleted.
When executing a statement would cause an implicit commit of the current transaction, that statement is not permitted within a trigger or stored function.
This includes a statement in a stored procedure that is invoked by a trigger or function -- there is no layer of abstraction here that makes the invalid action any more valid.

What is the MySQL equivalent of SQL Server's UPDATE() trigger function?

I need help converting this MS SQL update trigger to MySQL. My problem is with converting the MS SQL UPDATE() function, which identifies the specific columns that were updated:
ALTER TRIGGER [dbo].[tran_upd_action] ON [dbo].[tran_action]
FOR UPDATE AS
BEGIN
IF ##ROWCOUNT>0
BEGIN
IF update(column1) OR update(column2)
BEGIN
INSERT into tran_indexerqueue
(trankey, trkey2, tranname) SELECT tran_actionid, 0, 'tranaction' from inserted
END
END
END
This does not have the rowcount check, but the basic trigger syntax will be like this. Note that only the Super User role can create a trigger. Also, you have to change the delimiter first, which is usually a big gotcha for MSSQL DBAs.
DELIMITER $$
CREATE TRIGGER `tran_upd_action` AFTER UPDATE ON `tran_action` FOR EACH ROW BEGIN
INSERT INTO tran_indexerqueue
(trankey, trkey2, tranname)
VALUES(OLD.trankey, OLD.trkey2, OLD.tranname);
END $$
DELIMITER ;

Cannot create function in MySQL

I cannot add custom function to my MySql database,
I have problems even with simple function like this
DELIMITER $$
DROP FUNCTION IF EXISTS `foo`$$
CREATE FUNCTION `foo`(IN doWork boolean) RETURNS boolean
NO SQL
DETERMINISTIC
BEGIN
return doWork;
END%%
DELIMITER ;
when I execute above query , phpmyadmin show that all is ok, but when I want to call this function phpmyadmin say's that cannot find this function, also table ROUTINES in information_schema dont contains it.
your end delimiter is different from the defined one
replace
END%%
with
END $$

how to call StoredProcedure or Functions from a MySQL Trigger?

I am working on MySQL 5.1.3 and using PHPMyAdmin 3.1.3.1 to access it. With PHP as the Server side scripting Language. My problem statement is can we call a Stored Procedure or Function from the Trigger statement so that when ever an INSERT|UPDATE|DELETE trigger is called, it calls the SP for updating some other tables according to the logic defined.
Look here Mysql Trigger Syntax
mysql> delimiter //
mysql> CREATE TRIGGER upd_check BEFORE UPDATE ON account
-> FOR EACH ROW
-> BEGIN
-> IF NEW.amount < 0 THEN
-> SET NEW.amount = 0;
-> ELSEIF NEW.amount > 100 THEN
-> SET NEW.amount = 100;
-> END IF;
-> END;//
mysql> delimiter;
It can be easier to define a stored procedure separately and then invoke it from the trigger using a simple CALL statement. This is also advantageous if you want to invoke the same routine from within several triggers.
There are some limitations on what can appear in statements that a trigger executes when activated:
The trigger cannot use the CALL statement to invoke stored procedures that return data to the client or that use dynamic SQL. (Stored procedures are permitted to return data to the trigger through OUT or INOUT parameters.)
The trigger cannot use statements that explicitly or implicitly begin or end a transaction such as START TRANSACTION, COMMIT, or ROLLBACK.