2014-09-23 79 views
-2

我在mysql中创建程序。但我在创建时遇到了一些问题。如何在mysql中创建程序

我申请查询即

CREATE PROCEDURE simpleproc(param1 INT) BEGIN SELECT COUNT(*) 
INTO param1 FROM 91_nidhi; END// 

和错误是

#1064 - You have an error in your SQL syntax; c 
heck the manual that corresponds to your MySQL server version for the 
right syntax to use near '' at line 1 
+3

您在统计需要一个分隔符''delimiter //' – 2014-09-23 09:36:10

+1

如果你使用换行符,代码将更具可读性nd你会在错误信息中得到一个实际有用的行号。 – GolezTrol 2014-09-23 09:36:53

+0

好吧现在它正在工作,但你可以请告诉我怎么可以称此程序? – 2014-09-23 09:56:22

回答

1

这个你需要怎么做 - 您没有传递任何输入值,而你正在使用的输出值 - 所以将参数指定为OUT

以下是示例

mysql> create table 91_nidhi (id int,val varchar(20)); 
Query OK, 0 rows affected (0.09 sec) 

mysql> insert into 91_nidhi values (1,'aa'),(2,'bb'),(3,'cc'); 
Query OK, 3 rows affected (0.00 sec) 
Records: 3 Duplicates: 0 Warnings: 0 

现在让我们创建过程

delimiter // 
CREATE PROCEDURE simpleproc(out param1 INT) 
BEGIN 
    SELECT COUNT(*) INTO param1 FROM 91_nidhi; 
END; // 

然后更改终端的分隔符为

mysql> delimiter ; 

现在让我们调用过程

mysql> call simpleproc(@res); 
Query OK, 0 rows affected (0.00 sec) 

mysql> select @res ; 
+------+ 
| @res | 
+------+ 
| 3 | 
+------+ 
1 row in set (0.00 sec)