Trying to writing this function in mysql but it is giving error that:
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 'IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END ' at line 8
DELIMITER$$
CREATE FUNCTION PricePerHour (Price REAL, Per VARCHAR(5))
RETURNS REAL
DETERMINISTIC
BEGIN
DECLARE t REAL;
IF Price IS NOT NULL AND Per IS NOT NULL
IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END IF;
IF Per='week' THEN SET t=Price/7/24; END IF;
IF Per='month' THEN SET t=Price/30/24; END IF;
IF Per='year' THEN SET t=Price/365/30/24; END IF;
RETURN t;
END IF;
END $$
DELIMITER;
any help should be appreciated. Thanx in advance.
missing THEN ?
DELIMITER$$
CREATE FUNCTION PricePerHour (Price REAL, Per VARCHAR(5))
RETURNS REAL
DETERMINISTIC
BEGIN
DECLARE t REAL;
IF Price IS NOT NULL AND Per IS NOT NULL **THEN**
IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END IF;
IF Per='week' THEN SET t=Price/7/24; END IF;
IF Per='month' THEN SET t=Price/30/24; END IF;
IF Per='year' THEN SET t=Price/365/30/24; END IF;
RETURN t;
END IF;
END $$
DELIMITER;
Related
i'm trying to translate my oracle trigger to mysql but i'm getting an error. This is my mysql trigger:
delimiter //
CREATE TRIGGER z_asdsdas BEFORE UPDATE ON PRODUCT
FOR EACH ROW
BEGIN
DECLARE v_result INT;
SET v_result = 0;
IF 'BETWEEN' = 'BETWEEN' THEN
IF NEW.PRIJS >= 1 AND NEW.PRIJS <= 10 THEN
SET v_result = 1;
END IF;
ELSE
IF NEW.PRIJS < 1 OR NEW.PRIJS > 10 THEN
SET v_result = 1;
END IF;
END IF;
IF v_result = 0 THEN
signal sqlstate -20000 set msgtext = 'error here...';
END IF;
END //
DELIMITER ;
I gave the mysql trigger static values first to test if it should work. This is my oracle trigger:
CREATE OR REPLACE TRIGGER BRG_<code>_<attribute_table>_TRG
BEFORE DELETE OR INSERT OR UPDATE
ON <attribute_table>
FOR EACH ROW
DECLARE
L_OPER VARCHAR2(3);
L_ERROR_STACK VARCHAR2(4000);
BEGIN
IF INSERTING
THEN
L_OPER := 'INS';
ELSIF UPDATING
THEN
L_OPER := 'UPD';
END IF;
DECLARE
L_PASSED BOOLEAN := TRUE;
BEGIN
IF L_OPER IN ('INS', 'UPD')
THEN
IF '<operator>' = 'BETWEEN' THEN
L_PASSED := :NEW.<attribute_column> <GreaterOrEqual> <range_min> AND :NEW.<attribute_column> <LessOrEqual> <range_max>;
ELSE
L_PASSED := :NEW.<attribute_column> <LessThen> <range_min> OR :NEW.<attribute_column> <GreaterThen> <range_max>;
END IF;
IF NOT L_PASSED
THEN
raise_application_error(-20000, '<error>');
END IF;
END IF;
END;
END;
The compiler is gives red lines at the if-statements and a message: syntax error at line 17. Any idea what i'm doing wrong? Mysql is pretty new for me.
Try to correct the line signalling:
...
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'error here...';
...
I try to create a stored procedure with an if statement within.
I copied from: https://dev.mysql.com/doc/refman/5.7/en/local-variable-scope.html
But I get the following error exact on the END IF; near '':
DROP PROCEDURE IF EXISTS `myProc`;
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;
Has anyone an idea where I make a mistake?
Exact error message:
SQL 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 '' at line 19 SQL Error
MySQL Version:
5.5.46
Thanks for help!
I found the solution.
I have to set DELIMITER $$ at first statement and at the end DELIMITER ;
DELIMITER $$;
DROP PROCEDURE IF EXISTS `myProc`; $$
CREATE DEFINER=`root`#`%` PROCEDURE `myProc`(
IN in_userId int,
IN in_projectId int
)
BEGIN
DECLARE tmp_courseId int;
DECLARE done TINYINT DEFAULT 0;
DECLARE cursorProjectCourse CURSOR FOR SELECT CourseId FROM XC_PROJECT_COURSE where projectId = in_projectId ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
OPEN cursorProjectCourse;
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN LEAVE read_loop;
END IF;
SELECT tmp_courseId, in_userId;
END LOOP;
CLOSE cursorProjectCourse;
END;$$
DELIMITER ;
It is important to set the keyword on the first position in line. If there is a blank on the first position, the error above will be thrown.
Well you are missing the loop label while ending loop. Change it to below
read_loop: LOOP
FETCH FROM cursorProjectCourse INTO tmp_courseId;
IF done = 1 THEN
LEAVE read_loop;
END IF;
END LOOP read_loop;
SELECT tmp_courseId, in_userId;
I have a syntax error with my code, which adds 1000 random records to a table.\
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
first syntax error is at default 1 saying it's missing a semicolon, then at my_loop and there are like 4 more.. Any help please? It seems good to go from what I've gooogled.
You need to change the delimiter before defining the statement:
DELIMITER $$
CREATE PROCEDURE addrecords()
BEGIN
DECLARE a INT Default 1;
my_loop: LOOP
<INSERTING>
SET a = a + 1;
IF a=1001 THEN
LEAVE my_loop;
END IF;
END LOOP my_loop;
END
$$
Otherwise, the ; will end the entire CREATE PROCEDURE statement.
I have some problems trying to do a trigger.
Here is my MySQL query for the trigger:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN (
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE (temp%6<>0) temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
)
END IF;
END //
And 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 '; WHILE (temp%6<>0) temp=temp+1; END WHILE; UPDATE strutture SET ' at line 8
Does someone have an idea why I'm getting this error?
Remove the parentheses around your THEN clause. Also your WHILE clause is syntactically incorrect:
delimiter //
CREATE TRIGGER `aggiornaProduzione`
BEFORE UPDATE ON `strutture` FOR EACH ROW
BEGIN
DECLARE temp bigint;
IF ( tipo=1/*mercato*/ AND old.livello <> new.livello )
THEN
SELECT round(2*livello*livello + 13.8*livello)
FROM strutture WHERE tipo=1 AND city=old.city INTO temp;
WHILE temp%6<>0 DO
SET temp=temp+1;
END WHILE;
UPDATE strutture SET produzione=temp WHERE city=new.city AND tipo=1;
END IF;
END //
When running my query I am getting this:
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 3
SQL:
DROP PROCEDURE IF EXISTS insertPromos;
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END;
CALL insertPromos();
You need to define a new delimiter
delimiter |
DROP PROCEDURE IF EXISTS insertPromos |
CREATE PROCEDURE insertPromos()
BEGIN
DECLARE int_val INT DEFAULT 0;
insertPromosLoop : LOOP
IF (int_val = 501) THEN
LEAVE insertPromosLoop;
END IF;
INSERT INTO `promo_code` (`code`, `valid_from` ,`valid_to` , `free_period`)VALUES (CONCAT('PROMO', int_val), '2013-10-10', '2013-11-10', 'P1M');
SET int_val = int_val +1;
END LOOP;
END
|
delimiter ;
CALL insertPromos();
Otherwise the procedure definition would end at the first ; which would not be correct.