mySQL Trigger on second table - mysql

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;"

Related

MySQL IF statement in Trigger

I just want to create trigger in my sql but some error happened
this is the code
CREATE TRIGGER delete_santri_in_kamar
AFTER UPDATE ON
santri
FOR EACH ROW
BEGIN
DECLARE stat INT
SET stat = select status FROM santri WHERE id_santri=new.id_santri
IF (stat = 0) THEN
DELETE FROM santri_kamar_asrama WHERE id_santri=new.id_santri
END IF
END
and this is the error message
#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 'SET stat = select status FROM santri WHERE id_santri=new.id_santri
IF (stat =' at line 7
please help me
Add semicolons after the statements and change default delimiter(;) before the code of create trigger. Otherwise it will give
SQL Error(1064): You have an error in your SQL Syntax;
After the create trigger code make ; as default delimiter
DELIMITER $$
CREATE TRIGGER delete_santri_in_kamar
AFTER UPDATE ON
santri
FOR EACH ROW
BEGIN
DECLARE stat INT;
SET stat = (select status FROM santri WHERE id_santri=new.id_santri);
IF (stat = 0) THEN
DELETE FROM santri_kamar_asrama WHERE id_santri=new.id_santri;
END IF;
END
$$
DELIMITER ;

Mysql function - cant figure it out

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.

MySQL stored Proc syntax confusion

Using syntax form docs, read many examples and docs, I have reduced code down to isolate the syntax that is causing the failure, I cannot see it... I'm still getting an error.
Running thru MySQL command line
PARAMS: A_score smallint, B_score smallint
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
DECLARE winner BIGINT DEFAULT 0;
DECLARE winningScore, losingScore SMALLINT DEFAULT;
if A_score > B_score then
SET winningScore = 1;
elseif A_score < B_score then
SET winningScore = 2;
end if;
start transaction;
UPDATE
winners
SET
winner = winningScore
WHERE
id = 1
commit;
end $$
delimiter ;
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 A_score > B_score then
SET winningScore = 1;
elseif A_score' at line 4
I see you forgot to write SET (in the previous version of your question) to assign values to your variables.
(I changed the type for winningScore and losingScore to be characters, because smallints can't be strings):
-- Be sure to change the default delimiter before writing your procedure
delimiter $$
create procedure my_procedure(A_score smallint, B_score smallint)
begin
DECLARE winner BIGINT DEFAULT 0;
DECLARE winningScore, losingScore VARCHAR(2) DEFAULT;
if A_score > B_score then
SET winningScore = 'A';
-- ^^^--you forgot this
elseif A_score < B_score then
SET winningScore = 'B';
-- ^^^--and this
else
SET winningScore = 'AB';
-- ^^^--and this
end if;
start transaction;
-- Do whatever your transaction is meant to be
commit;
end $$
-- ^^--- And I think you're forgetting to put this
-- Be sure to reset the delimiter to ; after you end your procedure
delimiter ;
Quoting from the reference manual:
Variables can be set directly with the SET statement. See Section 13.7.4, “SET Syntax”.
Hope this helps

mysql syntax error near '', how can i correct it?

I'm using MySQL when I meet an error executing query below.
CREATE PROCEDURE insert_apply(
IN in_username VARCHAR(25),
IN in_machine_id INT
)
BEGIN
START TRANSACTION;
IF EXISTS (SELECT * FROM machine WHERE machine.id = in_machine_id AND machine.available = 1) THEN
INSERT INTO user_machine VALUES (in_username, in_machine_id);
UPDATE machine SET machine.available = 0 WHERE machine.id = in_machine_id;
END IF;
COMMIT;
END;
MySQL error is,
#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
What does " near '' " mean? How can I correct this error?
Thanks a lot.
I think these one useful to you. Actually your missed DELIMITER //
DELIMITER //
CREATE PROCEDURE insert_apply(
IN in_username VARCHAR(25),
IN in_machine_id INT
)
BEGIN
START TRANSACTION;
IF EXISTS (SELECT * FROM machine WHERE machine.id = in_machine_id AND machine.available = 1) THEN
INSERT INTO user_machine VALUES (in_username, in_machine_id);
UPDATE machine SET machine.available = 0 WHERE machine.id = in_machine_id;
END IF;
COMMIT;
END;
Thank you.

#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 ;