Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I launch a trigger on select statement in mysql?

Tags:

I am trying to run an INSERT statement on table X each time I SELECT any record from table Y is there anyway that I can accomplish that using MySQL only?

Something like triggers?

like image 328
Ahmad A Bazadgha Avatar asked May 26 '11 11:05

Ahmad A Bazadgha


People also ask

Can a SELECT statement fire a trigger?

No there is no provision of having trigger on SELECT operation. As suggested in earlier answer, write a stored procedure which takes parameters that are fetched from SEECT query and call this procedure after desired SELECT query.

How do I add a trigger to a SELECT query?

Nope - you can't trigger on SELECT - you'll have to create a stored procedure (or any other type of logging facility - like a log file or what ever) that you implicitly call on any query statement - easier if you create a wrapper that calls your query, calls the logging and returns query results.

Can we create trigger on SELECT statement in SQL Server?

This is not possible.


1 Answers

Short answer is No. Triggers are triggered with INSERT, UPDATE or DELETE.

Possible solution for this. rather rare scenario:

  • First, write some stored procedures that do the SELECTs you want on table X.
  • Then, restrict all users to use only these stored procedures and do not allow them to directly use SELECT on table X.
  • Then alter the stored procedures to also call a stored procedure that performs the action you want (INSERT or whatever).
like image 123
ypercubeᵀᴹ Avatar answered Sep 19 '22 15:09

ypercubeᵀᴹ