So I have this procedure to calculate freights. I need to select two values from matching row. And according to condition based on in_quantity, out of these two previously selected values one will be set to out_total.
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
out_total = val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2;
END IF;
END$$
DELIMITER ;
When executed it gives 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 '= val1;
END IF;
IF(in_quantity > 9) THEN
out_total = val2' at line 16
beacause any value assign to a variable in mysql , we use SET keyword.
so change only in procedure
SET out_total = val1;
SET out_total = val1;
DELIMITER $$
CREATE PROCEDURE freight_calc(
IN in_delivery_location VARCHAR(100),
IN in_category_id INT(11),
IN in_quantity INT(11),
OUT out_total DECIMAL(10,2)
)
BEGIN
DECLARE val1 DECIMAL(10,2);
DECLARE val2 DECIMAL(10,2);
SELECT col1,col2 INTO val1, val2
FROM `freight_rules` fr
WHERE fr.category_id = in_category_id AND fr.delivery_location = in_delivery_location;
IF(in_quantity <= 9) THEN
SET out_total = val1;
END IF;
IF(in_quantity > 9) THEN
SET out_total = val1;
END IF;
END$$
DELIMITER ;
Related
i have created one stored procedure for sum of perticular condition but getting syntax error.
create table script :
CREATE TABLE count_smaller_coverage (count_records INT(11) ,block_id INT(11))
insert data :
INSERT INTO count_smaller_coverage
SELECT '114000','1' UNION
SELECT '112000','2' UNION
SELECT '98765','3' UNION
SELECT '78965','4' UNION
SELECT '4125','5' UNION
SELECT '123654','6' UNION
SELECT '78999','7' UNION
SELECT '89888','8' UNION
SELECT '99654','9' UNION
SELECT '75365','10' UNION
SELECT '25638','11' UNION
SELECT '85236','12' UNION
SELECT '65478','13' UNION
SELECT '65478','14' UNION
SELECT '85236','15'
Stored Procedure :
DELIMITER $$
DROP PROCEDURE IF EXISTS test_mysql_while_loop$$
CREATE PROCEDURE test_mysql_while_loop()
BEGIN
DECLARE strat INT;
DECLARE END INT;
DECLARE SumofCount BIGINT;
DECLARE block_id VARCHAR(2000);
SET strat=(SELECT MIN(block_id) FROM count_smaller_coverage);
SET END =(SELECT MAX(block_id) FROM count_smaller_coverage);
CREATE TABLE blocks_parts (block_id VARCHAR(2000), Counts BIGINT);
test: WHILE strat<=END DO
BEGIN
IF SumofCount > 800000 THEN
SET SumofCount=0;
SET block_id = NULL;
END IF;
SET SumofCount=COALESCE(SumofCount,0)+(SELECT count_records FROM count_smaller_coverage WHERE block_id=strat);
SELECT block_id = (COALESCE(block_id + ',', '') + CAST(block_id AS CHAR)) AS id FROM count_smaller_coverage WHERE block_id=strat;
IF SumofCount BETWEEN 800000 AND 1000000 THEN
INSERT INTO blocks_parts(block_id,Counts) VALUES (block_id,SumofCount);
END IF;
IF SumofCount BETWEEN 800000 AND 100000 THEN
LEAVE test;
END IF;
SET strat=strat+1;
END test;
END$$
DELIMITER ;
Error :
Query: CREATE PROCEDURE test_mysql_while_loop() BEGIN DECLARE strat INT; DECLARE end INT; DECLARE SumofCount BIGINT; DECLARE block_id V...
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 'test;
END' at line 33
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.060 sec
You have a couple of issues in your procedure. Firstly, you have an unnecessary BEGIN after the DO in your WHILE statement. You can either remove that or match it with an END. Secondly, you need to end the WHILE loop with an END WHILE, in your case adding the test label to that statement. This should work:
test: WHILE strat<=END DO
-- BEGIN -- if you put BEGIN here ...
IF SumofCount > 800000 THEN
SET SumofCount=0;
SET block_id = NULL;
END IF;
SET SumofCount=COALESCE(SumofCount,0)+(SELECT count_records FROM count_smaller_coverage WHERE block_id=strat);
SELECT block_id = (COALESCE(block_id + ',', '') + CAST(block_id AS CHAR)) AS id FROM count_smaller_coverage WHERE block_id=strat;
IF SumofCount BETWEEN 800000 AND 1000000 THEN
INSERT INTO blocks_parts(block_id,Counts) VALUES (block_id,SumofCount);
END IF;
IF SumofCount BETWEEN 800000 AND 100000 THEN
LEAVE test;
END IF;
SET strat=strat+1;
-- END -- ... you must put END here
END WHILE test;
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `Test`$$
CREATE DEFINER=`root`#`%` PROCEDURE `Test`()
BEGIN
SET #SumofCount=0;
SET #block_id='';
SET #Start=(SELECT MIN(block_id) FROM count_smaller_coverage);
SET #End =(SELECT MAX(block_id) FROM count_smaller_coverage);
SET #v1=5;
myloop: WHILE #Start<=#End DO
IF #SumofCount > 800000 THEN
SET #SumofCount=0;
END IF;
SET #SumofCount=(IFNULL(#SumofCount,0)+(SELECT count_records FROM count_smaller_coverage WHERE block_id=#Start));
SET #block_id = (SELECT CONCAT(#block_id ,CAST(block_id AS CHAR),',') AS id FROM count_smaller_coverage WHERE block_id=#Start);
IF #SumofCount BETWEEN 800000 AND 1000000 THEN
SET #block_id = CONCAT(LEFT(#block_id, CHAR_LENGTH(#block_id) -1), '');
INSERT INTO blocks_parts(block_id,Counts) VALUES (#block_id,#SumofCount);
SET #block_id='';
END IF;
IF #Start = #End THEN
SET #block_id = CONCAT(LEFT(#block_id, CHAR_LENGTH(#block_id) -1), '');
INSERT INTO blocks_parts(block_id,Counts) VALUES (#block_id,#SumofCount);
END IF;
SET #Start=#Start+1;
END WHILE myloop;
END$$
DELIMITER ;
When I am trying to create a function in mysql 5.7.20, I met error 1064:
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case
when exists (select 1 from user_favorstocks where uid=uid_int and stockid = stockid_char)
then begin
insert into user_favorstocks (uid, stockid, added_date) values (uid_int, stockid_char, added_date);
set ret=1;
end
else
begin
case (select is_deleted from user_favorstocks where uid=uid_int and stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks set is_deleted=0, db_update_time=now() where uid=uid_int and stockid=stockid_char;
set ret=3;
end;
end
end
end
return ret
end//
delimiter ;
The error message is
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 'else
begin
case (select is_deleted from user_favorstocks whe' at line 15
Does this because that I am using begin...end in an else clause?
I found the example in MySQL's documentation:
DELIMITER |
CREATE PROCEDURE p()
BEGIN
DECLARE v INT DEFAULT 1;
CASE v
WHEN 2 THEN SELECT v;
WHEN 3 THEN SELECT 0;
ELSE
BEGIN
END;
END CASE;
END;
|
It seems valid to use a begin...end statement in an else clause.
Try:
DROP FUNCTION IF EXISTS `add_favorstocks`;
delimiter //
create function add_favorstocks (
uid_int INT,
stockid_char CHAR(20),
added_date CHAR(20)
)
returns INT
begin
declare ret INT;
case when exists (select 1
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
then begin
insert into user_favorstocks
(uid, stockid, added_date)
values
(uid_int, stockid_char, added_date);
set ret=1;
-- end <-- missing a semicolon
end;
else
begin
case (select is_deleted
from user_favorstocks
where uid=uid_int and
stockid = stockid_char)
when 0 then set ret=0;
else begin
update user_favorstocks
set is_deleted=0,
db_update_time=now()
where uid=uid_int and
stockid=stockid_char;
set ret=3;
end;
-- end <-- missing CASE and a semicolon
end case;
-- end <-- missing a semicolon
end;
-- end <-- missing CASE and a semicolon
end case;
-- return ret <-- missing a semicolon
return ret;
end//
delimiter ;
I am making a procedure that inserts a place ("Sted") and I would like to check if the inputs are NULL. However, whenever I try to add an if-statement at the start to surround my code (marked CRASH below), it gives me an error saying my syntax is not right at "DECLARE varStedskodeID INT;" which is the part after the IF-statement I'm trying to add.
To my eyes the syntax of my if-statement is the same inside the code, but only my soon-to-be-NULL-check if-statement crashes even with just a simple IF(TRUE) THEN.
Can anyone give me a hint of what causes this one if to crash?
DROP PROCEDURE IF EXISTS InsertSted;
DELIMITER $$
CREATE PROCEDURE InsertSted(
IN inputStedsnavn VARCHAR(255),
IN inputStedstype VARCHAR(255),
IN inputKommunenavn VARCHAR(255))
BEGIN
IF(TRUE) THEN <<------ CRASH
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF; <<------ CRASH
END$$
DELIMITER ;
DECLARE is permitted only inside a BEGIN ... END compound statement
and must be at its start, before any other statements.
http://dev.mysql.com/doc/refman/5.0/en/declare.html
MySQL follows strict rules for DECLARE. You have to DECLARE variables, tables, etc... at the beginning of Stored Procedure.
Change Stored Procedure like this
DECLARE varStedskodeID INT;
DECLARE varKommunenr INT;
IF(TRUE) THEN
IF(SELECT COUNT(StedkodeID) FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1) = 0 THEN
INSERT INTO stedstype VALUES(DEFAULT, inputStedstype);
END IF;
SET varStedskodeID = (SELECT StedkodeID FROM stedstype WHERE Kodenavn = inputStedstype LIMIT 1);
IF(SELECT COUNT(Kommunenr) FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1) = 1 THEN
SET varKommunenr = (SELECT Kommunenr FROM kommune WHERE Kommunenavn = inputKommunenavn LIMIT 1);
INSERT INTO sted VALUES(DEFAULT, inputStedsnavn, varStedskodeID, varKommunenr);
END IF;
END IF;
I encountered a problem when trying to convert MSSQL code to MySQL.
My MSSQL code is:
CREATE PROCEDURE ProductGroupGenerationSettingsCheck
AS
declare #id bit , #result varchar(50);
SELECT #id = automaticProductIdGeneration
FROM tbl_Settings--)
if(#id =0)
begin
set #result='false'
end
else if (#id =1)
begin
set #result='true'
end
select #result
My MySQL code is:
delimiter //
create procedure ProductGroupGenerationSettingsCheck(p_id tinyint(1),p_result varchar(50))
begin
select p_id = automaticProductIdGeneration from tbl_Settings ;
if(p_id = 0)
begin
set p_result = 'false' ;
end
else if (p_id = 1)
begin
set p_result = 'true' ;
end
select p_result as 'result' ;
end //
delimiter ;
the error I get 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 'begin set p_result = 'false' ; end else if (p_id = 1) begin set p_re' at line 5
What is wrong in my code?
delimiter //
create procedure ProductGroupGenerationSettingsCheck(IN p_id tinyint(1),IN p_result varchar(50)) /*specify what type of parameter it is, IN / OUT / INOUT*/
begin
select p_id := automaticProductIdGeneration from tbl_Settings ; /*use assignment operator := instead of comparison =*/
if(p_id = 0) then /*missing a then here*/
begin
set p_result = 'false' ;
/*don't end the if, when you still have an else if condition*/
else if (p_id = 1) then /*missing a then again*/
begin
set p_result = 'true' ;
end
end if; /*missing an if here*/
select p_result as 'result' ; /*you could also use an OUT parameter for this...anyway...*/
end //
delimiter ;
manual entry if statement
There are one error in the query at the forth line. Yo must use the next query.
select automaticProductIdGeneration into p_id from tbl_Settings;
It could be usefull if you post the entire error message. It shows you where is exactly the first problem.
#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 = -1 else SELECT audit_id INTO t_new_id FROM a_audit_reg at line 11
delimiter //
CREATE FUNCTION get_audit_id (p_pubco_id int(10),
p_audit_id int(10),
p_fiscal_date date)
RETURNS int(10)
BEGIN
DECLARE t_new_id int(10);
#check paremeters here
if p_pubco_id = 0 or p_audit_id = 0
then set t_new_id = -1
else
set t_new_id = (SELECT audit_id
FROM a_audit_reg
WHERE p_pubco_id = a_audit_reg.pubco_id
and p_audit_id = a_audit_reg.audit_id
and p_fiscal_period = a_audit_reg.fiscal_period_date);
if found_rows() = 0
then
insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
values (p_pubco_id, p_audit_id, p_fiscal_date);
set t_new_id = last_insert_id();
end if;
end if;
return t_new_id;
END //
delimiter ;
change the DELIMITER
use SET
query,
DELIMITER //
CREATE FUNCTION get_audit_id
(
p_pubco_id INT,
p_audit_id INT,
p_fiscal_date DATE
)
RETURNS INT
BEGIN
DECLARE t_new_id INT;
IF p_pubco_id = 0 or p_audit_id = 0 THEN
SET t_new_id = -1;
ELSE
SET t_new_id = (SELECT audit_id
FROM a_audit_reg
WHERE p_pubco_id = a_audit_reg.pubco_id
and p_audit_id = a_audit_reg.audit_id
and p_fiscal_period = a_audit_reg.fiscal_period_date);
IF found_rows() = 0 then
insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
values (p_pubco_id, p_audit_id, p_fiscal_date);
SET t_new_id = last_insert_id();
end if;
end if;
return t_new_id;
END //
DELIMITER ;