I have a general AFTER UPDATE trigger for my users table to check if a column has changed, and if so, call a stored procedure.
The problem is the stored procedure does some calculations and itself updates a field in users.
How do I avoid the recursion if my stored procedure updates the users table, which invokes the trigger, which again invokes the stored procedure?
Thanks!
MySQL doesn't let you disable triggers (without dropping and recreating them), but you have a couple of options:
Don't update the users table from within the procedure.
Add a field to the users that the procedure would set to a specific value on update. When the trigger sees that value for that field, don't call the procedure.
Use a global variable to accomplish the above (NOT connection safe - will disable triggers for all connections).
Related
I need to call bash script when mysql database schema has changed. As example queries:
ALTER TABLE, CREATE TABLE, DROP TABLE
it possible?
Don't think that's possible any way. You could have probably use a DDL Trigger but MySQL doesn't support one. See worklog https://dev.mysql.com/worklog/task/?id=2418.
Though you can write a stored procedure to perform the business logic and call that procedure but capturing the DDL event isn't possible AFAIK. You should also check on Event Scheduler in MySQL
I need help with calling a stored procedure. Let me explain briefly what the problem is.
I have a "booking" table and whenever new booking is made and new data is inserted into the booking table, I need to call a stored procedure which aims to update field in another table. I have problem with calling the stored procedure after user inserts data into "booking". I don't want to use trigger it needs to be SP. Do you guys think I can use something like AFTER INSERT table_name CALL stored_procedure(). etc
Any help is welcome.
Thanks a lot.
What you are describing is an AFTER INSERT TRIGGER. You can put your store procedure within the trigger although is not recommendable.
Options you have:
Call a trigger made by sql code (no store procedures)
Wrap the INSERT and the CALL to your SP inside a TRANSACTION
I know that in triggers you have the keywords NEW and OLD to refer to the entry that is being, or was, inserted into the table to which the trigger is bound. Are there any other keywords? I'm looking for one specifically that reference's the table that the trigger is bound to (like CUR_TABLE, or something); this way I can copy the trigger and apply it to several tables with different names and not need to alter the body of the trigger? Thanks in advance for any help!
Dynamic SQL cannot be used in triggers. For the trigger to exist, then the developer already knows what table he's in - so the table name should theoretically be hard coded.
If you are generating triggers, from say a stored procedure, you can generate them with variable table names - but cannot execute them (so you would have to take the result of the stored procedure and execute it separately).
See: http://dev.mysql.com/doc/refman/5.6/en/stored-program-restrictions.html
SQL prepared statements (PREPARE, EXECUTE, DEALLOCATE PREPARE) can be used in stored procedures, but not stored functions or triggers. Thus, stored functions and triggers cannot use dynamic SQL (where you construct statements as strings and then execute them).
This is not possible in MySQL. You may do a small PHP script that generates the code for each "table name" in an array :)
I have a windows forms application and Customers table on it and the according table in DB(Customer). When editing the WinForm table and clicking the save button there is stored procedure executing and updating the Customer table in DB. When the table updated, there is a trigger executing. Trigger executes some stored procedure for logging purposes and save the data to the log table. I need to pass Environment.UserName to the log table without saving it in Customer table i.e. I must pass it to the final log table, but i can't add it in Customer table as column. And I don't know how to do it. Is there any way to do it.
Thanks.
You can store the username on the connection context (encode it as varbinary).
DECLARE #a VARBINARY(128)
SET #a = CAST('myuser' AS VARBINARY(128))
SET CONTEXT_INFO #a
Where #a contains the encoded username. In your trigger you can then get the context info with the function CONTEXT_INFO() and decode it to a varchar.
SELECT cast(CONTEXT_INFO() as VARCHAR(128))
I'm not sure if passing parameters to triggers is good idea.
Please consider use suser_sname() function in trigger body to recognize user.
Another option is moving logic from trigger to stored procedure and passing information about cuurent user to stored procedure.
How are STORED PROCEDURES different than TRIGGERS in the MySQL world ?
Stored procedures are stored as precompilated code (stored routine) and called by the programmer wherever it wants to fire. Stored procedure can return value(s). About procedures and functions.
Triggers are named database objects fired automatically when insert, delete, update (or other event) occurred, there can be no explicit invocation. Trigger can not return any data.
About triggers.
You can use procedures in trigger's code.
A trigger is a type of stored procedure, but it runs based off of an event on a table instead of just being a set of instructions to be executed repeatedly.
A trigger is defined to activate when an INSERT, DELETE, or UPDATE statement executes for the associated table.
A stored procedure is a group of Transact-SQL statements compiled into a single execution plan.