Firing a trigger when stored procedure execute - sql-server-2008

I am using SQL Server 2008. I have stored procedure, which (on some conditions) insert data in my table, this stored procedure will be call from application each time when my window opens. SQL Profiler shows it like below
exec TEST.dbo.spInsertRecords #parameter
Now what I want is to fire a trigger whenever this stored procedure will be call to execute.
Is it possible? If yes please share the syntax example.

Triggers are set on tables, if you really need something triggered upon calling your procedure (i honestly cannot see why since you shall be able to modify the procedure to do whatever behaviour you'd want), add some dummy table (a log table ?) , create your trigger on that table (on insert for example), and "trigger" the trigger inside your procedure (insert a row).

If the trigger is expected to execute some code, for example writing record into a log.
Write the code directly into the procedure.
If you want to execute universal code before or after all procedures, write the code in the place where the procedure is executed.
For example in PHP:
odbc_exec("execute my_trigger_before '$procedure_name' #parameters;
execute $procedure_name #parameters;
execute my_trigger_after '$procedure_name' #parameters;")

Related

Working with Events in MySQL

I have a stored procedure which basically selects data from one table and insert into another table. Basically I am doing data archiving manually. Now, I want to write an event just like discussed here
However, after reading that post and researching online, I came to know that it's not possible to create an event inside a stored procedure. Is there a way to accomplish my goal in MySQLWorkbench?
I believe you are thinking this in the oposite direction: You can't create an event in a stored procedure, but you can create a stored procedure and call it from an event.
Example:
delimiter $$
create procedure myProc()
-- Dummy procedure to move the data from mainTable to backupTable,
-- and then clear (truncate) mainTable
begin
insert into backupTable select * from mainTable;
truncate mainTable;
end $$
delimiter ;
-- Now, suposing that you want to execute this procedure every hour:
delimiter $$
create event myEvent
on schedule every 1 hour
do
begin
call myProc();
end $$
delimiter ;
You can write this as any other query in the workbench, or directly in the command line client.
About your concern
After reading your comment, I believe you are a bit confused about what MySQL Workbench is.
MySQL Workbench is only a graphical application that allows you to connect to a MySQL server and perform queries and administration tasks. But Workbench is not the core of MySQL... it is only a viewer (with steroids, maybe, but a viewer after all).
Now, the event scheduler does not reside in Workbench, but in the MySQL server instance you are connecting to. Just as the tables, views, procedures and functions are not stored in the Workbench interface but in the server, the events are also stored in the server.
(Yes, I believe it is a relevant SNAFU that the scheduled events don't show anywhere in the graphical interface, but... after a while, one learns to live with that kind of frustrations and to move on with life)
Maybe your only concern is: "Hey, and what if I want to know what events are set to run in the event scheduler?" You can execute a "show events" query to show a list of the events in the current database, and you can execute "show create event yourEvent" to show the create event syntax for that event.
I insist: Read the manual, and keep a copy at hand (download the manual for your MySQL version here).

Calling stored procedure after user inserts data to a table

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

MySQL Create Trigger Keywords

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 :)

How to pass temporary data from trigger to stored procedure in SQL Server 2008

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.

Stored Procedures vs Triggers in MySQL

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.