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
Related
I use the example below :
select '10+20+30'
and generate value :
'++'
In other words, strip any digit and leave only signs.
Function:
DROP FUNCTION IF EXISTS getMOps;
DELIMITER $$
CREATE FUNCTION getMOps
( s varchar(100)
)
RETURNS VARCHAR(100) DETERMINISTIC
BEGIN
DECLARE sOut VARCHAR(100);
DECLARE theLen,iLooper INT;
SET iLooper=1;
SET theLen=LENGTH(s);
SET sOut='';
WHILE iLooper<theLen DO
IF SUBSTR(s,iLooper,1) IN ('+','-','*','/','^') THEN
SET sOut=CONCAT(sOut,SUBSTR(s,iLooper,1));
END IF;
SET iLooper=iLooper+1;
END WHILE;
return (sOut);
END$$
DELIMITER ;
Test:
select getMOps('fish'); -- blank string
select getMOps('1+2+7-1'); -- '++-'
Modify the IN clause to suit your tastes. I am sure there are better ways.
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 ;
I am bit confused with Mysql syntax. I want to check for NULL the value of ExtractValue(xml, '//order[1]/quantity[$#i]') function. It can be assign to variable or this action can be skipped. I tried this and there is syntax error:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
SET xml = '';
DECLARE test VARCHAR(1000);
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
IF (test IS NULL) THEN SELECT 1; END IF;
END$$
DELIMITER ;
CALL sp_test_for_null;
I'd try this (note that all DECLAREs are at the beginning:
DELIMITER $$
DROP PROCEDURE IF EXISTS `sp_test_for_null`$$
CREATE PROCEDURE `sp_test_for_null`()
BEGIN
DECLARE xml VARCHAR(1000);
DECLARE test VARCHAR(1000);
SET xml = '';
SET test = (SELECT ExtractValue(xml, '//order[1]/quantity[$#i]');
SELECT ISNULL(test, 1, 0);
END$$
DELIMITER ;
CALL sp_test_for_null;
I have been trying to validate a user through his username and password through a mySql procedure
delimiter //
create function validate_user(username_1,password_1)
return char(1)
DETERMINISTIC
begin
declare ret char(1);
if exists(select * from logintable where userid=username_1 and password=password_1) then
set ret='1';
endif;
return (ret);
end;//
delimiter ;
But i've been getting error #1064 error in sql syntax.
line no 3 in your code
return char(1)
should be
returns char(1)
A few things;
You're not declaring a type for your parameters.
RETURN should be RETURNS in the function declaration
ENDIF should be END IF
In other words;
create function validate_user(username_1 VARCHAR(32), password_1 VARCHAR(32))
returns char(1)
DETERMINISTIC
begin
declare ret char(1);
if exists(select * from logintable where userid=username_1 and password=password_1) then
set ret='1';
end if;
return (ret);
end;//
An SQLfiddle with all corrections.
I have been sitting with a stored procedure for MySQL for days now, it just won't work, so I thought I'd go back to basic and do a very simple function that checks if an item exists or not.
The problem I had on the first one was that it said END IF is invalid syntax on one of my IF clauses, but not the other two. The second one won't even recognize BEGIN as valid syntax...
Is it I that got everything wrong, or have I stumbled upon a MYSQL Workbench bug? I have Workbench 5.2 (latest version when I'm writing this) and this is the code:
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
to fix the "begin" syntax error, you have to declare a return value, like this:
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT(11)
after doing that, Workbench won't return an error anymore ;o)
You have to specify the return value in signature as well delimiter at the end is missing. So, your function should look like
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT) RETURNS INT
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER $$
CREATE FUNCTION `filmsidan`.`f_lateornot` (movie_id INT)
BEGIN
DECLARE check_val INT;
DECLARE return_val INT;
SELECT stockId
FROM orders
WHERE stockId = movie_id
INTO check_val;
IF check_val <= 0
THEN
SET return_val = 1;
ELSE
SET return_val = 0;
END IF;
RETURN return_val;
END
$$
DELIMITER ;
Add this last thing it works :
$$
DELIMITER ;
it means you are using ( ; ) this in function so for that reason we use it..see
and see also
MySQL - Trouble with creating user defined function (UDF)