Safe update error with stored procedures mysql - mysql

I'm trying to update a table with this code:
CREATE DEFINER=`root`#`localhost` PROCEDURE `refresh_stock`(id_product int)
BEGIN
update stock s inner join replacement r on r.product = s.product
set s.quantity = if (s.quantity = 0, r.quantity, s.quantity+r.quantity), s.refresh_date = now()
where s.product = id_product and r.product=id_product and r.date_replacement > s.refresh_date;
END
Basically, I have to update a number of items in a stock every time.I buy new products. The thing is, this procedure worked two times, then it stopped working and it's giving me this error: Error Code: 1175. You are using safe update mode and you tried to update a table without a WHERE that uses a KEY column To disable safe mode, toggle the option in Preferences -> SQL Editor and reconnect.
I tried to pass in the parameter the stock id, but it gives me the same error above. How can I solve this problem WITHOUT disable safe update?

Related

Mysql Update on Update trigger

I have a Denormalized database where I have the total points of other table into the driver table. I want to update the total points of the table driver when I update the table where the points are.
Something like this:
CREATE TRIGGER sanciones_trigger BEFORE UPDATE ON points
FOR EACH ROW
UPDATE drivers,
( SELECT pID,SUM(numpoints) AS total_points
FROM drivers
INNER JOIN points ON points.driverID = drivers.pID
GROUP BY drivers.pID
) sum
SET drivers.total_points= sum.total_points
WHERE drivers.pID = sum.pID;
But I cant Update inside the trigger. I also tried with a procedure, but I'm don't really know how to do it.
How can I resolve this?
If the 'numpoints' is available on 'points' table you can just add the delta into 'drivers' on each update through a trigger. However this won't work if you are changing the 'driverID' as well. Please try the following.
here 'NEW' keyword gives you the new value that you are changing to and 'OLD' keyword gives you the value that exists on the table.
CREATE TRIGGER sanciones_trigger BEFORE UPDATE ON points
FOR EACH ROW
UPDATE drivers
SET total_points= drivers.total_points + (NEW.numpoints - OLD.numpoints)
WHERE pID = NEW.driverID;

Moving MySQL trigger to DB2; issue with keyword NEW

I have this trigger working in MySQL:
CREATE TRIGGER Recharge_trigger
AFTER INSERT ON Recharges
FOR EACH ROW
UPDATE Balances
SET Balance = Balance + NEW.Amount
Where Uid = NEW.Uid AND Stid = NEW.Stid;
It automatically updates the balance column in the Balances table whenever I insert a row in the Recharges table.
However when I tried to run it in DB2, it's giving this error:
Server Error: Error for batch element #1: "NEW.AMOUNT" is not valid in the context where it is used.. SQLCODE=-206, SQLSTATE=42703, DRIVER=3.66.46
I think it's the keyword NEW that's messing it up. Is there any way I can write this in DB2?
I don't have access to any DB2 database so I couldn't test it but looking at the documentation I think you need to include a REFERENCING clause. Try this instead:
CREATE TRIGGER Recharge_trigger
AFTER INSERT ON Recharges
REFERENCING NEW AS N
FOR EACH ROW MODE DB2SQL
BEGIN ATOMIC
UPDATE Balances SET Balance = Balance + N.Amount Where Uid = N.Uid AND Stid = N.Stid;
END

Query not getting executed if supplied a nested sub query

For the below Query its giving the following error
Error Code: 1175. You are using safe update mode and you tried to update a table
without a WHERE that uses a KEY column
To disable safe mode, toggle the option in Preferences -> SQL Editor -> Query Editor
and reconnect.
Can anybody correct me where I have got my query wrong.
UPDATE delivery p SET p.OrderID =
(
SELECT OrderID from OrderTable o where o.DiagramID=p.DiagramID AND o.DeliveryDate=
(
Select min(o2.DeliveryDate) from OrderTable o2 where o2.DiagramId=o.DiagramID
)
)
where p.OrderID=0;
The problem is, you are working in MySQL safe mode, which disallows you to run any updates without using KEY in where clause. To disable it, use
SET SQL_SAFE_UPDATES=0;
just before running a query

MySQL Workbench error 1305 PROCEDURE doesn't exist

I'm trying to execute a statment in MySql to update a column in a table when the expiry date for one of the other columns surpasses the current date, this is then compared against something to make sure that there are no active people for it
but i keep getting this error , i can not see anything wrong with my syntax so im not sure what it is
Error 1305 PROCEDURE does not exist
UPDATE job j SET archived = 1 WHERE(SELECT count(*) FROM job_applied_candidates jac WHERE jac.jobID = j.id) = 0 AND enddate < now();
Add a space between WHERE an (Select...
Also check triggers on jobs table, since they could use a procedure that does not exist.

sql trigger not working/updating table

I had this trigger working fine on another sql server/database.. Now it does nothing, any prerequisites I need to setup or ideas why this isn't working.. SQL Server 2008
create trigger Autoupdate6 -- Creating Trigger
On NumericSamples
For Insert
As
Insert Into BM1OILT
Select SampleDateTime, SampleValue From NumericSamples
Where TagID = 8 and UpdateC = 0
UPDATE NumericSamples set UpdateC = 1 WHERE TagID = 8
go
Thanks,
Figured it out, the program that was inserting into the table was doing bulk inserts, there for by passing the triggers.. Went with a job instead of triggers. Thanks again Aaron Bertrand for trying to figure this out with me.
This will prevent the trigger from attempting to operate on the entire table:
CREATE TRIGGER dbo.Autoupdate6
ON dbo.NumericSamples
FOR INSERT
AS
BEGIN
INSERT INTO dbo.BM1OILT(...column names here please...)
SELECT SampleDateTime, SampleValue FROM inserted
WHERE TagID = 8 AND UpdateC = 0;
UPDATE n SET UpdateC = 1
FROM dbo.NumericSamples AS n
INNER JOIN inserted AS i
ON n.SampleDateTime = i.SampleDateTime
WHERE n.UpdateC = 0 AND n.TagID = 8;
END
GO
You might be able to collapse this to one statement using the OUTPUT clause, but there are so many limitations on composable DML, it's usually just wasted effort unless we already know that your environment is not subject to any of the restrictions.