I couldn't find anything on the site specifically addressing this issue,so I decided to post a question for some help.
I'm trying to run a "BEFORE UPDATE" trigger on a table in my database. The trigger is set to execute after a cron job query runs to evaluate user data. If the data targets are met ,then BEFORE UPDATE trigger kicks in to manipulate additional columns via two simple math formulas.
sqlQuery (via cron job)
UPDATE table1.records SET date_created=IF(expiration_date<(CURDATE()),IF(postCOUNT>0,CURDATE(),'error'),date_created)
BEFORE UPDATE TRIGGER (created via phpMyAdmin cPanel)
1. BEGIN
2. IF NEW.date_created != OLD.date_created THEN
3. SET NEW.date_created=New.date_created;
4. SET NEW.expiration_date = NEW.date_created + 2;
5. SET NEW.postCOUNT = OLD.postCOUNT-1;
6. END IF;
7. END
Now I'm sure there are far better ways of coding to accomplish this task but everything seems to working until the math formula in LINE 5 of the trigger. Instead of subtracting "1" from OLD.postCOUNT, the formula keeps subtracting "2". I'm baffled as to why this is happening,the formula seems straight-forward. I've even tried other numbers in the formula and still the answers are never correct.
Any advice or insight would be greatly appreciated.
Thanks in advance!
Related
I am trying to make an interface for my insert Query, this moves a huge number of data.
DataModule2.FDQueryInsertExceed.Close;
DataModule2.FDQueryInsertExceed.Sql.Clear;
DataModule2.FDQueryInsertExceed.Sql.Add('INSERT IGNORE INTO tblLogs');
DataModule2.FDQueryInsertExceed.Sql.Add('SELECT * FROM tbllogs WHERE Datetimelog <
date_sub(NOW(), INTERVAL 1 month);');
DataModule2.FDQueryInsertExceed.ExecSQL;
Dont know where to put the progress bar, and also if you can post here a link with full manual of FD Components.
FireDAC includes the TFDEventAlerter, which allows applications to receive certain notifications
from the SQL backend, including some progress notifications. Unfortunately, it does not support
MySQL so you are going to have to devise a way of updating your ProgressBar yourself.
The main problem with doing it yourself is that after you call DataModule2.FDQueryInsertExceed.ExecSQL,
it will not return until ExecSQL has completed execution, so you can't get any opportunity
to update your ProgressBar until then and by the time that you do, the SQL backend has finished
working.
So, realistically, the best you can do is to snip your db inserted up into a series of batches and
update the ProgressBar after each one. Before you start the batches, you can do a SELECT COUNT(*)
which should tell you at least approximately how many rows will be involved overall, so that
you can calculate the percentage complete after each batch. I say "approximately"
in case new inserts are required while your ExecSQL is executing,
The simplest place to start is by determining how many of your inserts can be executed in
a few hundred milliseconds, parameterize your SQL to match that and use a TTimer to start
each batch. So your TTimers's OnTimeer wou look something like
TForm1.OnTimer1.OnTimer(Sender : TObject);
var
S : String;
begin
S := 'INSERT IGNORE INTO tblLogs SELECT * FROM tbllogs WHERE Datetimelog ... //
// complete as necessary to add a starting DateTime and an ending one for the batch
DataModule2.FDQueryInsertExceed.Sql.Text := S;
DataModule2.FDQueryInsertExceed.ExecSQL;
// Update ProgressBar here
end;
An enhancement (but a step change in difficulty) would be to call DataModule2.FDQueryInsertExceed.ExecSQL
in a background thread and, once it has executed, execute code in the main, VCL thread, not in the background thread, to update
the ProgressBar. However, exactly how to do that is way beyond the scope of this answer.
I have the following trigger the gist is I only want it to trigger when it's already a problem and when any of the expected values are not found.
Currently this is not working. Any suggestions?
({TRIGGER.VALUE}=1 and {Template_App_JAMS_Agent:app.jams.agent.state.regexp(Online|Connected|Connecting|Idle|Throttled)}=0)
edit - I know it's not working as currently I have a value of Timeout to one of those keys for hours and it's not triggering. The regex does not seem to be working.
I saw post with similar question but seems it could not answer mine, I apologize upfront if this is a duplicated post, as I think my problem is a bit different from what was being asked in another previous post.
I have created a continuous form where there are 2 textbox, which will do calculation with each other when either one of them are being changed.
One is a "Mark-up" textbox with code of Me.Mark_Up = (Me.Unit_Selling_Price / Me.Cost_Unit_Price) - 1
Another textbox is a "Unit Selling Price" textbox with code of Me.Unit_Selling_Price = (Me.EUR_Cost_Unit_Price) * (1 + Me.Mark_Up). Each code was being put under as the After Update event, triggering the calculation if the other textbox has been changed.
Then I added a "Round-up" button which will execute the update query which then will roundup the number in the "Unit Selling Price" textbox. I tried 2 ways to update the "Mark-up" textbox but neither way works:
Insert code into update query to update the mark-up textbox as well but turns out all calculation done are inaccurate (examples as below):
Insert code of Me.Mark_Up = (Me.Unit_Selling_Price / Me.Cost_Unit_Price) - 1 after pressing the "Round-up" button and executing the update query, but "Mark-up" textbox did not update accordingly.
How should I code this?
Keep in mind that when you use VBA code to update a control (or a query), then the event stubs (such as after update) do NOT fire.
So, if you going to use code to update the data?
You need to code in the use of the after update code.
There are several ways to do this, but one tip would be to make sure that the after update code is a "stand alone" type of code routine. That way, you can use the SAME code in one place for both operations. (you really don't want to have to maintain two copies of that calculation code - since it THEN becomes rather easy to have those two routines use differnt rounding or slightly different code - thus resulting in DIFFERENT results for what should be the same thing.
So for the several text boxes, and the after update code? You could say set things up this way:
Me.Mark_Up = MyMarkUpCalc(Me.Unit_Selling_Price, Me.Cost_Unit_Price)
And then in the form have this:
Public Function MymarkUpCalc(cSell as currency, cCost as currency) as currency
MymarkUpCalc = (cSell / cCost) - 1
End Function
So now you have a COMMON routine.
Now, in your code that does the "round"
I would suggest this code:
Sub MyRound
if me.dirty = true then me.dirty = false 'save any pending writes
dim rst as DAO.ReocrdSet
set rst = me.RecordSetClone
rst.moveFirst
do While rst.EOF = false
rst.Edit
rst!SomeRoundField = some new value
rst!Unit_Selling_Price = MyMarkUpCalc(rst!Unit_Selling_Price, rst!Unit_CostPrice
rst.Update
rst.MoveNext
loop
So, in place of say a update query (sql), I think a loop + code will work somewhat better, and thus you now have a common set of calculation routines. Even if the routine is wrong, it will be constant wrong!
So now both your after update events, and your round up processing routine(s) will both use the SAME calculation code.
It is hard to tell your issue, as we don't have your secret update code.
Your results seem a bit off, so something is wrong. The correct results (last column) should be:
c c * (1 + m) -Int(-c * (1 + m))
296.53 505.049896 506
2225.69 3790.795208 3791
7074.41 12049.135112 12050
4659.79 7936.554328 7937
For extended rounding methods, study my project VBA.Round.
I want to create a trigger that does the following:
copy one column of info jobnumber from one table (jobs) to another (materials) on an existing record to attachedjobnumber.
I haven't found the correct syntax to say this. when I insert a new job - nothing gets update and no new row is inserted,,, but there are no error messages in the logs.
I also need to set the bool (hasjobnumber) equal to true - when I test that trigger - it works fine.
which makes me think that setting the value of material.attachedjobnumber = jobs.jobnumber is the problem, my guess is that jobs.jobnumber isn't in reference when updating table material...
if that's true - what's the proper syntax for this?
I've tested separate triggers, and so far this trigger works fine.
UPDATE material
SET isjobyet = "HAS"
WHERE barcode1 IN (
SELECT primaryRFID
FROM jobs
WHERE jobs.primaryRFID = material.barcode1
)
since this code does work - I make the assumption that the non-static JobNumber value is the source of the problem. since "HAS" is correctly updated.
UPDATE material
SET material.AttachedJobNumber = jobs.JobNumber
WHERE barcode1 IN (
SELECT primaryRFID
FROM jobs
WHERE jobs.primaryRFID = material.barcode1
)
from this - I expect that after each inserts on the table jobs:
jobs.JobNumber be assigned to the material.AttachedJobName
this updates only the material row where the material.barcode1 =jobs.primaryrfid.
but no new row is inserted at all.
Before you perform UPDATE,
Actually you can use the same script using SELECT [skip the UPDATE SYNTAX]
That way you can monitor your script without committing anything yet.
And also I dont recommend using this inside the subquery
WHERE jobs.primaryRFID = material.barcode1
This condition connecting a table works on IN-SELECT subquery.
If you are performing subqueries inside the [WHERE] clause, try to treat it as different buffer, dont connect them first.
I'm getting a strange error while trying to use a MySQL trigger.
I'm using XAMPP and creating the trigger using PhpMyAdmin.
The trigger's code is:
BEGIN
DECLARE stud INT(11) DEFAULT 0;
DECLARE sw CURSOR FOR
(SELECT CodiceStudente FROM Listastudenticorsi WHERE CodiceCorso = NEW.CodiceCorso);
OPEN sw;
get_loop: LOOP
FETCH sw INTO stud;
INSERT INTO inbox(Mittente, Destinatario, Oggetto, Contenuto, Data) VALUES (NEW.CodiceDocente, stud, "Nuova news inserita", NEW.Oggetto, NEW.Data);
END LOOP get_loop;
END
And is called BEFORE INSERT into the table 'News'.
What happens is that the syntax is correct, but when I try to run it triggering the event it says "#1329 - No data - zero rows fetched, selected, or processed".
I tried to find out what the real problem is, and it seems to be the line "FETCH sw INTO stud"; I tried many times and the SELECT statement DOES return the correct values, so 'sw' can't be empty... I'm stuck at this point.
There are 3 tables interested by this trigger. 'News' is the one that triggers the event; it has some columns that are called using the keyword "NEW". The second one is Inbox; it is the table in which I'll insert some values after the trigger has performed its actions. Finally, there's "Listastudenticorsi", which means approximately "list of students and courses".
What I do is: when a News is inserted, I get the course it refers to, its object, its date and the submitter of the news, I find (using the select statement) the students who attend the course that the News is referring to, and then send a mail to each of them using the insert statement.
You have no continue handler for the cursor, as I see it. It would allow that cursor to actually do something.
From the mysql Cursor manual page, see this.
Here too is a link to a stored proc I wrote showing a continue handler with a flag specifying done for the loop.