I am using MySQL v5.0.92 and I'm trying to import some data, but I get an error on declare my variables.
BEGIN
DECLARE flag INT;
DECLARE id INT;
while flag=0 begin
SET id=(SELECT top 1 user_id
FROM ac_user_info WHERE user_id>#id order by user_id)
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'first_name',first_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
INSERT INTO cuddleew_database1.cewp_usermeta(user_id,meta_key,meta_value)
SELECT id,'last_name',last_name
FROM cuddleew_backup.ac_user_info WHERE user_id=#id
SET flag=(select case #id when(SELECT MAX(user_id)
FROM cuddleew_bakup.ac_user_info) then 1
else 0
END CASE)
END WHILE
END
In MySQL console i get this:
BEGIN DECLARE flag INT;
#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 'DECLARE flag INT' at line 2.
Can you help me with this and tell me what's wrong. Thanks in advance.
You have to change your delimiter first, so the ; doesn't tell MySQL that this command is over.
DELIMITER $$
[your code here]
END $$
DELIMITER ;
Related
i'm using MySql Workbench and im unable to figure out this.
delimiter $$
CREATE function `klientu_copy`()
DECLARE v_laiks TIMESTAMP;
DECLARE v_liet VARCHAR(200);
set v_laiks = now();
set v_liet = current_user;
if (TG_OP = 'DELETE') THEN
insert into kopija_klienti values (v_liet,v_laiks,old.Vards,old.Uzvards,null,null);
ELSEif (TG_OP = 'INSERT') THEN
insert into kopija_klienti values (v_liet,v_laiks,null,null,new.Vards,new.Uzvards);
ELSEif (TG_OP='UPDATE') THEN
insert into kopija_klienti values (v_liet,v_laiks,old.Vards,old.Uzvards,new.Vards,new.Uzvards);
end if;
END; $$
delimiter ;
21:24:22 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 ''klientu_copy' () BEGIN DECLARE v_laiks timestamp; DECLARE v_liet varchar; s' at line 1 0.000 sec
Did try set #variable / declare #variable, cant figure this out. I'm still learning :)
You must specify the length of the varchar variable, like varchar(100)
Also add the closing ( for the first insert.
I cannot seem to get this working properly, its always a different error I am getting. Any suggestions are appreciated at this point...
CREATE DEFINER=`db`#`localhost` FUNCTION `output_date`(in_date DATE) RETURNS DATE
READS SQL DATA
BEGIN
DECLARE date_format_index INT;
DECLARE date_format_string VARCHAR;
SELECT s.output_date_format INTO date_format_index FROM config s
SET date_format_string = ( CASE date_format_index WHEN 2 THEN '%d-%m-%Y' WHEN 3 THEN '%m-%d-%Y' ELSE '%Y-%m-%d' END );
RETURN in_date
END
I am using DELIMITER $$ when attempting to process this function.
The current error is ...
Error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '; SELECT s.output_date_format INTO date_format_index FROM system_config s SET d'
My environment is MyEclipse, newest version.
The MySQL version I have is 5.2.
You are missing some semi-colons and a problem with varchar, try this:
DELIMITER $$
CREATE DEFINER=`db`#`localhost` FUNCTION `output_date`(in_date DATE) RETURNS DATE
READS SQL DATA
BEGIN
DECLARE date_format_index INT;
DECLARE date_format_string VARCHAR(100); -- obviously change the size
SELECT s.output_date_format INTO date_format_index FROM config s;
SET date_format_string = ( CASE date_format_index WHEN 2 THEN '%d-%m-%Y' WHEN 3 THEN '%m-%d-%Y' ELSE '%Y-%m-%d' END );
RETURN in_date;
END
$$
DELIMITER ;
It runs on my box.
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 ;
Hey guys i am facing a problem when i use a variable with a case in mysql.
The code which i have used is
DECLARE vSite VARCHAR(20);
SET vSite = case
when id > 0 then 'sdfsdf'
else 'asd' end as name
from customers;
When i run this code it throws me error like
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 vSite VARCHAR(20)' at line 1: DECLARE vSite VARCHAR(20)
CAN anyone point me where am going wrong..Thanks for your valuable help
You need to declare variables inside a BEGIN END block.
Here is a simple example of a stored procedure
DELIMITER //
CREATE procedure blah(IN customer_id INT,OUT vSite VARCHAR(20))
BEGIN
SELECT CASE WHEN id > 0 THEN 'blah'
ELSE 'mah' END INTO vSite FROM customers WHERE id=customer_id;
END//
DELIMITER ;
CALL blah(3,#somevar);
SELECT #somevar;
I want to execute the following code in MySql but I am getting the error
CREATE TRIGGER CINEMAHALLNO_GENERATE_TRIGGER
BEFORE INSERT ON CINEMA_HALLS
FOR EACH ROW
BEGIN
DECLARE cnt DOUBLE;
DECLARE next1 DOUBLE;
SELECT COUNT(HALLNO) INTO cnt from CINEMA_HALLS;
IF cnt > 0 AND cnt < 20 THEN
SELECT MAX(HALLNO) INTO NEXT1 FROM CINEMA_HALLS;
set next1=next1+1;
SET :new.HALLNO=next;
ELSEIF cnt = 0 THEN
SET :new.HALLNO=1;
end if;
end;
Output Error:
SQL query:
CREATE TRIGGER CINEMAHALLNO_GENERATE_TRIGGER
BEFORE INSERT ON CINEMA_HALLS
FOR EACH ROW
BEGIN
DECLARE cnt DOUBLE
MySQL said: Documentation
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
Declare must be placed after the Begin.
Please check the following link