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.
Related
I am getting a syntax error on executing the below function in MySQL.
CREATE FUNCTION FABC (P_MEMBER_EMAIL VARCHAR(30))
RETURNS int
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END;
The error is as follows "#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
Not sure what's causing this. Can anyone help me ?
You need to set a delimiter that will tell MySQL the END of the function definition.
DELIMITER $$
CREATE FUNCTION `FABC`(`P_MEMBER_EMAIL` VARCHAR(30)) RETURNS int(11)
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END$$
DELIMITER ;
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 ;
Please help with this MySQL trigger - have searched and can not find problem with syntax.
Getting 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 '#v_index int DEFAULT 1; WHILE #v_index <= NEW.pt_number INSERT INTO Pat' at line 9
DELIMITER $$
CREATE TRIGGER after_insert_calls_insert_patients
AFTER INSERT ON Calls
FOR EACH ROW
BEGIN
DECLARE #v_index int DEFAULT 1;
WHILE #v_index <= NEW.pt_number
INSERT INTO Patients SET
Patient_id = CONCAT(NEW.Calls_id, '-', #v_index),
FK_Calls_id = NEW.Calls_id,
update_by = "SYSTEM";
SET #v_index := #v_index +1;
END WHILE;
END;
$$
DELIMITER ;
Try dropping the # in "DECLARE #v_index int DEFAULT 1;"
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 ;
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.