简体   繁体   English

mysql存储过程:out参数

[英]mysql stored-procedure: out parameter

I have a mysql stored procedure from this ( google book ), and one example is this:我有一个 mysql 存储过程来自这个( 谷歌书),一个例子是这样的:

DELIMITER $$

DROP PROCEDURE IF EXISTS my_sqrt$$
CREATE PROCEDURE my_sqrt(input_number INT, OUT out_number FLOAT)
BEGIN
    SET out_number=SQRT(input_number);
END$$

DELIMITER ;

The procedure compiles fine.该程序编译正常。 (I am using MySQL Query Browser in ubuntu). (我在 ubuntu 中使用 MySQL 查询浏览器)。

However when I call the procedure:但是,当我调用程序时:

CALL my_sqrt(4,@out_value);

(also in query browser) (也在查询浏览器中)

It returns an error:它返回一个错误:

(1064) check the manual that correspond to the...

Why isn't this example working?为什么这个例子不起作用?

Unable to replicate.无法复制。 It worked fine for me:它对我来说很好用:

mysql> CALL my_sqrt(4, @out_value);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @out_value;
+------------+
| @out_value |
+------------+
| 2          | 
+------------+
1 row in set (0.00 sec)

Perhaps you should paste the entire error message instead of summarizing it.也许您应该粘贴整个错误消息而不是对其进行总结。

You must have use correct signature for input parameter *IN is missing in the below code.您必须为输入参数使用正确的签名 *IN 在以下代码中丢失。

CREATE PROCEDURE my_sqrt(IN input_number INT, OUT out_number FLOAT)

I just tried to call a function in terminal rather then MySQL Query Browser and it works.我只是尝试在终端而不是 MySQL Query Browser 中调用一个函数并且它有效。 So, it looks like I'm doing something wrong in that program...所以,看起来我在那个程序中做错了什么......

I don't know what since I called some procedures before successfully (but there where no out parameters)...我不知道是什么,因为我之前成功调用了一些程序(但那里没有输出参数)......

For this one I had entered对于这个我已经输入

CALL my_sqrt(4,@out_value);
SELECT @out_value;

And it results with an error:结果出现错误:

You have an error in your SQL syntax;您的 SQL 语法有误; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SELECT @out_value' at line 2检查与您的 MySQL 服务器版本对应的手册,了解在第 2 行的“SELECT @out_value”附近使用的正确语法

Strangely, if I write just:奇怪的是,如果我只写:

CALL my_sqrt(4,@out_value); 

The result message is: "Query canceled"结果消息是:“查询已取消”

I guess, for now I will use only terminal...我想,现在我只会使用终端......

I know this is an old thread, but if anyone is looking for an answer of why their procedures doesn't work in the workbench and think the only result is "Query canceled" or anything like that without clues:我知道这是一个旧线程,但是如果有人正在寻找为什么他们的程序在工作台中不起作用的答案并且认为唯一的结果是“查询已取消”或类似的没有线索的东西:

the output with errors or problems is hiddenl.有错误或问题的输出是隐藏的。 I do not know why, I do understand it's annoying, but it is there.我不知道为什么,我知道这很烦人,但它就在那里。 just move your cursor above the line above the message, it will turn in an double arrow (up and down) you can then click and drag that line up, then you will see a console with the message you missed!只需将光标移动到消息上方的行上方,它将变成双箭头(向上和向下),然后您可以单击并向上拖动该行,然后您将看到一个控制台,其中包含您错过的消息!

If you are calling from within Stored Procedure don't use @.如果您从存储过程中调用,请不要使用@。 In my case it returns 0在我的例子中它返回 0

CALL SP_NAME(L_OUTPUT_PARAM) CALL SP_NAME(L_OUTPUT_PARAM)

SET out_number=SQRT(input_number);

Instead of this write:而不是这样写:

select SQRT(input_number); 

Please don't write SET out_number and your input parameter should be:请不要写SET out_number并且您的输入参数应该是:

PROCEDURE `test`.`my_sqrt`(IN input_number INT, OUT out_number FLOAT) 

try changing OUT to INOUT for your out_number parameter definition.尝试为您的out_number参数定义将OUT更改为INOUT

CREATE PROCEDURE my_sqrt(input_number INT, INOUT out_number FLOAT)

INOUT means that the input variable for out_number ( @out_value in your case.) will also serve as the output variable from which you can select the value from. INOUT表示@out_value的输入变量(在您的情况下为out_number 。)也将用作输出变量,您可以从中选择值。

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

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