SQL syntax error, - mysql

This small query is driving me crazy. I keep getting errors no matter what I try
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
This is the code
CREATE PROCEDURE deposit (
IN custAccount INT(11),
IN amount INT(11)
)
IF amount < 0 THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Invalid amount';
END IF;
The procedure is incomplete, dont judge it yet :D
Thanks

"The example uses the mysql client delimiter command to change the statement delimiter from ; to // while the procedure is being defined. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself."

Related

How to check SQL State in a if statement?

I'm trying to commit an insert in a trigger based on a sql state based on success of a previous query in a trigger. But I'm getting a syntax error.
Can anyone point out what's wrong I'm doing here.
SET #OLDTMP_SQL_MODE=##SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
DELIMITER //
CREATE TRIGGER `insert_exp_register` AFTER INSERT ON `trnInvDtl` FOR EACH ROW begin
insert into trninvdtl_exportregister(comp_code,
inv_type,
inv_no,
item_srl_no) values(
new.comp_code,
new.inv_type,
new.inv_no,
new.item_srl_no);
if (sqlstate='0000') then
rollback;
end if;
end//
DELIMITER ;
SET SQL_MODE=#OLDTMP_SQL_MODE;
Here's the error message for syntax error.
[Window Title]
xxxx PC: Error
[Content]
SQL Error (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 'sqlstate='0000') then
rollback;
end if;
end' at line 11
[OK]
[Footer]
Find some help on this error

Error 1064 on Mysql trigger

I'm writing a trigger in MySQL to take log of table updates. The log table is called individuo_storico and the target table is called individuo. When individuo is updated, I want to check if IDQualifica and IDLivello are changed, if yes a record in individuo_storico is inserted.
I write down this code but I get a #1064 error, where's the syntax error?
use ore;
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;
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
Please wrap your trigger creation with the monikers necessary so the db engine does not choke on it. So three areas:
1) line 1
2) right after the end;
3) and then a reset to a default delimiter.
Same concept is typical with stored proc creations.
DELIMITER $$
create trigger individuo_update after update on individuo
for each row
begin
if ( NEW.IDLivello <> OLD.IDLivello or NEW.IDQualifica <> OLD.IDQualifica) then
insert into individuo_storico(IDIndividuo, IDQualifica, IDLivello)
values (NEW.IDIndividuo, NEW.IDQualifica, NEW.IDLivello);
end if;
end;$$
DELIMITER ;

How to dissallow updates to a table if a column is a certain value in mysql?

I dont want anyone to be able to update thier Name to certain values and cancel the update if it is attempting to update the PLayername to "Error", and need to prevent it at the database level.
I figured I could simply do this with a trigger, so I tried making one here.
delimiter $$
create trigger errorcheck before insert on player_data
for each row
begin
if new.PlayerName = 'Error' then
signal sqlstate '45000';
end if;
end;$$
but the server responds with this error:
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 7
Thank you for any help / suggestions
delimiter $$
create trigger errorcheck before insert on player_data
for each row
begin
if new.PlayerName = 'Error' then
signal sqlstate '45000';
end if;
end;
I dont understand why but removing the $$ delimeter at the end saved the trigger and i tested it and it works how i wanted it to.

MYSQL syntax delimiter and syntax error

I have tried to fix the syntax errors in the following but I can't see what on earth is wrong here:
DELIMITER =
CREATE TRIGGER trigs BEFORE UPDATE ON autoinc
FOR EACH ROW BEGIN
DECLARE num_rows INTEGER;
SELECT (*) INTO num_rows FROM autoinc;
IF num_rows >=3 THEN
DELETE FROM autoinc LIMIT 1;
END IF;
END=
DELIMITER ;
The errors are:
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 '*) INTO num_rows FROM autoinc; IF num_rows >' at line 4
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 '3 THEN
Can any please help me fix this?
As already mentioned in the comments:
SELECT (*) AS num_rows ...
was probably meant to be
SELECT COUNT(*) AS num_rows ...
And the
IF num_rows >=3 THEN
breaks as you defined = as delimiter.
Use a delimiter that doesn't occur in your code, e.g.:
DELIMITER //
With these two changes things should work without syntax errors

Syntax errors in mysql with procedure, triggers and signal

I feel embarrassed that I have to come and ask for help with this, but as surely many before me have learned, it seems mySQL syntax error messages are about as useful as a pope hat on a grizzly bear. Attached is my first attempt at writing a trigger for a car company database. The table, can_lease, relates the id of an employee and the id of a car model. The trigger is expected to enforce two rules: 1) there can be at most 10 car models associated with 1 employee, and 2) the employee must be of type leasing (there is a column 'leasing' which must equal 'Y').
So the goal is for the trigger to catch violations of this rule and send a signal and a message explaining the violation. I'm simply not sure what the errors are, but I will attach the relevant error messages as well.
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end;
delimiter $$
create trigger can_lease_insert_trigger
after insert on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
create trigger can_lease_update_trigger
after update on can_lease
for each row begin
call can_lease_check(new.emp_id);
end;
$$
And here are the error messages:
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 '' at line 3
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 'if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then' at line 1
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 'end' at line 1
Thank you for your help! I would also appreciate any advice one has for debugging this sort of thing in general. Coming from gcc telling me at least something about why my code is wrong, this is a very foreign process!
EDIT: I realize that I should have moved the delimiter change up to above the procedure as well. I don't get it, but that removes all but one of the errors. Currently, the error is
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 'if (select count(rent_model_id) from can_lease where emp_id = eid) == 10
then s' at line 4
The semicolons (;) between the first begin and end keywords are the culprits. Just enclose your original create block with a DELIMITER, as follows. I use # as the delimiter in my example, and I recognize that you use $$, though there will be no resulting difference.
DELIMITER #
create procedure can_lease_check (eid int)
begin
declare can_lease_too_many_models condition for sqlstate '90001';
if ((select count(rent_model_id) from can_lease where emp_id = eid) >= 10)
then signal sqlstate '90001' set message_text = 'employee can lease at most 10 rent models.';
declare can_lease_not_leaser for sqlstate '90002';
if not (select leasing from employer where employer.emp_id = eid) == 'Y'
then signal sqlstate '90002' set message_text = 'employee must be of type "leasing"';
end#
Also, there will be no difference if you end it like I did (end#), or like you did, with the semicolon after the end keyword:
end;
#