I am tring to perform a BITOR on a argument that i am passing into a function. so i wrote the below code. Statement 'SET result = arg1 | arg2 ' isnt working. I tried few ways, however i wasnt able to achieve the right result. What do i need to change ?
DELIMITER $$
CREATE FUNCTION BIT_OR(arg1 varchar(255),arg2 varchar(255)) RETURNS varchar(255)
BEGIN
DECLARE result varchar(255);
BEGIN
SET result = arg1 | arg2;
END;
RETURN result;
END $$
DELIMITER ;
select bitwise_OR(00011101,00001111); -- 12127 ( i am expecting 00011111 or 31 (decimal equivalent))
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 ;
I was told to create a mysql function that, given the artwork title, return the name of the creator. I know it sounds silly, i could use just a select.
I followed some tutorials, but none of them helped me. When I try to create the function, mysql workbench says "Error Code: 1415. Not allowed to return a result set from a function"
Here is what i'm trying to do
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
SELECT result= ar.artistName FROM artist as ar, art_object as oa
where ar.artistCode= oa.artistCode
and oa.title= workName
group by ar.artistName ;
return result;
end$$
delimiter ;
That's not how you assign the result of a variable in a SELECT statement. You have to use SELECT ... INTO variable.
You also need to declare the variable first.
delimiter $$
create function findObject(workName VARCHAR(45)) returns VARCHAR(250)
begin
DECLARE result VARCHAR(250);
SELECT DISTINCT ar.artistName FROM artist as ar, art_object as oa
INTO result
where ar.artistCode= oa.artistCode
and oa.title= workName;
return result;
end$$
delimiter ;
SELECT result = ar.artistName is comparing the value of result with ar.artistName, and returning the result of that comparison from the SELECT statement.
I have a question about the stored procedure:
Create a stored procedure named format_currency that accepts a character and a double number. It will return a VARCHAR(32) with the symbol in the front, followed by the number to 2 decimal places.
For example, format_currency('$', 123.4) should return $123.40
Here is my code:
CREATE OR REPLACE PROCEDURE format_currency(IN c VARCHAR2, IN n DOUBLE(9,2))
AS
BEGIN
SELECT CONCAT(c,',' n);
END;
It's not working, I have no idea how to write codes inside BEGIN and END.
Many Thanks for your help.
Henry
Instead of procedure I suggest to use function:
Code:
DELIMITER $$
DROP FUNCTION IF EXISTS test_f$$
CREATE FUNCTION test_f(a varchar(22),b double(9,2)) returns varchar(64)
BEGIN
SET #RET = (select concat(a,b));
return #RET;
END$$
DELIMITER ;
Test Run:
mysql> select test_f('$',123);
+-----------------+
| test_f('$',123) |
+-----------------+
| $123.00 |
+-----------------+
1 row in set (0.10 sec)
mysql>
DELIMITER\\
CREATE PROCEDURE format_currency (a CHAR, b DOUBLE(9,2))
BEGIN
declare ret VARCHAR(32);
set #ret = (SELECT CONCAT(a,b));
select #ret;
END
DELIMITER\\
call format_currency('$',123);
I had a similar question and nothing the other people answered worked for me in MySQL. Here is a working solution with 2 parameters as "SIGN" and "MONEY" and creating a variable called "NEWRESULT" that you delcare, set as 0, and then set as the concat of "SIGN" and "MONEY".
DELIMITER //
CREATE PROCEDURE format_currency`enter code here`
(IN SIGN char(1),IN MONEY double(9,2))
BEGIN
declare NEWRESULT char(8);
set NEWRESULT=0;
SET NEWRESULT=(SELECT CONCAT(SIGN,MONEY));
SELECT NEWRESULT;
END//
DELIMITER ;
I'm learning stored procedures/functions/triggers in MySQL this morning and I'm having some problems trying to use variable table and column names in queries.
DELIMITER |
USE R2R |
DROP FUNCTION IF EXISTS getCategoryName |
CREATE FUNCTION getCategoryName(LMID INT, levelNum INT)
RETURNS VARCHAR(255)
BEGIN
DECLARE levelName VARCHAR(255) DEFAULT makeLevelName(levelNum);
DECLARE levelID INT;
DECLARE levelNameID VARCHAR(255) DEFAULT CONCAT(levelName, 'ID');
DECLARE ret VARCHAR(255);
SELECT #levelNameID INTO levelID FROM LevelMaster WHERE id=LMID;
SELECT description INTO ret FROM #levelName WHERE id=levelID;
RETURN ret;
END;
|
DROP FUNCTION IF EXISTS makeLevelName |
CREATE FUNCTION makeLevelName(levelNum INT)
RETURNS VARCHAR(255)
BEGIN
DECLARE word VARCHAR(255);
DECLARE ret VARCHAR(255);
IF levelNum=2 THEN SET word='Two';
ELSEIF levelNum=3 THEN SET word='Three';
ELSEIF levelNum=4 THEN SET word='Four';
ELSEIF levelNum=5 THEN SET word='Five';
END IF;
SET ret=CONCAT('Level', word);
RETURN ret;
END;
|
SELECT getCategoryName(347, 2) |
It's the first function (getCategoryName) that's causing me the problems, I need the two variables marked with # to be the table/column names - these two lines:
SELECT #levelNameID INTO levelID FROM LevelMaster WHERE id=LMID;
SELECT description INTO ret FROM #levelName WHERE id=levelID;
I want to keep this function as a function rather than a procedure if possible, but would accept answers for a procedure if it's the only way.
Thanks for you help,
Richard
Use User/Global Vars for this along with PREPARE & EXECUTE:
SET #columnName='myColumn';
SET #tableName='myTable';
SET #whatEver='requiredValue';
SET #query=CONCAT('SELECT ', #columnName, ' FROM ', #tableName, ' WHERE Column=', #whatEver);
PREPARE QUERY FROM #QUERY;
EXECUTE QUERY;
Haven't tested this EXACT code but something along these lines will work. Also has to be inside a Procedure, cannot be used with a function or trigger, if anyone has a soloution for that then please post.
I want to create a function with optional arguments in MySQL. For instance, I want to create function that calculates the average of its arguments. I create a function of five arguments, but when user passes just two arguments to the function then it should still run and return the average of the two arguments.
You cannot set optional parameters in MySQL stored procedures.
You can however set optional parameters in a MySQL UDF.
You do know that MySQL has an AVG aggregate function?
Workaround
If you can face the ugliness of this workaround here's samplecode that uses a comma separated string with values as input and returns the average.
DELIMITER $$
CREATE FUNCTION MyAvg(valuestr varchar) RETURNS float
BEGIN
DECLARE output float;
DECLARE arg_count integer;
DECLARE str_length integer;
DECLARE arg float;
DECLARE i integer;
SET output = NULL;
SET i = LENGTH(valuestr);
IF i > 0 THEN BEGIN
SET arg_count = 1;
WHILE i > 0 DO BEGIN
IF MID(valuestr, i, 1)
SET i = i - 1;
END; END WHILE;
/* calculate average */
SET output = 0;
SET i = arg_count;
WHILE i > 0 DO BEGIN
SET arg = SUBSTRING_INDEX(
SUBSTRING_INDEX(valuestr, ',' , i)
, ',', -1 );
SET output = output + arg;
SET i = i - 1;
END; END WHILE;
SET output = output / arg_count;
END; END IF;
RETURN output;
END $$
DELIMITER ;
Use concat_ws to feed the function.
SELECT MyAvg(CONCAT_WS(',',100,200,300,500)) AS test;
You can also write an UDF in C(++) or Delphi/Lazarus
While far from an ideal solution, here's how I solved optional parameters for a concat function I needed:
delimiter ||
create function safeConcat2(arg1 longtext, arg2 varchar(1023))
returns longtext
return safeConcat3(arg1, arg2, '');
||
create function safeConcat3(arg1 longtext, arg2 varchar(1023), arg3 varchar(1023))
returns longtext
return safeConcat4(arg1, arg2, arg3, '');
||
create function safeConcat4(arg1 longtext, arg2 varchar(1023), arg3 varchar(1023), arg4 varchar(1023))
returns longtext
begin
declare result longText;
set result = concat(arg1, arg2, arg3, arg4);
if( result is null) then
set result=arg1;
end if;
return result;
end
||
Note: This means you have to call the method that corresponds to the number of args.
Another approach is to pass only one 'super' parameter which is string with commas in it separating the real parameters. The mysql procedure can then parse the 'super' parameter into the separate real parameters.
Example:
create procedure procWithOneSuperParam(param1 varchar(500))
declare param2 varchar(100);
begin
if LOCATE(',',param1) > 0 then
.. param2=<extract the string after the ',' from param1> ..