Error message:
SQL (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 'END' at line 15
DELIMITER //
CREATE TRIGGER fill_address AFTER UPDATE ON orders
FOR EACH ROW
BEGIN
UPDATE orders
SET address = CONCAT_WS(
' ',
payment_method,
payment_address_1,
payment_address_2,
payment_city,
payment_postcode,
payment_country,
payment_zone)
WHERE id IN (SELECT id FROM INSERTED)
END //
DELIMITER
When you only run a single statement you don't need to do anything special. You send the code to the server and you're done:
SELECT COUNT(*) AS customer_count FROM customer
However, if you want to send more than one query at a time you need to tell MySQL where each statement ends. Otherwise, the SQL parser will treat everything as one query and will abort parsing wherever the query stops making sense:
SELECT COUNT(*) AS customer_count FROM customer
SELECT COUNT(*) AS order_count FROM order
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 'SELECT COUNT(*) AS order_count FROM order' at line 2
In almost all MySQL clients out there, default delimiter is semicolon:
SELECT COUNT(*) AS customer_count FROM customer;
SELECT COUNT(*) AS order_count FROM order;
Triggers and other pieces of user defined routines are a special case because you need to distinguish between two situations:
Element creation
Element execution
When you create the trigger, everything you send is a single statement: all the CREATE TRIGGER code. When the trigger executes, each statement inside the trigger body is an independent query. How can we distinguish both situations with the same code? The trick is to use the standard ; separator in the trigger body (where we don't have control about it) but temporarily change the delimiter when executing the CREATE TRIGGER code (where we can use the DELIMITER command offered by most clients).
The first version of your code was missing all separators. Current version is missing trigger body separators.
Related
I'm trying to create a trigger whereby an insertion on one table updates another. This is my SQL Query:
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END;
No matter what I do I get the following 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 '' at line 6
I don't think it's related to the SUM, because trying a basic = 1 on the SET command gives me the exact error. There is no '' at line 6 which is very confusing?
If you're entering this query directly into MySQL you will need to change the delimiter prior to the query using (e.g.) DELIMITER //, otherwise it thinks the query ends at the ; at the end of your UPDATE statement. MySQL then sees an END with nothing before it and complains about the nothing (''). So try this:
DELIMITER //
CREATE TRIGGER makePayment AFTER INSERT ON Payments FOR EACH ROW
BEGIN
UPDATE Invoice
SET InvoiceClientPaid = SUM(InvoiceClientPaid + NEW.PaymentAmt)
WHERE InvoiceID = NEW.PaymentInvoiceID;
END; //
DELIMITER ;
I would like to create a trigger to this example table when I insert, update or delete any row into the table. I have never worked with triggers before so this will be my first one.
I have this code so far:
DROP TRIGGER auditemployees
CREATE TRIGGER auditemployees AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employees_trigger select * from employees where emp_no = NEW.emp_no;
END;
The problem now is:
I would like to copy the affected row into another table in this case employees_trigger, but in order to copy the whole row and to pass the values all I can think of is using variables for each field which in my opinion sounds inefficient since if I had 50 fields I will have to use 50 variables and then insert all those values into the new table.
Is there any better and efficient way to do this?
Also I get the error:
> ERROR 1064 (42000): 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 'CREATE TRIGGER auditemployees AFTER INSERT ON
> employees
Every statement needs to be terminated -- there is no ; at the end of the DROP statement.
You are missing a DELIMITER statement, and you are not using a different delimiter for the CREATE.
What's wrong with OLD.* for the 50 variables?
I have a Chef recipe for creating Unix user IDs and deploying them across multiple nodes, to guarantee uniqueness and prevent devs from having to track them themselves. They merely name the application and it is granted a unique ID if an ID for that application doesn't already exist. If one does, it is simply returned to the script and user accounts are created on the webservers with the appropriate value.
I have a mysql database with a single table, called application_id_table which has two columns, id and application_name. id is autoincrementing and application name cannot be null (and must be unique).
Removing the Ruby from my script and making a couple of substitutions, my sql looks like this:
INSERT INTO application_id_table(application_name) VALUES('andy_test')
WHERE NOT EXISTS (select 1 from application_id_table WHERE
application_name = 'andy_test');
when run, I receive the syntax parsing error:
ERROR 1064 (42000): 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 'WHERE NOT EXISTS (select 1 from
application_id_table WHERE application_name = 'a'
I recall seeing that the values statement does not allow a where clause but I don't wish to use a select statement to populate the values as I'm populating them from variables supplied from within Ruby/Chef. Anyone have an idea how to accomplish this?
You want to use insert . . . select:
INSERT INTO application_id_table(application_name)
SELECT aname
FROM (SELECT 'andy_test' as aname) t
WHERE NOT EXISTS (select 1 from application_id_table ait WHERE ait.application_name = t.aname);
You should be able to plug your variable directly into the select statement, the same you would would with the VALUES statement.
Try this:
INSERT INTO application_id_table(application_name)
select 'andy_test'
WHERE NOT EXISTS (select 1 from application_id_table WHERE application_name = 'andy_test');
I'm fairly new to using triggers and have a tiny question.
I have a trigger finds a match between a newly inserted enquiry and a customer table.
INSERT INTO customersmatched (customerID,enquiryID) SELECT id, NEW.id FROM customer AS c WHERE c.customerName=NEW.companyName HAVING COUNT(id)=1;
I then need to update the newly inserted enquiry so it has a status which shows it's matched (but only if it has matched). So I tried adding this line after the insert.
UPDATE enquiry SET status="Live-Enquiry" WHERE id IN ( SELECT enquiryID FROM customersmatched WHERE enquiryID = NEW.id);
Except I get this error:
MySQL said: #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 enquiry SET status="Live-Enquiry" WHERE id
IN ( SELECT enquiryID FROM cus' at line 5
How do I allow multiple queries within a trigger. I've tried doing something like in this link: Multiple insert/update statements inside trigger?
But doesn't work either. I'm using phpmyadmin btw. Can anyone help? :D
If you have ansi quotes enabled then you can't use double quotes as a string literal, and need to use single quotes instead. see: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_ansi_quotes Otherwise, I don't see any syntax errors that jump out at me.
Try changing SET status="Live-Enquiry" to SET status='Live-Enquiry'
EDIT:
What is the purpose of the first query? I'm not sure you need the HAVING in that query. If want a distinct list of matches, just use DISTINCT
INSERT INTO customersmatched (customerID,enquiryID)
SELECT DISTINCT id, NEW.id
FROM customer AS c
WHERE c.customerName=NEW.companyName;
The second query, if I understand it correctly, can be simplified to this:
UPDATE enquiry
SET status='Live-Enquiry'
WHERE id = NEW.id;
I've recently started working on MySQL n right now i want to create a trigger but MySQL is returning an error in my syntax.
delimiter $$;
create trigger abc after insert on ratings
for each row
begin
set #n1 = select avg(rating) from ratings join users where ratings.uname=users.uname
and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic';
update books set avgcriticrating = #n1 where bookid=new.bookid;
end;
The select statement runs perfectly when fired on its own but gives an error when i use it inside of the trigger.
Here's the error that MySQL gives
#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 'select avg(rating) from ratings join users where ratings.uname=users.uname an' at line 4
Both the books and ratings table contain a field called bookid.
please help
If you want a single value from a select statement, the statement has to be in (brackets).
set #n1 = (select avg(rating) from ratings join users where ratings.uname=users.uname
and ratings.bookid=new.bookid users.`type`='admin' or users.`type`='critic');
The next error will occur at new.bookid users - there might be an and or an or missing.