I am trying to create the below function in MySQL but getting syntax error.
I am not able to find the solution, would be grateful for some help
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar
BEGIN
DECLARE title varchar;
if depart_id = 1 then
set title='IT Department';
else if depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END$$
DELIMITER ;
You have several syntax errors in your script:
varchar must have a length
You should define DELIMITER $$ first
It's not else if, but elseif
Try this;)
DELIMITER $$
CREATE FUNCTION `test`.`pro`(depart_id int) RETURNS varchar(10)
BEGIN
DECLARE title varchar(10);
if depart_id = 1 then
set title='IT Department';
elseif depart_id = 2 then
set title='HR Department';
else
set title='Admin';
end if;
return title;
END $$
DELIMITER ;
Related
I CREATED TWO same FUNCTION without the function name
DELIMITER //
CREATE FUNCTION fnc_credit_custstatus(custcredit INT)
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN (custstatus);
END//
DELIMITER ;
and the other one is :
DELIMITER //
CREATE FUNCTION f(custcredit INT)
RETURNS VARCHAR(6) DETERMINISTIC
BEGIN
DECLARE custstatus VARCHAR(6);
IF CustCredit>=1000 THEN SET custstatus='VIP';
ELSEIF CustCredit<1000 THEN SET custstatus='NONVIP';
END IF;
RETURN (custstatus);
END//
DELIMITER ;
This two function are all exists
but when I want to select the same query with different name,
SELECT *,fnc_credit_custstatus(custcredit)AS custstatus FROM customer_salon;
SELECT *, f(custcredit) AS custstatus FROM customer_salon;
BUT the longer name one can't run out
Error Code: 1318
Incorrect number of arguments for FUNCTION database.fnc_credit_custstatus; expected 0, got 1`
I don't know why
I have procedure in MySQL called 'luhn_'.
I would like to use it like function in my other procedure - in IF condition. So it should return me some output parameter (maybe true / false?)
What is the easiest way to implement something like that?
DELIMITER $$
DROP procedure if exists `luhn_`$$
CREATE procedure `luhn_` (IN input_string VARCHAR(256), IN input_lenght INTEGER)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght)
THEN SELECT count(*) from courrier_envoye; -- true
ELSE
SELECT * from courrier_envoye; -- false
END IF;
END$$
-- call luhn_('123456789',10);
if condition
...
if(luhn_('123456789',10) = true) THEN ...
ELSE ...
...
Creating a function would do it for you
DELIMITER $$
CREATE FUNCTION `luhn_`(input_string VARCHAR(256), input_lenght INTEGER) RETURNS tinyint(1)
BEGIN
SET #luhn_string = LEFT(TRIM(input_string), input_lenght);
IF(LENGTH(#luhn_string)=input_lenght) THEN
return true;
ELSE
return false;
END IF;
END
Usage in a stored procedure
if(select luhn_(input_string,input_lenght)) then
select 'a';
else
select 'b';
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.
Please tell me whats wrong with the code. Not able to fine the bug
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you're missing a BEGIN that would match up to your END at the end:
DELIMITER $$
CREATE TRIGGER update_status BEFORE Update ON listing_basic_new_updated
FOR EACH ROW
BEGIN
if new.processing_status is not null
then begin
SET new.rep_status = New.processing_status;
end;
elseif new.televeri_status is not null
then begin
SET new.rep_status = New.televeri_status;
end;
elseif new.verification_status is not null
then begin
SET new.rep_status = New.verification_status;
end;
end if;
END$$
DELIMITER ;
I think you might be able to replace all of it with
SET new.rep_status = COALESCE(new.processing_status, new.televeri_status,
new.verification_status, new.rep_status);
COALESCE: "Returns the first non-NULL value in the list, or NULL if there are no non-NULL values."
I get a error on line 12 (at the endif statement).
I belief I´m doing something wrong within the IF or ELSE, can anyone help me?
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
IF `param` > 0 THEN
SET `var` = `var` + `param`;
END IF;
RETURN `var`;
END$$
EDIT: (same function with case instead of if, same problem)
DELIMITER $$
CREATE FUNCTION TEST (`param` INT)
RETURNS INT
DETERMINISTIC
BEGIN
DECLARE `var` INT;
SET `var` = 1;
SET `var` =
CASE
WHEN `param` > 0 THEN `var` + `param` ELSE `var`
END ;
RETURN `var`;
END$$
Try this instead:
BEGIN
DECLARE `var` INT;
SET var =
CASE
WHEN param > 0 THEN var + 1 ELSE var
END ;
RETURN var;
END$$
Found out it was a bug in PHPMyAdmin, if I added the function with 'add routine' it worked!