gpt4 book ai didi

mysql - 在MySql中,定义存储过程时如何使用 'REPLACE'函数?由于引号,查询无法执行

转载 作者:行者123 更新时间:2023-11-29 13:30:07 26 4
gpt4 key购买 nike

我正在尝试运行以下存储过程。

CREATE PROCEDURE RNS_CLEANTEXT_MAX_LENGTH()
BEGIN
SELECT MAX(LENGTH(rns_cleantext) - LENGTH(REPLACE(rns_cleantext," ", ""))+1) FROM rns;
END

但是,当我收到消息时,此查询不会运行:

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 3

我还尝试过单引号、转义引号和如上所述的双引号。

任何想法。

由@eggyal修复:

固定查询现在如下所示:

CREATE FUNCTION RNS_CLEANTEXT_MAX_LENGTH()
SELECT MAX(LENGTH(rns_cleantext) - LENGTH(REPLACE(rns_cleantext," ", ""))+1) FROM fns;

正如 @eggyal 指出的,Begin 和 End 语句不是必需的。在这种情况下,我必须修改分隔符。

谢谢!

最佳答案

Defining Stored Programs 下所述:

Each stored program contains a body that consists of an SQL statement. This statement may be a compound statement made up of several statements separated by semicolon (;) characters. For example, the following stored procedure has a body made up of a BEGIN ... END block that contains a SET statement and a REPEAT loop that itself contains another SET statement:

CREATE PROCEDURE dorepeat(p1 INT)
BEGIN
SET @x = 0;
REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
END;

If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.

To redefine the mysql delimiter, use the delimiter command. The following example shows how to do this for the dorepeat() procedure just shown. The delimiter is changed to // to enable the entire definition to be passed to the server as a single statement, and then restored to ; before invoking the procedure. This enables the ; delimiter used in the procedure body to be passed through to the server rather than being interpreted by mysql itself.

mysql> <strong>delimiter //</strong>mysql> <strong>CREATE PROCEDURE dorepeat(p1 INT)</strong>    -> <strong>BEGIN</strong>    -> <strong>  SET @x = 0;</strong>    -> <strong>  REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;</strong>    -> <strong>END</strong>    -> <strong>//</strong>Query OK, 0 rows affected (0.00 sec)mysql> <strong>delimiter ;</strong>mysql> <strong>CALL dorepeat(1000);</strong>Query OK, 0 rows affected (0.00 sec)mysql> <strong>SELECT @x;</strong>+------+| @x   |+------+| 1001 |+------+1 row in set (0.00 sec)

You can redefine the delimiter to a string other than //, and the delimiter can consist of a single character or multiple characters. You should avoid the use of the backslash (“\”) character because that is the escape character for MySQL.

The following is an example of a function that takes a parameter, performs an operation using an SQL function, and returns the result. In this case, it is unnecessary to use delimiter because the function definition contains no internal ; statement delimiters:

mysql> <strong>CREATE FUNCTION hello (s CHAR(20))</strong>mysql> <strong>RETURNS CHAR(50) DETERMINISTIC</strong>    -> <strong>RETURN CONCAT('Hello, ',s,'!');</strong>Query OK, 0 rows affected (0.00 sec)mysql> <strong>SELECT hello('world');</strong>+----------------+| hello('world') |+----------------+| Hello, world!  |+----------------+1 row in set (0.00 sec)

在您的情况下,由于您的存储过程仅包含一条语句,因此您可以简单地删除 BEGIN ... END block 。

关于mysql - 在MySql中,定义存储过程时如何使用 'REPLACE'函数?由于引号,查询无法执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19583981/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com