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.
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
DELIMITER //
CREATE FUNCTION fnc_credit_custstatus
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 ;
1 queries executed, 0 success, 1 errors, 0 warnings
Query:
CREATE function fnc_credit_custstatus returns varchar(6) deterministic
begin DECLARE custstatus VARCHAR(6); if CustCredit>='1000...
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 'varchar(6) deterministic
begin
DECLARE custstatus VARCHAR(6);
if CustCredit>=' at line 2
Try remove comma
DELIMITER //
CREATE FUNCTION fnc_credit_custstatus
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 use proper comparision data type)
I am getting a syntax error on executing the below function in MySQL.
CREATE FUNCTION FABC (P_MEMBER_EMAIL VARCHAR(30))
RETURNS int
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END;
The error is as follows "#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 4
Not sure what's causing this. Can anyone help me ?
You need to set a delimiter that will tell MySQL the END of the function definition.
DELIMITER $$
CREATE FUNCTION `FABC`(`P_MEMBER_EMAIL` VARCHAR(30)) RETURNS int(11)
BEGIN
DECLARE RESULT int;
DECLARE V_DATE DATE;
SELECT DOJ+365 INTO V_DATE FROM CHYK_MEMBER_MASTER
WHERE UPPER(MEMBER_EMAIL)=UPPER(P_MEMBER_EMAIL) AND SUBSCRIPTION='Y';
IF V_DATE>SYSDATE THEN
SET RESULT=1;
ELSE
SET RESULT=0;
END IF;
RETURN RESULT;
END$$
DELIMITER ;
I am trying to migrate a scalar valued function of MS SQL to function in MySQL and have been using the following syntax as Queries to create function named xx4.
DROP FUNCTION IF EXISTS `xx4`;`
CREATE DEFINER = CURRENT_USER FUNCTION `xx4`(code VARCHAR(3))`
RETURNS CHAR`
BEGIN
`DECLARE coden INT UNSIGNED DEFAULT 0= CAST(CODE AS UNSIGNED)`
`DECLARE ret CHAR =CASE`
`when coden<50 then 'A'`
`when coden<100 then 'B'`
`when coden<350 then 'C'`
`when coden<360 then 'D'`
`else null`
`END`
`RETURN ret`
`END;
It gives me error as:
at Declare ret char=Case when coden<50 then 'A' when coden<100 w' at line 4
Can you please tell me where I am wrong? Appreciate your help.
I'm not sure why there are so many backticks, but the following syntax works (mind your semicolons):
DROP FUNCTION IF EXISTS xx4;
DELIMITER $$
CREATE DEFINER = CURRENT_USER FUNCTION xx4(`code` VARCHAR(3))
RETURNS CHAR
BEGIN
DECLARE coden INT UNSIGNED DEFAULT 0 = CAST(`CODE` AS UNSIGNED);
DECLARE ret CHAR;
set ret = CASE
when coden<50 then 'A'
when coden<100 then 'B'
when coden<350 then 'C'
when coden<360 then 'D'
else null
END;
RETURN ret;
END;$$
DELIMITER ;
I've written a function but it gives me mistake a the second line (create statement) if anyone could help me, I really appreciate:
CREATE FUNCTION GetPrefix (phone_num VARCHAR(30)) RETURNS varchar(30)
deterministic
BEGIN
DECLARE x INT;
DECLARE prefix varchar(30);
SET x = 0;
for prefix in SELECT code
FROM tab_len
while (length(phone_num)) > 0
do
if prefix<>left(phone_num, length(phone_num)-x)
then set x=x+1 ;
else return 1 ;
END while ;
END $$;
and I receive this 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 'for prefix in SELECT code
FROM tab_len while (length(phone_n' at line 9
DELIMITER $$
DROP FUNCTION IF EXISTS GetPrefix $$
CREATE FUNCTION GetPrefix
(
phone_num VARCHAR(30)
)
RETURNS varchar(30)
BEGIN
DECLARE var_x INT DEFAULT 0;
DECLARE var_prefix VARCHAR(100);
SET phone_num = IFNULL(phone_num,'');
-- your logic will go here.
return phone_num;
END$$
DELIMITER ;
SELECT GetPrefix('test');
This is right syntax to write a function in mysql.
check out the differences. Take a look Here