I'm having big troubles to find the error in my sql query.
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SELECT count(*) FROM passenger WHERE Booking IN(SELECT Id FROM booking WHERE
Flight = (SELECT Flight FROM booking WHERE Id = bookingID)) INTO numberBooked;
IF (numberBooked > 59) THEN SET status =”No free seats”;
ELSE SET status =”OK”;
END IF;
RETURN status;
END;
I get this error message:
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 'free seats”; ELSE SET status =”OK”; END IF;
RETURN status; END' at line 10
I would appreciate some help.
Thanks in advance
don't for get to change the DELIMITER
use single quotes
example,
DELIMITER $$
CREATE FUNCTION freeSeats(bookingID INT)
RETURNS VARCHAR(30)
BEGIN
DECLARE numberBooked INT ;
DECLARE status VARCHAR(30);
SET numberBooked =
(
SELECT count(*)
FROM passenger
WHERE Booking IN
( SELECT Id
FROM booking
WHERE Flight = (SELECT Flight FROM booking WHERE Id = bookingID)
)
);
IF (numberBooked > 59) THEN
SET status = 'No free seats';
ELSE
SET status = 'OK';
END IF;
RETURN status;
END $$
DELIMITER ;
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 have a stored procedure that checks whether or not a new entry is already existing in the table. if it exists, the insert will not happen. when I run it, there is an error
DROP PROCEDURE IF EXISTS AddPriority2;
DELIMITER $$
CREATE PROCEDURE AddPriority2
(
IN strName VARCHAR(100),
OUT itExists INT
)
BEGIN
DECLARE
SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId = 1;
IF(itExists = 0) THEN
INSERT INTO priorities
(
NAME,
StatId
)
VALUES
(
strName,
1
);
END IF;
END
here is the error
Query: CREATE PROCEDURE AddPriority2 ( IN strName VARCHAR(100), OUT itExists INT ) BEGIN DECLARE SELECT COUNT(Id) INTO itExists FROM pr...
Error Code: 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 'SELECT COUNT(Id) INTO itExists
FROM priorities
WHERE Name = strName AND StatId =' at line 8
1) You cannot declare a select statement - a declare has to be for a variable..(and I would not use an output parameter for that) 2) or you can use exists instead
if not exists (select 1 from priorities WHERE Name = strName AND StatId = 1) then
insert...
end if;
I ask because I don't know where is the error.
I need to create a function in MySQL. I'm using PhpMyAdmin.
CREATE FUNCTION `aggiungiPermesso`(matricolaUtenteLoggato VARCHAR(20),nomeNuovoPermesso VARCHAR(20),descrizioneNuovoPermesso VARCHAR(250)) RETURNS VARCHAR(150)
BEGIN
DECLARE hapermesso INT;
SELECT COUNT(*) INTO hapermesso FROM ottiene WHERE matricolaUtenteLoggato = ottiene.Matricola AND ID =(SELECT id FROM permesso WHERE Nome = 'Aggiungi permesso')
IF(hapermesso < 1) BEGIN
RETURN 'Errore. Non hai i permessi.';
END
INSERT INTO `permesso`(`ID`,`Nome`, `Descizione`) VALUES(0,nomeNuovoPermesso,descrizioneNuovoPermesso) RETURN 'Nuova operazione aggiunta con successo.';
END
DELIMITER;
The 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 3
What is the error?
The error was because you were using wrong syntax for Return and IF. I have corrected your query and it works fine now.
DELIMITER $$
CREATE FUNCTION `aggiungiPermesso`(matricolaUtenteLoggato VARCHAR(20),nomeNuovoPermesso VARCHAR(20),descrizioneNuovoPermesso VARCHAR(250)) RETURNS VARCHAR(150) CHARSET latin1
BEGIN
DECLARE hapermesso INT;
SELECT COUNT(*) INTO hapermesso FROM ottiene
WHERE matricolaUtenteLoggato = ottiene.Matricola AND ID =(SELECT id FROM permesso WHERE Nome = 'Aggiungi permesso');
BEGIN RETURN IF(hapermesso > 1,'Errore. Non hai i permessi.','None');
END;
INSERT INTO `permesso`(`ID`,`Nome`, `Descizione`)
VALUES(0,nomeNuovoPermesso,descrizioneNuovoPermesso);
RETURN 'Nuova operazione aggiunta con successo.';
END$$
DELIMITER ;
I receive the following error when using the below function and can not figure out how to fix it.
Error:
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 ';
RETURN IF(result IS NOT NULL, result, '0');
END' at line 8
Function:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE result VARCHAR(255);
SET result = NULL;
IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
ENDIF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
Email would be read in as a varchar or text such as "user#domain.com"
expiration_date is a of type TIMESTAMP
payer_email is of type VARCHAR(255)
I've also tried the following function with a different error being shown.
Error:
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 ';
READS SQL DATA
BEGIN
DECLARE result TIMESTAMP;
SET res' at line 2
Function:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS TIMESTAMP;
READS SQL DATA
BEGIN
DECLARE result TIMESTAMP;
SET result = NULL;
IF (LENGTH(verify_email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
ENDIF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
I want to use either of these functions so that an application can just pass in expiration(email); and get a result if the email is of length 9 or greater or otherwise receive nothing.
UPDATE:
Thank you KAILLINGER. Spent 2 hours working on this, and it was a typo! :(
Correct answer for completeness:
delimiter $$
CREATE FUNCTION expirations(email VARCHAR(255))
RETURNS VARCHAR(200)
READS SQL DATA
BEGIN
DECLARE result VARCHAR(200);
SET result = NULL;
IF (LENGTH(email) > 8) THEN SELECT expiration_date INTO result FROM paypal WHERE payer_email = email;
END IF;
RETURN IF(result IS NOT NULL, result, '0');
END$$
delimiter ;
Your ENDIF; is not good, it should be END IF; :)
I am trying to write a trigger for my database using mysql and phpmyadmin. I have a database with a table for a client's information in which I store a credit card number and a table for hotel reservations where I store the client's way of paying. I want my trigger to activate after update on client's info and if the credit card number is deleted to change the way of payment. The problem is that phpmyadmin does not compile it.
CREATE TRIGGER after_credit_card_update
AFTER UPDATE
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or (new.Credit_Card_Number is null)) then
select into cl_id
select reservation.Client_Id from
reservation where ((reservation.Client_Id = new.Client_Id) and (reservation.Payment = 'Credit'))
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$
I believe that the problem is when I am declaring my value: cl_id
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 'into cl_id select reservation.Client_Id from reservation where ' at line 8
FIXED :
DELIMITER $$
CREATE TRIGGER after_credit_card_update
AFTER UPDATE
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or
new.Credit_Card_Number is null)) then
select reservation.Client_Id into cl_id from reservation
here ((reservation.Client_Id = new.Client_Id) and
reservation.Payment = 'credit'))
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$
DELIMITER ;
Not going into detail here, but you need to add delimiter statements before and after your trigger - so MySQL will know how to parse the code. For example:
DELIMITER $$
CREATE TRIGGER after_credit_card_update
AFTER UPDATE
ON client
for each row
BEGIN
declare cl_id int;
IF ( (new.Credit_Card_Number = " ") or
new.Credit_Card_Number is null)) then
select into cl_id
select reservation.Client_Id from
reservation where reservation.Client_Id = new.Client_Id
limit 1 ;
update reservation
set reservation.Payment = "NOT PAID"
where reservation.Client_Id = cl_id;
END IF;
END$$
DELIMITER ;