mysql error, right syntax to use near '' - mysql

I have a problem with this SQL code. I want to create a trigger, here it is.
CREATE TRIGGER `blog_after_insert` AFTER UPDATE ON `agent`
FOR EACH ROW
BEGIN
DECLARE costvalue INTEGER default 0;
IF (OLD.PacketStatusType_Id!=NEW.PacketStatusType_Id) THEN
IF (OLD.Packet_Cost IS NULL) THEN
SET costvalue = OLD.Packet_Cost;
ELSE
SET costvalue = OLD.Packet_SMSCount;
END IF;
IF (costvalue IS NOT NULL) THEN
CALL tcksms_packet_update_proc(OLD.Transaction_Id,OLD.PacketStatusType_Id,costvalue,-1);
ELSE
CALL tcksms_packet_update_proc(OLD.Transaction_Id,OLD.PacketStatusType_Id,0,-1);
END IF;
DECLARE COST_VALUE_NEW INT DEFAULT 0;
IF (NEW.Packet_Cost IS NULL) THEN
SET COST_VALUE_NEW = NEW.Packet_Cost;
ELSE
SET COST_VALUE_NEW = NEW.Packet_SMSCount;
END IF;
IF (COST_VALUE_NEW IS NOT NULL) THEN
CALL tcksms_packet_update_proc(NEW.Transaction_Id,NEW.PacketStatusType_Id,COST_VALUE_NEW,1);
ELSE
CALL tcksms_packet_update_proc(NEW.Transaction_Id,NEW.PacketStatusType_Id,0,1);
END IF;
ELSE
END IF;
END IF;
END
But i get this;
Error
SQL query:
CREATE TRIGGER `blog_after_insert` AFTER UPDATE ON `agent` FOR EACH ROW BEGIN DECLARE costvalue INTEGER default 0;
MySQL said: Documentation > #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 5
That line is
I tried deleting spaces, made combinations of apostrophes (' and `), deleted and rewrited some lines manually but can't make it work, what is wrong with this code?

You need to use the delimiter command before and after the create trigger command, otherwise the semicolons terminating the commands within the trigger body will confuse mysql. See mysql's documentation on defining stored programs, which also has examples how to use the delimiter command.

Related

wrong syntax to use near 'declare exists_check int'

I'm writing an script to update a db schema, the script is the following:
delimiter //
begin
set #check_exists = 0;
select count(*)
into #check_exists
from information_schema.columns
where table_schema = 'my_app'
and table_name = 'users'
and column_name = 'points';
if (#check_exists = 0) then
alter table my_app.users
add points int not null default 0
end if;
end //
delimiter
when I run it, i get the below error:
Reason:
SQL Error [1064] [42000]: (conn=34) You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for the right syntax
to use near 'set #check_exists = 0;
I have already checked the answers the below two posts but none solves my problem.
this question where the solution was to change the delimiter, and to
this question where the solution was to remove the DECLARE keyword and just declare the variable as it is in the MariaDB manual here with set #var int;
I think you need to have a CREATE PROCEDURE before your BEGIN statement. So:
delimiter //
CREATE PROCEDURE UPDATE_SCHEMA()
begin
set #check_exists = 0;
(...your other script here...)
end//
delimiter ;
call UPDATE_SCHEMA();
An additional note: check_exists in your if statement needs a #
According to the BEGIN END documentation here, the syntax is:
BEGIN [NOT ATOMIC]
[statement_list]
END [end_label]
And here is what I did not know:
NOT ATOMIC is required when used outside of a stored procedure. Inside stored procedures or within an anonymous block, BEGIN alone starts a new anonymous block.
As I'm not using the BEGIN END in a stored procedure, I have to add the NOT ATOMIC after the BEGIN keyword.
So the code should look like this:
delimiter //
begin not atomic
set #check_exists = 0;
-- rest of the code here...

MYSQL is not supporting a while loop containing more than one statement

I am trying to install a procedure in Mysql 5.6 launching the command from mysql workbench (version 6).
I get a sintax error with no apparent reason.
If I just remove one of the two assigments inside the while loop, then it would works.
How come i cannot put two statements inside the while?
DELIMITER //
CREATE PROCEDURE PROC (IN TABLE_ varchar(400))
BEGIN
DECLARE Statement varchar(400) DEFAULT "";
DECLARE i INTEGER DEFAULT 1;
DECLARE N INTEGER DEFAULT 2;
while i <= N do
set Statement = 'a';
set i = i+1;
end while;
END
//
DELIMITER ;
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 '= 'a'; set i = i+1; end while; END' at line
10

MySQL Trigger usage : if column is null set other column null

How do I realize the following in MySQL with Triggers:
When value of some column is null -> set other column values to null
and
when value of some column is not null -> set other column values to null
table definition:
CREATE TABLE variations (
id int(10) NOT NULL,
x1 int(10) NOT NULL,
x2 int(10),
x1_option1 BOOL,
x2_option1 BOOL,
x1_option2 varchar(10),
x2_option2 varchar(10)
);
The idea is that we have 2 Elements, x1and x2. While x1is mandatory, x2is optional and can be null. Both, x1 and x2 have two options: x1_option1 , x2_option1, x1_option2 and x2_option2.
The first rule should be that when x2 is null, both options for x2 (x2_option1, x2_option2) must also be null.
My attempt:
CREATE
TRIGGER check_null_x2 BEFORE INSERT
ON variations
FOR EACH ROW BEGIN
IF NEW.x2 IS NULL THEN
SET NEW.x2_option1 = NULL;
SET NEW.x2_option2 = NULL;
END IF;
END$$
Throws 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 6
Can you please help me figuring out whats wrong? I just dont understand what '' means.
The second rule should be that there can only be one of the two options selected. that means if x2_option1 is NOT NULL, x2_options2 must be NULL. In general i think this can be done the same way as the first rule. My question: how can i do multiple 'IF', 'ELSE IF' etc in one trigger?
This is syntax for trigger:
delimiter //
CREATE TRIGGER upd_check BEFORE UPDATE ON account
FOR EACH ROW
BEGIN
IF NEW.amount < 0 THEN
SET NEW.amount = 0;
ELSEIF NEW.amount > 100 THEN
SET NEW.amount = 100;
END IF;
END;//
delimiter ;
...and your code is here:
DELIMITER //
CREATE TRIGGER check_null_x2 BEFORE INSERT ON variations
FOR EACH ROW
BEGIN
IF NEW.x2 IS NULL THEN
SET NEW.x2_option1 = NULL;
SET NEW.x2_option2 = NULL;
END IF;
END$$ -- THIS LINE SHOULD BE: "END;//"
DELIMITER ;
EDIT:
The official Documentation says the following:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.
you seem to have ";" set as DELIMETER, which causes the query to execute once it sees a ";". try changing it first:
DELIMITER //
CREATE
TRIGGER check_null_x2 BEFORE INSERT
ON variations
FOR EACH ROW BEGIN
IF NEW.x2 IS NULL THEN
SET NEW.x2_option1 = NULL;
SET NEW.x2_option2 = NULL;
END IF;
END;//
DELIMITER ;
CREATE TRIGGER `XXXXXX` BEFORE INSERT ON `XXXXXXXXXXX` FOR EACH ROW BEGIN
IF ( NEW.aaaaaa IS NULL ) THEN
SET NEW.XXXXXX = NULL;
SET NEW.YYYYYYYYY = NULL;
END IF;
END
Worked for me....

Mysql error 1064 in Mysql 5.1- unable execute successfully

I am trying to write trigger in Mysql (5.1), but getting following error, please help.
The error is:
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 '' at line 5.
Purpose for writing trigger:
I am writing application where I am assigning users, and I want to store unassigned usercount to field cluster_count in IX_branchdetails table.After updating the base table.
trigger:
DELIMITER $$
CREATE TRIGGER upd_trg AFTER
UPDATE ON DBNAME.BASETABLE
FOR EACH ROW
BEGIN
DECLARE m_branchcode INTEGER;
DECLARE cnt INTEGER;
DECLARE cursor_branch CURSOR FOR
SELECT DISTINCT branchcode
FROM ix_branchdetails;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
open cursor_branch;
my_loop: loop
set done = false;
fetch cursor_branch into m_branchcode;
if done then
leave my_loop;
end if;
select count(1) into cnt from (select count(1) from BASETABLE Where IX_BRANCHCODE = m_branchcode) as temp;
update DBANAME.ix_branchdetails set DBANAME.ix_branchdetails.cluster_count = cnt where DBANAME.ix_branchdetails.BRANCHCODE = m_branchcode;
end loop my_loop;
close cursor_branch;
END $$
DELIMITER ;
I don't see a declare for the done variable:
DECLARE done TINYINT DEFAULT FALSE;
The semicolon (;) is the default delimiter for MySQL statements. To get a procedure/function/trigger defined, we normally see the statement delimiter changed to a string that doesn't appear in the statement:
DELIMITER $$
CREATE PROCEDURE ...
END$$
DELIMITER ;
If the delimiter is not changed from the semicolon, then when MySQL encounters the first semicolon in your procedure/function/trigger, it sees that as the end of the statement, which is not what you want. You want MySQL to see the entire block of code as a single statement.

Adding MySQL Trigger: invisible (to me) Syntax Error

NOTE: I initially thought my problem had to do with CONCAT, but since the exact error persisted even when I completely eliminated CONCAT from my query, I figured I should completely over-write of my original post.
I'm trying to set a trigger, and keep getting a Syntax Error.
When I tried this
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF (NEW.aka IS NULL) THEN
SET NEW.aka = 'test value';
END IF;
END
...I got 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 6
And when trying this (without the IF peren's)
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
SET NEW.aka = 'test value';
END IF;
END
...I get the same exact 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 6
It's got to be something simple ... but I'm just not seeing it.
I'm on MySQL 5.1.36.
What am I missing?
Any pointers?
try to change delimiter first:
DELIMITER |
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
SET NEW.aka = 'test value';
END IF;
END|
DELIMITER ;
Maybe MySQL sees the first ";" as the end of CREATE TRIGGER
try this:
CREATE TRIGGER set_aka_name
BEFORE INSERT ON sandbox_person
FOR EACH ROW
BEGIN
IF NEW.aka IS NULL THEN
NEW.aka = CONCAT(NEW.fname, ' ', NEW.lname);
END IF;
END
there is a difference between IF () function and IF statement (see this)