Error Code: 1241 Operand should contain 1 column(s) MySQL - 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?

Related

Create a simple stored procedure IN mysql

I'm trying create a simple stored procedure in mysql, In this stored procedure I'm trying to call a view and page it.
delimiter //
CREATE PROCEDURE SelectSearchResultsContract (start int, quantity int)
BEGIN
select
searchresultsdisplayview.CompanyName,
searchresultsdisplayview.LastChanceDate,
searchresultsdisplayview.PhoneNumber,
searchresultsdisplayview.ContactName,
searchresultsdisplayview.City,
searchresultsdisplayview.State
FROM searchresultsdisplayview -- this is a view
OFFSET start
LIMIT quantity ;
END
//
delimiter ;
I cannot create this because of the syntax. says I'm missing a simicolon.
I have create many that we like this using tables but the view doesnt like it. Can someone please tell me what I am missing.
EXACT ERROR:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'start LIMIT quantity' at line 15
I Figured this out by right clicking and adding stored procedure then copy and paste the sql that it displayed. below is anwser:
DELIMITER $$
USE `construction_bid_source`$$
CREATE PROCEDURE `SelectSearchResultsContract` (quantity int, start int)
BEGIN
select
searchresultsdisplayview.CompanyName,
searchresultsdisplayview.LastChanceDate,
searchresultsdisplayview.PhoneNumber,
searchresultsdisplayview.ContactName,
searchresultsdisplayview.City,
searchresultsdisplayview.State
FROM searchresultsdisplayview
LIMIT quantity-- this is a view
OFFSET start;
END$$
DELIMITER ;

How to handle one Stored Procedure output (ResultSet or Table) into another Stored Procedure as Table

one Stored Procedure is returning me a table and I want to hold that result into another Stored Procedure.
and I am facing SQL syntax error.
First Stored Procedure
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnTable`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnTable`()
BEGIN
SELECT `user_id`, `email` FROM `users`;
END$$
DELIMITER ;
and I am trying to call this Store Procedure into another and want to hold the data into a view or table
DELIMITER $$
USE `dataBase`$$
DROP PROCEDURE IF EXISTS `testReturnCall`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `testReturnCall`()
BEGIN
DROP VIEW IF EXISTS `my_view`;
CREATE OR REPLACE VIEW my_view AS (CALL testReturnTable());
SELECT * FROM my_view;
END$$
DELIMITER ;
and getting 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 'CALL testReturnTable());
I can use function here as well, but I don't know how to handle result set,
please advice.
it is not possible to get table ouput from one stored procedure to another stored procedure. But we have alternate to do this which is Temp table

Truncated incorrect DOUBLE error in 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).

Error when creating stored procedure to view data in MYSQL

I am having a lot of trouble with the syntax of this stored procedure that is supposed to return All information from a table called Country and is trying to use a parameter for comparison:
Create Procedure CountrybyPK (country char(50))
begin
Select * From Country
Where Country.Name=country
end;
The error I get states:
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 'end' at line 5
You need a statement delimiter between the SELECT statement and END.
Create Procedure CountrybyPK (country char(50))
begin
Select * From Country
Where Country.Name=country;
end;
But since the delimiter would also terminate the whole procedure, you need to use the DELIMITER command to switch to another delimiter:
DELIMITER $$
Create Procedure CountrybyPK (country char(50))
begin
Select * From Country
Where Country.Name=country;
end$$
But since your procedure only has one statement in it, you don't need begin and end at all, you can just do:
Create Procedure CountrybyPK (country char(50))
Select * From Country
Where Country.Name=country;

Cannot create stored procedure mysql syntax error

I'm experiencing a serious headache while trying to create a stored procedure.
DELIMITER $$
create PROCEDURE insertDummyUser(rank int unsigned)
BEGIN
INSERT INTO tbl (name, rank) VALUES ('DummyUser', rank);
END$$
It gives me the following 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 '' at line 5
I just cant wrap my head around this. It looks fine to me.
I'm just starting out with these stored procedures, so any help at all is greatly appreciated :)
The following stored procedure runs smoothly:
DELIMITER $$
CREATE PROCEDURE `insertDummyUser`(`p_rank` INT UNSIGNED)
BEGIN
INSERT INTO `tbl` (`name`, `rank`) VALUES ('DummyUser', `p_rank`);
END$$
DELIMITER ;