Error Syntax on Mysql Function - mysql

Would like to know what is the error on this syntax since all the sample given is similar with the 1 i try to create
CREATE FUNCTION test(regionCode varchar(5)) RETURNS CHAR(50)
DETERMINISTIC
READ SQL DATA
BEGIN
DECLARE NAME_FOUND CHAR(50);
SELECT 'abc' into NAME_FOUND from dual;
RETURN NAME_FOUND;
END
Thanks for the help.

mysql Functin having only READS SQL DATA not READ SQL DATA,that is the mistake you have done in your function.Please find the link for more details:
https://dev.mysql.com/doc/refman/5.7/en/stored-programs-logging.html
READS SQL DATA explicitly tells to MySQL that the function will ONLY read data from databases, thus, it does not contain instructions that modify data, but it contains SQL instructions that read data (e.q. SELECT).
DELIMITER $$
CREATE FUNCTION test(regionCode varchar(5)) RETURNS CHAR(50)
DETERMINISTIC
READS SQL DATA
BEGIN
DECLARE NAME_FOUND CHAR(50);
SELECT 'abc' into NAME_FOUND from dual;
RETURN NAME_FOUND;
END

CREATE FUNCTION test(regionCode varchar(5)) RETURNS CHAR(50)
READS SQL DATA
DETERMINISTIC
BEGIN
DECLARE NAME_FOUND CHAR(50);
SELECT 'abc' into NAME_FOUND from dual;
RETURN NAME_FOUND;
END
TRY THIS QUERY

Related

How to set varchar datatype in function?

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 ;

Mysql stored function freezing

I have a stored function in MySQL and it works partially.
DELIMITER $$
DROP FUNCTION IF EXISTS `getsubdomain`$$
CREATE FUNCTION getsubdomain(page_id int(11))
RETURNS CHAR(255)
DETERMINISTIC
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
declare current_p_id int(11);
declare current_p_parent_id int(11);
declare current_p_address_type char(255);
declare current_p_adress char(255);
SET current_p_id = page_id;
WHILE (current_p_id) <> 0
DO
select p_parent_id, p_address_type, p_adress from opu_pages where p_id = current_p_id into current_p_parent_id, current_p_address_type, current_p_adress;
IF current_p_address_type <> ''
THEN
IF current_p_address_type = 'subdomain'
THEN
RETURN current_p_adress;
ELSE
SET current_p_id = current_p_parent_id;
END IF;
ELSE
RETURN NULL;
END IF;
END WHILE;
RETURN NULL;
END$$
DELIMITER ;
If I call in query SELECT getsubdomain(p_id) FROM opu_pages; it works Ok. But if I call it in SELECT * FROM opu_pages WHERE getsubdomain(p_id)='library'; the database is collapsed and freezing.
Query and function work with one table.
What did I do wrong?
I thought that it can be caused by the table format MyISAM. But I can't change it to InnoDB because I use FULLTEXTFORMAT fields in this table.
Table opu_pages (MyISAM) scheme
p_id INT
p_parent_id INT
p_address_type ENUM (path, subdomain)
p_adress VARCHAR
Based on your post I would say that your code is entering an infinite loop for some of your input parameters.
In particular the case where p_id = p_parent_id in the opu_pages table and the current_p_address_type = 'subdomain'

Can't create stored function - wrong syntax?

I've got the following Problem.
I want to create a stored function which converts an entity_id into the corresponding sku
Here's the code:
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
return returnvalue;
END
Now my problem is if i fire the query i get the following message:
[Window Title]
Error
SQL 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 'LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT' at line 3
The db im using is MySQL 5.0.51a
Thanks in advance for your ideas.
MySQL by default uses ; as a delimiter, so when it encounters the ; at line 9:
DECLARE returnvalue varchar(50);
MySQL thinks the command ends - it is trying to execute:
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
which isn't valid SQL.
Set a new delimiter:
DELIMITER $$
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue varchar(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
return returnvalue;
END
$$
You can then change the delimiter back with:
DELIMITER ;
This should work:
DELIMITER $$
CREATE FUNCTION `id2sku`(`entity_id_in` INT)
RETURNS VARCHAR(50)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
DECLARE returnvalue VARCHAR(50);
SELECT `sku` INTO returnvalue FROM catalog_product_entity WHERE entity_id = entity_id_in LIMIT 1;
RETURN returnvalue;
END$$
DELIMITER ;
You had not specified the varchar length. :-)

setting a single row to variable in mysql

