In MySql, how can I use the 'REPLACE' function when defining a stored procedure? The query cannot be executed due to quotes

2.5k Views Asked by At

I'm attempting to run the below stored procedure.

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

However this query doesn't run as I get the message:

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

I've also attempted single quotes, escaped quotes and as above double quotes.

Any idea.

Fixed by @eggyal:

The fixed query now looks like this:

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

As @eggyal points out the Begin and End statements are not required. And in the cases where they are I have to modify the delimiter.

Thanks!

1

There are 1 best solutions below

3
eggyal On BEST ANSWER

As documented under 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> delimiter //

mysql> CREATE PROCEDURE dorepeat(p1 INT)
    -> BEGIN
    ->   SET @x = 0;
    ->   REPEAT SET @x = @x + 1; UNTIL @x > p1 END REPEAT;
    -> END
    -> //
Query OK, 0 rows affected (0.00 sec)


mysql> delimiter ;

mysql> CALL dorepeat(1000);
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT @x;
+------+
| @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> CREATE FUNCTION hello (s CHAR(20))
mysql> RETURNS CHAR(50) DETERMINISTIC
    -> RETURN CONCAT('Hello, ',s,'!');
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT hello('world');
+----------------+
| hello('world') |
+----------------+
| Hello, world!  |
+----------------+
1 row in set (0.00 sec)

In your case, since your sproc comprises only a single statement, you can simply remove the BEGIN ... END block.