When mysql is imported into the stored procedure, problems related to the declare error are solved.

Source: Internet
Author: User
When mysql is imported into the stored procedure, the declare error is solved. When importing the stored procedure, the following DECLARE errors are often encountered :? ErrorCode: 1064 YouhaveanerrorinyourSQLsyntax; checkthemanualthatcorrespondstoyourMySQLserverversionfortherightsyntaxtousene

When mysql is imported into the stored procedure, the declare error is solved. When importing the stored procedure, the following DECLARE errors are often encountered :? Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use ne

Solve the problem that declare reports an error when importing mysql into the Stored Procedure

When importing data to a stored procedure, you may encounter the following problems:

?

Error Code: 1064

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

(0 MS taken)

?

If there are multiple DECLARE statements, an error is reported if the variable is not obtained.

?

This is because delimiter is not defined.

?

An error is reported as follows:

Create procedure p8 ()?

BEGIN?

DECLARE a INT ;?

DECLARE B INT ;?

SET a = 5 ;?

SET B = 5 ;?

Select pkid, name, userGroup_desc, parent_id, group_state from T_VSM_SECPOLICY_USERGROUP ;?

END;

Error message:

Error Code: 1064

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

(0 MS taken)

?

....

?

If delimiter is added, no message is reported.

Delimiter //

Create procedure p8 ()?

BEGIN?

DECLARE a INT ;?

DECLARE B INT ;?

SET a = 5 ;?

SET B = 5 ;?

Declare cur0 cursor for select pkid from T_VSM_SECPOLICY_USERGROUP ;? -- Why is an error reported here?

END //

?

For details, refer to the mysql official manual.

Http://dev.mysql.com/doc/refman/5.1/zh/stored-procedures.html#declare-handlers

The following content in the create a Stored Procedure Section cannot be ignored:

?

The following is an example of a simple storage program using the OUT parameter. In this example, when the program is defined, use the mysql client delimiter command to change the statement delimiter from; //. This is allowed to be used in the program body; the delimiters are passed to the server rather than explained by mysql itself.

?

Mysql> delimiter //

?

Mysql> create procedure simpleproc (OUT param1 INT)

? ? -> BEGIN

? ? ->? Select count (*) INTO param1 FROM t;

? ? -> END

? ? -> //

Query OK, 0 rows affected (0.00 sec)

?

Mysql> delimiter;

?

Mysql> CALL simpleproc (@ );

Query OK, 0 rows affected (0.00 sec)

?

Mysql> SELECT @;

+ ------ +

| @? |

+ ------ +

| 3? ? |

+ ------ +

1 row in set (0.00 sec)

When using the delimiter command, you should avoid using the backslash ('\'), because it is an escape character of MySQL.

?

The following is an example. A parameter-based function uses an SQL function to perform an operation and return the result:

?

Mysql> delimiter //

?

Mysql> create function hello (s CHAR (20) returns char (50)

? ? -> Return concat ('hello, ', s ,'! ');

? ? -> //

Query OK, 0 rows affected (0.00 sec)

?

Mysql> delimiter;

?

Mysql> SELECT hello ('World ');

+ ---------------- +

| Hello ('World') |

+ ---------------- +

| Hello, world! ? |

+ ---------------- +

1 row in set (0.00 sec)

If the RETURN statement in the stored function RETURNS a type different from the type value specified in the RETURNS clause of the function, the RETURN value is forced to be of the appropriate type. For example, if a function returns an ENUM or SET value, but the RETURN statement returns an integer, for the corresponding ENUM Member of the SET member SET, the value returned from the function is a string.

?

?

This problem is also explained in the following content.

?

Limit 2.7. BEGIN... END compound statement

[Begin_label:] BEGIN

? ? [Statement_list]

END [end_label]

You can use the in... END compound statement to store subprograms to contain multiple statements. Statement_list indicates the list of one or more statements. Each statement in statement_list must end with a semicolon.

?

Compound statements can be marked. Unless begin_label exists, end_label cannot be given, and if both exist, they must be the same.

?

Note that the optional [NOT] ATOMIC clause is NOT supported yet. This means that no interaction storage point is set at the beginning of the instruction block, and the BEGIN clause Used in the context has no impact on the current interaction action.

?

To use multiple statements, the client must be able to send query strings containing statement delimiters. This symbol is processed by the delimiter command on the command line client. Change the end Separator of the query (for example, change to //) so that it can be used in the subprogram body.

?

?

In summary, the Stored Procedure contains many statements containing the default defined symbol ";". If you do not redefine the defined operator, you can only use "; to resolve the statement in the delimiter to the Mysql server, the first statement of the stored procedure must be incorrectly formatted:

Create procedure p8 ()?

BEGIN?

DECLARE a INT;

?

Then

DECLARE B INT;

An error will also be reported, because the Manual specifies that DECLARE must appear in BEGIN? And? Between EDN and before all other statements. The separate DECLARE B INT; a syntax error is returned.

?

Now I understand...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.