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
Related
I have execute only access to a stored procedure.
This SP seems to select some data from multiple tables, and returns one row. I need to store two columns of the output of this SP into a table.
Is there any way to do this within MySQL?
If it returns a row, this is a stored function and not a stored procedure. You can use something like the following to insert into your table:
INSERT INTO tablename SELECT (SELECT col1, col2 FROM (SELECT somefunction()))
Otherwise, it will be a stored procedure and you should do something like this, assuming that #var1 and #var2 are output parameters:
CALL someprocedure(#var1, #var2, #var3)
INSERT INTO tablename SELECT(#var1, #var2)
See the documentation about Create Procedure and Create Function for more information about functions versus procedures.
MySQL has an extension to stored procedures that allows the procedure to return one or more result sets to the client, as if the client had issued a SELECT query... but those results are ephemeral. They don't persist and they can't be stored in variables or otherwise accessed after the procedure finishes -- they can only be "fetched" the one time.
There is a way to make them accessible without breaking the way the procedure already works, as I discussed here, but you can't do it without a change to the procedure:
How to use Table output from stored MYSQL Procedure
The idea is for the procedure to write its output in a temporary table, and then return it to the caller by calling SELECT against the temporary table -- but to leave the temporary table behind so that the caller can access it directly if desired.
That's not exactly the same as what you're asking though, which is why I didn't mark this question as a duplicate, since you, unlike the other poster, do not appear to have administrative control of the procedure... but unless you can make the case for a change like this, there's not another way within MySQL to access those returned values, since they only exist in the result-set that's returned.
Of course, procedures do have optional OUT parameters, where you can hand variables to the procedure as part of arguments you use to call it, and it can set those variables, so that they'll have the values you need when the procedure is done, but that only works when the return values are scalars and would require a change to the procedure's interface, since procs in MySQL do not have "optional" arguments... if the procedure were changed to permit this, it would require an increased number of arguments to be provided every time it was called, and if other components are calling it, that could easily break other things.
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;")
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.
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).