I tried to create this simple function, but I have the following 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 '(customerLevel);
END' at line 17
here is the code:
DELIMITER $$
CREATE FUNCTION GetCustomerLevel(CustomerID INT)
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE credit DECIMAL(10,2) DEFAULT 0;
DECLARE customerLevel VARCHAR(20);
SELECT SUM(total_price)
INTO credit
FROM sales s,customer c
WHERE c.customer_id= CustomerID And s.customer_id=c.customer_id ;
IF credit > 1400 THEN
SET customerLevel = 'PLATINUM';
ELSE
SET customerLevel = 'NOT PLATINUM';
END IF;
RETURN (customerLevel);
END$$
DELIMITER ;
I just shut down MySQL server and restart it and the code run without any error!
Thanks all :)
Related
I am trying to create a function which will return the name of the day one is born.I am passing the name of person as parameter.
CREATE FUNCTION name_of_day (name_of_student VARCHAR(150))
RETURNS VARCHAR(150)
AS
BEGIN
RETURN (SELECT DAYNAME(`date_of_birth`) AS 'Name of week'
FROM student
WHERE student.student_name = name_of_student);
END;
I got the error message
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 'as
Remove the "AS" from it, on line three, see if that works :) good luck!
Here's a basic example from mysqltutorial.org:
DELIMITER $$
CREATE FUNCTION CustomerLevel(
credit DECIMAL(10,2)
)
RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE customerLevel VARCHAR(20);
IF credit > 50000 THEN
SET customerLevel = 'PLATINUM';
ELSEIF (credit >= 50000 AND
credit <= 10000) THEN
SET customerLevel = 'GOLD';
ELSEIF credit < 10000 THEN
SET customerLevel = 'SILVER';
END IF;
-- return the customer level
RETURN (customerLevel);
END$$
DELIMITER;
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 ;
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.
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 keep getting the following 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_id' varchar(20) null , OUT 'v_status' varchar(40)) IS Begin ' at line 2
It is the first time I'm using MySql and the little pl sql knowledge isn't really helping.
delimiter //
->CREATE PROCEDURE dbi292801.campsite
(IN 'v_id' varchar(20) null ,
OUT 'v_status' varchar(40)) IS
Begin
SELECT status into v_status FROM participant WHERE RFID = v_id;
if (v_status = "in", v_status := "at campsite", v_status := 'at event');
End if;
End //
If you have an idea to solve this, please reply!!