MySQL #1064: declare a variable - mysql

I need to declare the variable in MySQL. and here is what I have till now.
CREATE TRIGGER upd
BEFORE UPDATE ON chats FOR EACH ROW
BEGIN
DECLARE my_cursor CURSOR FOR SELECT name FROM messages;
END
logically, it seams OK. but there is a strange error
i.e
SQL query:
CREATE TRIGGER upd
BEFORE UPDATE ON chats FOR EACH ROW
BEGIN
DECLARE my_cursor CURSOR FOR SELECT name FROM messages;
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 '' at line 4

This works. Will gladly delete it when it gets dupe closed.
It merely lacks a delimiter wrapping block.
drop trigger if exists upd;
delimiter $$
CREATE TRIGGER upd
BEFORE UPDATE ON chats FOR EACH ROW
BEGIN
DECLARE vapenid INT;
END;
$$
delimiter ;

Related

error in DECLARE variable mysql

this is my trigger
CREATE TRIGGER `proximo_pago` BEFORE INSERT ON `pago`
FOR EACH ROW BEGIN
declare num_orden integer;
select max(orden) from pago where lote=NEW.lote into num_orden;
if(num_orden is NULL)
then
set NEW.orden=1;
else
set NEW.orden=num_orden+1;
end if;
END
and the ERROR
SQL query:
CREATE TRIGGER `proximo_pago` BEFORE INSERT ON `pago`
FOR EACH ROW BEGIN
declare num_orden integer;
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 3
I've tried to use DELIMITER but it isnĀ“t works, please help me , thanks
My answer is based only on the necessity of your question which is declaration of the variable.. if the Delimiter doesn't do the trick maybe its an error in your syntax because you didn't SET your SELECT query in a variable. try this first.
DELIMITER $$
CREATE
TRIGGER `proximo_pago` BEFORE INSERT
on `pago`
FOR EACH ROW
BEGIN
DECLARE num_orden int;
SET num_orden = select max(orden) from pago where lote=NEW.lote;
if(num_orden is NULL)
then
set NEW.orden=1;
else
set NEW.orden=num_orden+1;
end if;
END$$
DELIMITER ;

#1064 - You have an error in your SQL syntax when create trigger

I have faced this problem when I created an after insert trigger. I know this is an syntax error, but I don't know how to fix it in my SQL.
1064 - 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 '' at line 5
Here is my trigger:
CREATE TRIGGER insert_to_student_table
AFTER INSERT ON user
FOR EACH ROW
BEGIN
DECLARE v_UserID INTEGER;
SET #v_UserID := (Select Max(UserID) from user where Type="Student");
INSERT INTO student (StudentID,TutorID)VALUES (v_UserID, NULL);
END
You need to have a delimiter
delimiter //
CREATE TRIGGER insert_to_student_table
AFTER INSERT ON user
FOR EACH ROW
BEGIN
DECLARE v_UserID INTEGER;
SET v_UserID = (Select Max(UserID) from user where Type="Student");
INSERT INTO student (StudentID,TutorID)VALUES (v_UserID, NULL);
END;//
delimiter ;

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.

MySQL Trigger ERROR in phpAdmin

Hi
Is there any error in this TRIGGER Statement.When ever i try to run this in phpAdmin its giving error saying "#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 Count(*) into SIM_CCode_Count".I cant get what's wrong in this..please help me
This is my trigger statement
CREATE TRIGGER Is_CountryCode_There After INSERT on mr_details FOR EACH ROW
BEGIN
DECLARE SIM_CCode_Count INTEGER;
DECLARE NET_CCode_Count INTEGER;
SELECT Count(*) into SIM_CCode_Count FROM Country_Main where CountryCode=NEW.SimCntISO;
IF SIM_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.SIMCntISO,"Unknown");
END IF
If NEW.SimCntISO<>NEW.NetCntISO then
SELECT Count(*) into NET_CCode_Count FROM Country_Main
where CountryCode=NEW.NetCntISO
IF NET_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.NETCntISO,"Unknown");
END IF
END IF
END
Without proper explanation about your requirement and about tables and what you are expecting this trigger to do,its very difficult to say if any issues there in your trigger..
But as far as i can see there is some minor correction need to be done..
Try this Code and let know in detail your requirements..
CREATE TRIGGER Is_CountryCode_There After INSERT on mr_details FOR EACH ROW
BEGIN
DECLARE SIM_CCode_Count INTEGER;
DECLARE NET_CCode_Count INTEGER;
SELECT Count(*) into SIM_CCode_Count FROM Country_Main where CountryCode=NEW.SimCntISO;
IF SIM_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.SIMCntISO,"Unknown");
END IF;
If (NEW.SimCntISO<>NEW.NetCntISO) then
SELECT Count(*) into NET_CCode_Count FROM Country_Main
where CountryCode=NEW.NetCntISO;
IF NET_CCode_Count=0 THEN
INSERT INTO Country_Main(CountryCode,CountryName) Values(NEW.NETCntISO,"Unknown");
END IF;
End IF;
END;
You have to declare a mysql-statement delimiter before the trigger statement:
DELIMITER |
CREATE TRIGGER ...
(your code)
END|
DELIMITER ;
Otherwise MySQL interprets your ; in this statement as statement commit and executes the code immidiately. With the delimiter changed to a different character you can use the semicolon inside the trigger declaration safely.
See here: http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html

declare and assign value my sql stored procedure(5.0.45)

DELIMITER $$
DROP PROCEDURE IF EXISTS quotations.sp_addservices $$
CREATE PROCEDURE quotations.sp_addservices
(In categoryname varchar(25),in servicename varchar(250),in hours float,in cost float,in basis nvarchar (100))
BEGIN
insert into categorydetails (Category_Name) values (categoryname);
if(categoryname!=null)
then
DECLARE category_id int;
set category_id= select max(Category_Id) from categorydetails ;
insert into servicesdetails (Service_Name,Category_Id,Hours,Cost,Basis) values(servicename,category_id,hours,cost,basis);
end if;
END $$
DELIMITER ;
This is my stored procedure .I have to retrive the value of categoryid that is posted into the database which is auto increased.Here i cant declare the variable and assign value to variable.Am getting error like
Script line: 4 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 'DECLARE category_id int;
set category_id= select max(Category_Id) from categor' at line 9
Can any one help me
Thanks in advance.
Try
SELECT MAX(c.category_id) INTO category_id FROM categorydetails c;