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;
Related
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 :)
I have been searching this site and Google for over an hour trying to figure out why I get this 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 '#msg = 'HIGH VALUE CUSTOMER - SALES GREATER THAN
$10K' IF (SALES > 10000) THEN' at line 5
...when running the SQL to create this trigger:
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100)
#msg = 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K'
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = #msg
ENDIF
This is for an assignment that is due by midnight tomorrow night and I'm stumped because everything I have tried is in the same syntax as the examples and I'm not trying to create a complicated trigger. Please help!?!?!
This query has so many errors, syntax, variable define, delimiter etc... Even the inner logic is not clear as well, you should read about CREATE PROCEDURE and CREATE FUNCTION Syntax first, here I only fix some syntax errors:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100) DEFAULT 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = msg;
END IF;
END;
||
DELIMITER ;
Edit:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
BEFORE INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE msg VARCHAR(100) DEFAULT 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
IF (NEW.SALES > 10000) THEN
SET NEW.CUSTOMERNOTES = msg;
END IF;
END;
||
DELIMITER ;
I ended up figuring it out...here's what worked for me:
DELIMITER ||
CREATE TRIGGER TOP_CUSTOMERS
AFTER INSERT ON ORDERS
FOR EACH ROW BEGIN
DECLARE CUSTOMERNOTES VARCHAR(100);
IF (SALES > 10000) THEN
SET CUSTOMERNOTES = 'HIGH VALUE CUSTOMER - SALES GREATER THAN $10K';
END IF;
END;
||
DELIMITER ;
Thanks everyone that responded for pointing me in the right direction.
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'm getting the following error whenever i'm trying to create the following function in mysql.
error : 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
And my function is
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
RETURN result;
END
You need to change the delimiter before creating a function. If you don't your CREATE statement terminates at the first semi-colon.
Try this:
DELIMITER $$
CREATE FUNCTION GET_HOUR_MINUTES(seconds INT)
RETURNS VARCHAR(16)
BEGIN
DECLARE result VARCHAR(16);
IF seconds >= 3600 THEN SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%kh %lm');
ELSE SET result = TIME_FORMAT(SEC_TO_TIME(seconds),'%lm');
END IF
RETURN result;
END$$
DELIMITER ;
Thanks to #Ravinder for catching the missing END IF.
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 ;