简体   繁体   中英

Using a sp with an out variable get the following error message: Error Code: 1241. Operand should contain 1 column(s)

I wrote a stored procedure in MySql that contains an out variable but when I want to call it, I get the below error. Can someone please help me understand what am I doing wrong? Here is the sp:

CREATE DEFINER=`root`@`localhost` PROCEDURE `storedp2`(out shift nvarchar(40))
begin
set shift= (SELECT * FROM myblog.computed);
end

and here is how I call it:

set @test='';
call storedp2 (@test) ;
select @test as t;

and here is error:

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

You need to return single value:

CREATE DEFINER=`root`@`localhost` PROCEDURE `storedp2`(out shift nvarchar(40))
begin
set shift= (SELECT col_name FROM myblog.computed WHERE id = ?);
-- (single column/single row)
-- set shift = (SELECT col_name FROM myblog.computed WHERE ... LIMIT 1);
end;

You cannot assign result of SELECT * FROM tab to NVARCHAR(40) :

scalar :=  (col1, col2, col3)         -- won't work (multiple cols, single row)
scalar :=  (col1, col2), (col1, col2) -- won't work (multiple cols, multiple rows)
sclara :=  (col1), (col1)             -- won't work (single col, multiple rows)

EDIT:

what should I do if I want to return the whole select sentance

CREATE DEFINER=`root`@`localhost` PROCEDURE `storedp2`()
begin
  -- some logic
  SELECT * FROM myblog.computed;
end

call storedp2 ();

DBFiddle Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM