MySQL Workbench error 1305 PROCEDURE doesn't exist - mysql

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.

Related

Ambiguous error in creating BEFORE INSERT trigger

I have a Reservation table and am creating a BEFORE INSERT trigger that checks whether a user's reservation is already in the database, i.e. prevent him from making duplicate reservations. This is done to signal the front-end that the user is unable to make a duplicate reservation.
However, mySQL raises this error: ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5. For some reason when the body between BEGIN and END is empty, it doesn't raise an error.
CREATE TABLE reservation (
customer_user_id integer,
date integer,
time integer,
name varchar(100)
);
CREATE TRIGGER trigger_name
BEFORE INSERT -- error raised in this line
ON reservation FOR EACH ROW
BEGIN
DECLARE sum INT;
SELECT COUNT(*)
INTO sum
FROM reservation r
WHERE (r.customer_user_id = new.customer_user_id)
AND (r.date = new.date)
AND (r.time = new.time);
IF sum = 0 THEN
INSERT INTO reservation values (new.customer_user_id, new.date, new.time, new.name);
ELSE
RAISE NOTICE 'duplicate'
END IF;
END;
Additionally, could this trigger be a constraint on the table Reservation? E.g. (customer_user_id, date, time) UNIQUE of sorts, which would prevent adding of duplicates, but I am clueless on how to "signal" the front-end which in turn prompts the user that they cannot add a duplicate reservation.
Sorry if some parts of the code might be weird, I'm new to SQL and was taught pSQL.
Thanks in advance!

MySql update query not working-generic error

I get a generic error message, and have no idea what the problem is with the query. What do I do to fix this?
Query explanation: there are two tables, Invoice, and temp. I need to take zip codes from temp table and push them to the Invoice table, based on the invoice number.
START TRANSACTION
UPDATE
Invoice
SET
Invoice.zip_code = (SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number)
WHERE
Invoice.invoice_date >= '2017-08-01'
ROLLBACK
this is the error:
Error Code: 1064 You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'UPDATE Invoice SET Invoice.zip_code = (SELECT
zip_code FROM temp WHERE temp' at line 3
Add semicolon after each command.
START TRANSACTION;
UPDATE
Invoice
SET
Invoice.zip_code = (SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number)
WHERE
Invoice.invoice_date >= '2017-08-01';
ROLLBACK;
Sounds like #Daniel Blais is right. One thing you can do to troubleshoot is to break the query down and run each part individually. That will help you find out what's wrong.
SELECT zip_code FROM temp WHERE temp.invoice_number = Invoice.invoice_number;
One other question: Why are you rolling back after the table is updated? Don't you want to commit?

Safe update error with stored procedures 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?

SQL Server 2008 trigger throwing error when I use procedure GETDATE

I am trying to create a trigger in SQL Server 2008 which inserts a row into a 2nd database after an update in the 1st database.
However I keep getting an error..
(Procedure behaviour_alert, Line 11 Incorrect syntax near ')'
Is this because of using DATETIME in the trigger.
This works as a normal query, I can't see why it won't work as a trigger.
Can only adjust the query to choose the current datetime?
Query below
create trigger behaviour_alert
on [database1].[dbo].[studconduct]
for update
as
begin
declare #conductdatetime as datetime
set #conductdatetime = GETDATE()
insert into database2.dbo.behaviouralert
select *
from studconduct
where curr_ind='Y'
and cond_pts >= '5'
and conduct_date >= #conductdatetime
What am I missing here, going crossed eyed looking at this. Maybe I have had too much coffee.
Edit: this is what I ended up with and it worked. I missed the END at the end of the trigger
create trigger behaviour_alert
on [database1].[dbo].[studconduct]
for update
as
begin
insert into database2.dbo.behaviouralert
select *
from studconduct
where curr_ind='Y'
and cond_pts >= '5'
and conduct_date >= datetime
end
I think the problems is not in GETDATE().
maybe you just forget about END in the end of trigger?

Using a MySQL event to "deactivate" accounts

I setup a database and one of the columns in a table is "status" (active,inactive,locked). I want the event to compare NOW() to the value of column "pdate" (which is a timestamp), and if greater than 30 days, update the value of "status" to "inactive".
I wrote the following, but I get a few syntax errors :s
CREATE EVENT `expireAccounts_oldPwd` ON SCHEDULE EVERY DAY
DO
USE databasename;
SELECT pdate FROM tablename WHERE status = "active";
FOR EACH ( ROW IN tablename WHERE( ( SELECT DATEDIFF(NOW(),pdate) AS age ) > 30 ) ) {
UPDATE tablename SET status = "inactive";
};
ERROR 1064 (42000): You have an error in your SQL syntax near 'USE databasename' at line 2
ERROR 1064 (42000): You have an error in your SQL syntax near 'FOR EACH ROW IN "tablename" WHERE( ( SELECT DATEDIFF(NOW(),"pdate") AS age )' at line 4
ERROR 1064 (42000): You have an error in your SQL syntax near '}' at line 6
of course 'databasename' was replaced the actual database's name and 'tablename' the actual table's name.
Now at least it's doing something:
+---------------------+
| pdate |
+---------------------+
| 2011-08-11 18:01:02 |
| 2011-08-11 18:03:31 |
+---------------------+
2 rows in set (0.00 sec)
If I don't included USE databasename; on line 2, I get no output.
FINAL CODE:
USE databasename;
DELIMITER %
CREATE EVENT eventname
ON SCHEDULE EVERY 1 DAY
DO UPDATE tablename SET status = "inactive" WHERE status = "inactive" AND DATEDIFF(NOW(), columnname) > 30);
%
I didn't realize events were database-specific (so you have to be in the database when you create it).
Thanks all!
Two event specific things apart from the obvious syntax problems:
Where does your event end? You need to enclose it in a BEGIN/END block (the same way as a stored procedure).
You need to switch the DELIMITER when defining an event (the same way as when you define a stored procedure).
There are two relevant examples at the end of http://dev.mysql.com/doc/refman/5.1/en/create-event.html.
Update:
Also check http://dev.mysql.com/doc/refman/5.1/en/stored-routines-syntax.html for SQL statements which are permitted in stored procedures and events. USE is not permitted.
One more update:
It would be advisable to first try to get your SQL working without putting it in a event. After you have fixed your statements so that they work, try creating a stored procedure out of it. When you get the stored procedure working, you can replace it with an event. This way it will be much easier to sort out the rest of the problems (such as where is the output of the first SELECT supposed to go? etc.).
Is there a reason you can't use a query like this one?
UPDATE tablename SET status = "inactive" WHERE status = "active" AND DATEDIFF(NOW(),pdate) > 30;
I've never even seen FOR EACH in MySQL...
First step's going to be using valid SQL.
SELECT "pdate" FROM databasename.tablename WHERE "status" = "active";
will always cause an error, because column names shouldn't be in quotes. If you enclose a table name in anything, it'd be backticks (`).
SELECT pdate FROM databasename.tablename WHERE status="active";
You have the same problem in the rest of your query.