使用带有out变量的sp得到以下错误消息:错误代码:1241操作数应包含1列

k7fdbhmy  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(273)

我在mysql中编写了一个存储过程,其中包含一个out变量,但是当我想调用它时,我得到以下错误。有人能帮我理解我做错了什么吗?这是sp:

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

我是这样称呼它的:

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

这是一个错误:
错误代码:1241。操作数应包含1列

luaexgnf

luaexgnf1#

您需要返回单个值:

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;

不能指定的结果 SELECT * FROM tabNVARCHAR(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)

编辑:
如果我想把整句话都还回去,我该怎么办

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

call storedp2 ();

dbfiddle演示

相关问题