[英]Stored procedure to create a MaillingListCount
I am following sql in 10 minutes
to learn "stored procedure"我
sql in 10 minutes
关注sql in 10 minutes
以学习“存储过程”
#+BEGIN_SRC sql :engine mysql :dbuser org :database grocer
CREATE PROCEDURE MailingListCount (
ListCount OUT INTEGER )
IS
v_rows INTEGER;
BEGIN
SELECT COUNT(*) INTO v_rows
FROM Customers
WHERE NOT cust_email IS NULL;
ListCount := v_rows;
END;
#+END_SRC
#+RESULTS:
| |
it report error:它报告错误:
ERROR 1064 (42000) at line 1: 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 'OUT INTEGER )
IS
v_rows INTEGER' at line 2
Could you please provide any hints?你能提供任何提示吗?
Couple of fixes:几个修复:
OUT
comes before the parameter name OUT
出现在参数名称之前IS
IS
BEGIN
END
blockBEGIN
END
块中声明变量SET
when you assign variablesSET
So:所以:
CREATE PROCEDURE MailingListCount(OUT ListCount INTEGER )
BEGIN
declare v_rows INTEGER;
SELECT COUNT(*) INTO v_rows
FROM Customers
WHERE NOT cust_email IS NULL;
SET ListCount := v_rows;
END;
Usually it's easier to handle the procedure output from result set rather than OUT
variables.通常,处理结果集的过程输出比处理
OUT
变量更容易。 The OUT
variables are useful primarily on calls between procedures. OUT
变量主要用于过程之间的调用。
So if you plan to call the routine from application, use:因此,如果您打算从应用程序调用例程,请使用:
CREATE PROCEDURE MailingListCount()
BEGIN
SELECT COUNT(*) as 'Count'
FROM Customers
WHERE NOT cust_email IS NULL;
END;
First thing is the position of the OUT keyword.首先是 OUT 关键字的位置。 It should be before the Parameter name.
它应该在参数名称之前。
Then second one no need to create the variable v_rows to store the output and then finally assigning it back to the OUT parameter listCount.然后第二个不需要创建变量 v_rows 来存储输出,然后最终将其分配回 OUT 参数 listCount。
If you want to check condition like email should not null then you should do something like WHERE cust_email IS NOT NULL
instead of WHERE NOT cust_email IS NULL
如果你想检查条件,比如 email 不应该为空,那么你应该做一些类似
WHERE cust_email IS NOT NULL
而不是WHERE NOT cust_email IS NULL
Please refer below code for the reference :请参考以下代码以供参考:
DELIMITER $$
CREATE PROCEDURE `MailingListCount` (OUT listCount INTEGER)
BEGIN
SELECT COUNT(*) INTO listCount
FROM Customers
WHERE cust_email IS NOT NULL;
END$$
DELIMITER ;
You can use the different delimiter for the MySql stored procedures and functions.您可以为 MySql 存储过程和函数使用不同的分隔符。
MySql use ;
MySql 使用
;
as default delimiter so delimiters other than the default ;
作为默认分隔符,所以除默认之外的分隔符
;
are typically used when defining functions, stored procedures, and triggers wherein you must define multiple statements.通常在定义函数、存储过程和触发器时使用,其中必须定义多个语句。 You define a different delimiter like
$$
which is used to define the end of the entire procedure, but inside it, individual statements are each terminated by ;
您定义了一个不同的分隔符,如
$$
用于定义整个过程的结尾,但在其中,各个语句都以;
结尾;
. . That way, when the code is run in the mysql client, the client can tell where the entire procedure ends and execute it as a unit rather than executing the individual statements inside.
这样,当代码在mysql客户端运行时,客户端可以知道整个过程在哪里结束并作为一个单元执行,而不是执行内部的单个语句。
You can refer the https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html to learn MySQL Stored procedure.您可以参考https://dev.mysql.com/doc/refman/8.0/en/create-procedure.html来学习 MySQL 存储过程。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.