I get a #1064 syntax error on the beginning of line 5's if clause when trying to run the following code in PhpMyAdmin:
DELIMITER ;
CREATE DEFINER=`root`#`localhost` FUNCTION `ANREDE`(geschlecht enum('m','w'), vorname VARCHAR(255), nachname VARCHAR(255)) RETURNS varchar(1023) CHARSET latin1
DETERMINISTIC
BEGIN
IF geschlecht = 'm' THEN RETURN CONCAT_WS('',CONCAT_WS(' ','Sehr geehrter Herr',vorname,nachname),',');
ELSE RETURN CONCAT_WS('',CONCAT_WS(' ','Sehr geehrte Frau',vorname,nachname),',');
END IF;
END
It seems to me the if clause is correct - what could cause such an error?
You can either use DELIMITER or you can rewrite your function to make it one-statement function like so
CREATE FUNCTION `ANREDE`
(
geschlecht enum('m','w'),
vorname VARCHAR(255),
nachname VARCHAR(255)
)
RETURNS VARCHAR(1023) CHARSET latin1
DETERMINISTIC
RETURN CONCAT
(CONCAT_WS(' ',
IF(geschlecht = 'm',
'Sehr geehrter Herr',
'Sehr geehrte Frau'),
vorname,
nachname),
',');
Here is SQLFiddle demo.
You need to change your delimiter. Currently, ; is your delimiter, and the parser interprets your ;, at the end of line 5, as the end of your function creation statement.
Try this:
DELIMITER // -- change the delimiter
CREATE ...
BEGIN
IF ... ; -- this is not the end of the statement
...
END
// -- end of statement
DELIMITER ; -- useless if you issue this in PhpMyAdmin, but a good practice nevertheless
Related
I have an issue in my code where i took this : (ApliEMAIL int) in below code but when i execute function it return empty values because in table i took email filed as varchar.
When i write code with this (ApliEMAIL varchar) it does not create function and gives error.
DELIMITER $$
#DROP FUNCTION IF EXISTS `get_appli_name`$$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL int)
RETURNS VARCHAR(255)
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
DELIMITER ;
DELIMITER $$
#DROP FUNCTION IF EXISTS `get_appli_name`$$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar)
RETURNS VARCHAR(255)
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
DELIMITER ;
I tested your function. I got this error.
ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you might want to use the less safe log_bin_trust_function_creators variable)
By the way, in the future, when you are asking for help with an error, include the error message in your post. Help us to help you! Don't make us guess!
Read the documentation about READS SQL DATA here: https://dev.mysql.com/doc/refman/8.0/en/stored-programs-logging.html
So I added that option to your function:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL VARCHAR(255))
RETURNS VARCHAR(255)
READS SQL DATA
BEGIN
DECLARE A_NAME VARCHAR(255) DEFAULT"";
SELECT apli_fname INTO A_NAME
FROM tbl_signup
WHERE apli_email = ApliEMAIL;
RETURN A_NAME;
END$$
It works now:
mysql> select get_appli_name_by_email('thefrog#muppets.org');
+------------------------------------------------+
| get_appli_name_by_email('thefrog#muppets.org') |
+------------------------------------------------+
| Kermit |
+------------------------------------------------+
You are missing the length of the ApliEMAIL-parameter.
Insted of:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar)
you should have:
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar(255))
..or whatever the length of ApliEMAIL is.
You must add deterministic or reads sql data flag.
DELIMITER $$
CREATE FUNCTION get_appli_name_by_email(ApliEMAIL varchar(50))
RETURNS VARCHAR(255)
DETERMINISTIC
BEGIN
RETURN ifnull((SELECT apli_fname FROM tbl_signup WHERE apli_email = ApliEMAIL LIMIT 1), '');
END$$
DELIMITER ;
Define procedure:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
Call Procedure:
CALL SP_Reporting();
Error :
ERROR 1318 (42000): Incorrect number of arguments for PROCEDURE
cds.SP_Reporting ; expected 1, got 0
How pass var by default like SP_Reporting(IN tablename = 'default value' VARCHAR(20))
When you are making your stored procedure, you can assign a value to your input, so there is no need to pass parameters while you are calling the proc.
We usually assign NULL and for making parameters optional, we use this method.
tablename VARCHAR(20) = NULL
Your complete script:
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20) = NULL)
proc_label:BEGIN
IF tablename IS NULL THEN
SELECT 'Null detect';
LEAVE proc_label;
END IF;
SELECT 'after';
END;
$$
DELIMITER ;
EDIT
MySQL is not accepting optional parameters. So one way is to pass NULL value in your stored procedure and check it with IF statement inside your proc.
DELIMITER $$
CREATE PROCEDURE SP_Reporting(IN tablename VARCHAR(20))
proc_label:BEGIN
IF tablename IS NULL THEN
-- Do something;
ELSE
-- Do something else;
END IF;
END;
$$
DELIMITER ;
You have to pass the table name in the procedure call statement like:
CALL SP_Reporting(table_name);
you can't pass default in call statement.
You can assign default value before calling the procedure.
or use OUT instead of IN as a parameter.
you miss the parameter :tablename
you should like this :
call SP_Reporting('any varchar')
or
call SP_Reporting(null)
I get that error when I try to create a procedure. I dont know what's failing and I searched a lot if someone has the same error than me, but usually they mistake at delimiters, and I think i have them right.
"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 ')
SELECT SUM(robatori.quantitat_robada) FROM robatori WHERE param1=jugador_lla' at line 1 "
My query is the next:
DELIMITER //
CREATE PROCEDURE robatoris (IN param1 VARCHAR)
SELECT SUM(robatori.quantitat_robada) FROM robatori WHERE param1=jugador_lladre;
//
DELIMITER ;
Thanks you all, that's my first question here. :)
You are missing the length for input param on type VARCHAR.
Change it like the following:
DELIMITER //
CREATE PROCEDURE robatoris ( IN param1 VARCHAR(255) )
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
//
DELIMITER ;
As you have only statement to execute, the BEGIN - END block was optional.
But it is advised to practice it in all cases.
DELIMITER //
CREATE PROCEDURE robatoris ( IN param1 VARCHAR(255) )
BEGIN
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
END;
//
DELIMITER ;
Add BEGIN and END. Use TEXT type:
DELIMITER //
CREATE PROCEDURE robatoris (IN param1 TEXT)
BEGIN
SELECT SUM(robatori.quantitat_robada)
FROM robatori
WHERE param1=jugador_lladre;
END//
DELIMITER ;
Could anyone help me to solve issues with converting MsSQL to MySQL? It gives a syntax error.
Original Mssql function:
CREATE FUNCTION StrToNum (srt1 varchar(250))
RETURNS real AS
BEGIN
DECLARE t real
IF srt1 IS NOT NULL and ISNUMERIC(srt1)=1 and PATINDEX('%,%',srt1)=0 and
PATINDEX('%e%',srt1)=0
SET t=CONVERT(Money,srt1)
ELSE
SET t=NULL
RETURN t
END
i tried like this as mysql
DELIMITER $$
CREATE FUNCTION StrToNum (srt1 VARCHAR(250))
RETURNS REAL DETERMINISTIC
BEGIN
DECLARE t REAL;
IF srt1 IS NOT NULL AND srt1 > 0 AND POSITION('%,%' IN srt1=0) AND POSITION('%e%' IN srt1=0)
THEN SET t=CONVERT(Money,INT);
ELSE
THEN SET t=NULL; END IF;
RETURN t;
END IF;
END $$
DELIMITER;
This code should parse:
DELIMITER $$
CREATE FUNCTION StrToNum (srt1 VARCHAR(250))
RETURNS REAL DETERMINISTIC
BEGIN
DECLARE t REAL;
IF srt1 IS NOT NULL AND srt1 > 0
AND POSITION('%,%' IN srt1=0)
AND POSITION('%e%' IN srt1=0)
THEN SET t=CONVERT(srt1,signed); --(1)
ELSE
SET t=NULL; END IF; --(2)
RETURN t;
END $$ --(3)
DELIMITER ; --(4)
I'll break it down for you:
CONVERT() takes SIGNED or UNSIGNED as target types. INT doesn't work. (1)
You brought forward a error from your CONVERT() syntax in mssql; you used Money instead of srt1 (1). The reason for the confusion is that CONVERT() in MSSQL and MySQL have their parameters reversed.
ELSE doesn't need (or accept) a THEN keyword after it (2)
You had an extra END IF before the final END$$ (3)
DELIMITER ; requires a space (4)
I have a function that returns a date string. I need this because I can't use a variable in a view, but I can use a function that returns a variable that I set ahead of time...
So I got all that working, but then I decided that if that I wanted it to return the current date if no date variable was set. I thought the code below wold work, but I get syntax errors...
DELIMITER $$
USE `cc`$$
DROP FUNCTION IF EXISTS `ox_date`$$
CREATE FUNCTION `ox_date`() RETURNS CHAR(50) CHARSET latin1
DECLARE ox VARCHAR(20)
IF #oxdate <1 THEN SET ox = CURDATE$$
ELSE SET ox = #oxdate$$
RETURN ox $$
DELIMITER ;
I tried isnull on that first if, but it wasn't any help.
I'm no expert but here are a few of things I see.
First, you've got
DELIMITER $$
and then use it in the function itself. That DELIMITER line allows you to use the semicolons within the body of the function. Otherwise the ';' would end the CREATE FUNCTION statement prematurely.
Also, the line
DECLARE ox varchar(20)
is missing a semicolon at the end.
And then you're missing the
END IF;
after the else condition.
Also what about the BEGIN END$$ wrapped around the function's definition?
I'd expect a stored function to generally take the form:
DELIMITER $$
DROP FUNCTION IF EXISTS `testdb`.MyFunc$$
CREATE FUNCTION `testdb`.`MyFunc` () RETURNS INT
BEGIN
DECLARE someVar varchar(20);
# some stuff
RETURN something;
END $$
DELIMITER ;
Modifying the guts of the function to suit your needs and setting the return type as appropriate.
Anyway, I'm not an expert but that is what I see and hope that helps.
Why do you need to fiddle with the delimiter? For simple logic prefer the IF function to the IF statement.
CREATE FUNCTION `ox_date`( )
RETURNS CHAR(50) CHARSET latin1
RETURN IF(#oxdate < 1 OR #oxdate IS NULL, CURDATE(),#oxdate)
Else
DELIMITER $$
USE `cc`$$
DROP FUNCTION IF EXISTS `ox_date`$$
CREATE FUNCTION `ox_date`( )
RETURNS CHAR(50) CHARSET latin1
RETURN IF(#oxdate < 1 OR #oxdate IS NULL, CURDATE(),#oxdate)$$
DELIMITER ;