简体   繁体   English

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

[英]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. 我在MySql中编写了一个存储过程,其中包含一个out变量,但是当我要调用它时,出现以下错误。 Can someone please help me understand what am I doing wrong? 有人可以帮我了解我在做什么错吗? Here is the sp: 这是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) 错误代码:1241。操作数应包含1列

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) : 您不能将SELECT * FROM tab结果分配给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 DBFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 mysql 中使用 CASE function 但出现错误代码:1241 时出现问题。操作数应包含 1 列 - Problem when using CASE function in mysql but getting Error Code: 1241. Operand should contain 1 column(s) sql错误代码:1241。操作数应包含1列 - sql Error Code: 1241. Operand should contain 1 column 插入时出错 - 错误代码 1241。操作数应包含 1 列 - Error on Insert - error code 1241. operand should contain 1 column(s) 错误代码:1241。操作数应包含 1 列不知道如何解决此问题 - Error Code: 1241. Operand should contain 1 column(s) don't know how to fix this mysql调用过程错误代码:1241。操作数应包含1列 - mysql calling procedure Error Code: 1241. Operand should contain 1 column(s) 尝试在mysql的情况下包含“或”-错误代码:1241。操作数应包含1列 - Trying to include an “or” in a case on mysql - Error Code: 1241. Operand should contain 1 column(s) 插入WHERE和AND,错误代码:1241。操作数应包含1列 - Inserting with WHERE and AND, Error Code: 1241. Operand should contain 1 column(s) 错误 #1241 - 操作数应包含 1 列 - Error #1241 - Operand should contain 1 column(s) 错误1241操作数应包含2列 - Error 1241 Operand should contain 2 column(s) MYSQL-错误代码:-1241“运算符应包含1列” - MYSQL - Error Code :- 1241 “Operand should contain 1 column(s) ”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM