Truncated incorrect DOUBLE error in MySQL - mysql

I wrote a simple mysql procedure as follows:
DELIMITER $$
create procedure trialdbjs.spDeleteEmployees(in IDs nvarchar(1000))
Begin
Delete from employees_js Where ID IN (IDs);
end$$
it works for single values of IDs but when I call the procedure with a comma separated value such as: '2,4'
for example:
call trialdbjs.spDeleteEmployees('2,4');
This returns an error of: Truncated incorrect DOUBLE value: '2,4'
I hope you can help me!
Thanks in advance

You cannot write the stored procedure this way. The problem is IN. It is treating '2,4' as a string value and then trying to convert it to a number (because id is a number).
Here is one way to solve this:
delimiter $$
create procedure trialdbjs.spDeleteEmployees(
in in_IDs nvarchar(1000)
)
Begin
Delete from employees_js Where find_in_set(id, in_IDs) > 0;
end$$
There are other ways to solve this problem (dynamic SQL would be faster because it can make use of an index).

Related

Temporary Procedure define variable issue

I'm trying out MySQL procedures for the first time, however I can't figure out how to define the variable #index_ids for the life of me. It really doesn't like the SET.
CREATE PROCEDURE #indextemp
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END
Problem is in CREATE PROCEDURE syntax, not in setting variable. You just have to add parentheses after procedure name. Here's working sample
delimiter $
CREATE PROCEDURE indextemp()
BEGIN
SET #index_ids = (SELECT DISTINCT index_id FROM visibility_index_processing_queue WHERE process_id IS NOT NULL);
SELECT #index_ids;
END$
delimiter ;
Sometimes use of delimiter character in procedure body can cause problems too. That's why I set delimiter to $ before creating procedure and revert it to default ; after I'm done.
Also notice that I have removed # from your procedure name. In sql # is used to insert comments. If for some reason you really want to use it in your name you have to do it like that
CREATE PROCEDURE `#indextemp`()

MySQL Call procedure inside function for count

Is it possible to use a procedure inside a function? For example, I would like to gather all my rows related to an id but I would also like to count the rows and use it in a select statement. This is not working:
drop procedure if exists relatives;
create procedure relatives(in parent int(11),out counted int(11))
begin
set counted=(select count(*) from category where related=parent);
end;
drop function if exists relatives_count;
create function relatives_count(parent parent(11)) returns int(11)
begin
declare count int(11);
call relatives(parent,counted);
return counted;
end;
So that I can use the count
select relatives_count(id) from category
This is just for curiosity purposes. It may look senseless since I can just call a single select query and get the same results but I want to know how I can use my procedure out variable in a function.
Yes, a MySQL FUNCTION can call a MySQL PROCEDURE.
But... the operations the procedure performs will be limited to the operations allowed by a function. (We can't use a procedure to workaround the limitations placed on a function.)
"is not working" is so nebulously vague as to be practically useless in debugging the issue. What exact behavior is being observed?
My suspicion is that the SQL statements shown are failing, because there is no override for the default statement delimiter.
Also, parent(11) is not a valid datatype.
Be aware that when an identifier for a column in a SQL statement in a MySQL stored program matches an identifier used for an argument or local variable, MySQL follows a rule about which (the column name or the variable) that is being referenced.
Best practice is to adopt a naming convention for arguments and local variables that do not match column names, and to qualify all column references with a table name or table alias.
Personally, I use a prefix for arguments and local variables (a for argument, l for local, followed by a datatype i for integer, d for date/datetime, n for decimal, ...
DELIMITER $$
DROP PROCEDURE IF EXISTS relatives$$
CREATE PROCEDURE relatives(IN ai_parent INT(11),OUT ai_counted INT(11))
BEGIN
SELECT COUNT(*)
INTO ai_counted
FROM category c
WHERE c.related = ai_parent
;
END$$
DROP FUNCTION IF EXISTS relatives_count$$
CREATE FUNCTION relatives_count(ai_parent INT(11))
RETURNS INT(11)
BEGIN
DECLARE li_counted INT(11);
CALL relatives(ai_parent,li_counted);
RETURN li_counted;
END$$
DELIMITER ;
Please identify the exact behavior you observe. Error message when creating the procedure? Error message when executing the function? Unexpected behavior. That's much more precise and informative than telling us something "is not working".

mysql where clause doesn't work

I am writing a stored procedure in mysql which simply returns the row with ID provided or return all table when no ID is provided.
CREATE DEFINER=`root`#`localhost` PROCEDURE `SLICE_GET`(`slice_id` int)
BEGIN
SELECT *
FROM `thesis_db`.`SLICE_INFO`
WHERE (SLICE_ID = `slice_id` OR `slice_id` IS NULL);
END
I have used the same idea in ms-sql for years yet it doesn't seem to work for mysql since no matter which ID is passed, the procedure returns entire table.
What am I missing here ?
This is a way to write procedures in mysql
DELIMITER $$
CREATE PROCEDURE `name of procedure` (x CHAR(1), D1 DATE, D2 DATE)
BEGIN
SELECT name of columns you want to display
FROM table name
WHERE SLICE_ID= x
OR SLICE_ID IS NULL;
END
$$
Note: Moreover mysql is not case sensitive means all caps or all small will not effect it.
delimiter is used to:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises.
By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

MySQL mylfunctions

I have created this code using MySQL within a "mylibrary" database but when I turned it in the professor said that the function had nothing to do with the database and it seemed to be missing some statements. Could some of you take a look at this code and tell me what I am missing in order for the information to work properly?
delimiter $$
adds number of titles to the qty count
create function titleCount_5(number INT,qty INT)
RETURNS INT
BEGIN
RETURN number+number;
END $$
delimiter ;
select titleCount_5(5,8);

Error Code: 1241 Operand should contain 1 column(s) MySQL

I am getting error message 'Operand should contain 1 column(s)' when executes a stored procedure in MySQL.
Following is the stored procedure:
DELIMITER $$
USE `test`$$
DROP PROCEDURE IF EXISTS `test_proc`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN inputMsisdn BIGINT)
BEGIN
IF(CHAR_LENGTH(inputMsisdn)=12, SUBSTR(3,inputMsisdn), inputMsisdn)
THEN
SELECT rmnum FROM testbase WHERE msisdn=inputMsisdn;
END IF;
END$$
DELIMITER ;
Can you please tell me whats the problem within this procedure?
I think the error is on this line
IF(CHAR_LENGTH(inputMsisdn)=12, SUBSTR(3,inputMsisdn), inputMsisdn) but i couldnt tell you why :)
IF(CHAR_LENGTH(inputMsisdn)=12, SUBSTR(3,inputMsisdn), inputMsisdn)
The condition in an IF must be a single scalar, but you have commas so it's trying to treat it as a list of values. Perhaps you meant to use AND where you have commas?