I cannot seem to track down why MySQL is throwing an error when trying to update a column in a table given the following.
The update query is:
update jobitems set itemprice=itemprice/100;
The error is:
Error Code: 1054. Unknown column 'JobID' in 'where clause'
When I remove the following from the triggers on jobitems, the update works.
DELIMITER $$
CREATE
DEFINER=`root`@`localhost`
TRIGGER `alacarte`.`jobitems_beforeupdate`
BEFORE UPDATE ON `alacarte`.`jobitems`
FOR EACH ROW
Begin
declare customer varchar(45);
Set @customer = (select CustomerID FROM jobs WHERE JobID=old.JobID);
If new.ItemCompleted<=>1
AND (select InvoiceStatus
FROM
(Select InvoiceStatus, CustomerID
FROM invoices
as custinvoices
Where CustomerID=@customer)
as InvoiceStatus
WHERE InvoiceStatus='Open') IS NULL
then
insert into invoices
set
CustomerID=@customer,
InvoiceBillToName=ifnull((select CustomerName FROM customers WHERE CustomerID=@customer),'-'),
InvoiceBillToAddress=etc;
Naturally, the jobs table has a JobID and CustomerID column. Many other triggers and our front end utilize these columns and they work flawlessly.
The following query works and returns "cust-000002" which is correct.
Set @customer = (select CustomerID FROM jobs WHERE JobID='ALC-20121119-001');
select @customer;
Also the following query works (ItemCompleted=0 for this jobitem, so it is not triggering the above trigger):
update jobitems set itemprice=itemprice/100 where JobID='ALC-20121119-001';
So, I am officially stuck with the above info. Please help :).
Update:
Eliminating the variable and replacing "@customer" with (select CustomerID FROM jobs WHERE JobID=old.JobID) throughout the trigger give the same error, as was expected.
Also, by modifying the trigger, the error goes away. I used the variable in simply a new way and it works. The following adds the output of @customer to each job into a test column and works perfectly. Err!
declare customer varchar(45);
Set @customer = (select CustomerID FROM jobs WHERE JobID=old.JobID);
If new.ItemCompleted<=>1
then
update jobs set testcol=@customer where JobID=old.JobID;
end if;
Update:
Just to make sure I am not crazy, I copy and pasted the trigger back and I get the exact same error message as before so this problem is reproducable. Also, just to be thorough, entries with "ItemCompleted=1" cannot be updated without error and entries with "ItemCompleted=0" can be updated without error found from trying to edit entries via our front end one by one. I figured I'd just make sure because I am thouroughly confused right now.