I am not a MYSQL developer, but just had to write some code. Please accept my apologies if I am doing anything silly in my code.
My need is to get a single row like select * from users where userID = 1 limit 1 and assign it to a variable and access columns for doing some calculation. Firstly, is this possible?
I have tried to go through step by step, that is why I wrote a simple function like below
DELIMITER $$
CREATE DEFINER=`user`#`%` FUNCTION `GetReportees`(userid VARCHAR(255)) RETURNS varchar(50) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE Var1 varchar(120);
DECLARE Var2 varchar(120);
Select #Var1=forename, #Var2=surname from company_users.users where userID = #userid limit 1;
return #Var1;
END
when I try to save this function, it says that ERROR 1415: Not allowed to return a result set from a function. But I clearly return a varchar variable.
Could anyone tell me what I am doing wrong? it should not be this much hard, I believe.
Many thanks
Regards
You should read the documentation on the difference between user-defined variables and local variables.
In your example, you have a parameter and 2 local variables, so you should use them like this:
DELIMITER $$
CREATE DEFINER=`user`#`%` FUNCTION `GetReportees`(p_userid VARCHAR(255))
RETURNS varchar(120) CHARSET latin1
DETERMINISTIC
BEGIN
DECLARE Var1 varchar(120);
DECLARE Var2 varchar(120);
Select forename, surname
into Var1,Var2
from company_users.users
where userID = p_userid
limit 1;
return Var1;
END

Convert SQL Server stored procedure to MySQL

I need to convert the following stored procedure of SQL Server to MySQL. I am new to MySQL.
CREATE PROC InsertGenerator
(#tableName varchar(100)) as
--Declare a cursor to retrieve column specific information
--for the specified table
DECLARE cursCol CURSOR FAST_FORWARD FOR
SELECT column_name,data_type FROM information_schema.columns
WHERE table_name = #tableName
OPEN cursCol
DECLARE #string nvarchar(3000)
--for storing the first half
--of INSERT statement
DECLARE #stringData nvarchar(3000)
--for storing the data
--(VALUES) related statement
DECLARE #dataType nvarchar(1000) --data types returned
--for respective columns
SET #string='INSERT '+#tableName+'('
SET #stringData=''
DECLARE #colName nvarchar(50)
FETCH NEXT FROM cursCol INTO #colName,#dataType
IF ##fetch_status<>0
begin
print 'Table '+#tableName+' not found, processing skipped.'
close curscol
deallocate curscol
return
END
WHILE ##FETCH_STATUS=0
BEGIN
IF #dataType in ('varchar','char','nchar','nvarchar')
BEGIN
SET #stringData=#stringData+'''''''''+
isnull('+#colName+','''')+'''''',''+'
END
ELSE
if #dataType in ('text','ntext')
--if the datatype
--is text or something else
BEGIN
SET #stringData=#stringData+'''''''''+
isnull(cast('+#colName+' as varchar(2000)),'''')+'''''',''+'
END
ELSE
IF #dataType = 'money' --because money doesn't get converted
--from varchar implicitly
BEGIN
SET #stringData=#stringData+'''convert(money,''''''+
isnull(cast('+#colName+' as varchar(200)),''0.0000'')+''''''),''+'
END
ELSE
IF #dataType='datetime'
BEGIN
SET #stringData=#stringData+'''convert(datetime,''''''+
isnull(cast('+#colName+' as varchar(200)),''0'')+''''''),''+'
END
ELSE
IF #dataType='image'
BEGIN
SET #stringData=#stringData+'''''''''+
isnull(cast(convert(varbinary,'+#colName+')
as varchar(6)),''0'')+'''''',''+'
END
ELSE
--presuming the data type is int,bit,numeric,decimal
BEGIN
SET #stringData=#stringData+'''''''''+
isnull(cast('+#colName+' as varchar(200)),''0'')+'''''',''+'
END
SET #string=#string+#colName+','
FETCH NEXT FROM cursCol INTO #colName,#dataType
END
http://dev.mysql.com/doc/refman/5.0/en/stored-routines-syntax.html
This link contains the documentation on creating stored procedures and functions in MySql. You have quite a lot of code there so it may come down to 6 of 1 and half dozen of the other.
You can convert it to MySQL by using the following steps.
In SQL Server the parameters of the stored procedure are declared as
For Example:
CREATE PROCEDURE Strproc_example(
#strBillingPeriodID VarChar(200),
#result int OUT)
In MySQL it is
CREATE PROCEDURE Strproc_example(
v_strBillingPeriodID VarChar(200),
OUT v_result int )
In SQL server there is the As keyword. Remove that in MySQL
Then put a semicolon after in statement of SQL Server to convert it
into MySQL statements.