MySQL:存储过程中的 IF / THEN 语句
我正在编写一个使用多个 IF / THEN 语句的存储过程,如果它们的计算结果为 true,则还需要执行多个查询。问题是,我似乎找不到任何适当语法的示例。从 MySQL 开发手册来看,我似乎可以在“statement_list”,但到目前为止我还无法让它工作。
这就是我想要做的:
SET agency =
COALESCE((SELECT org_agency_o_id
FROM orgs_agencies
WHERE org_agency_code = maj_agency_cat)
,(SELECT min(org_id)
FROM orgs
WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5))))
IF agency IS NULL THEN
-- execute multiple queries
INSERT INTO orgs (org_name
,org_name_length
,org_type
,org_sub_types)
VALUES (CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5))
,LENGTH(CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
,'org','Org,GovernmentEntity,Federal,Agency');
SET agency = LAST_INSERT_ID();
END IF;
错误:
您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,了解在
'IF Agency IS NULL THEN 附近使用的正确语法 INSERT INTO orgs (org_name,org_name_length,org_type,'
第 53 行
有什么想法吗?我知道它必须是简单的东西,所以我将非常感谢任何人的输入。
I'm writing a stored procedure that uses multiple IF / THEN statements that also need to execute multiple queries if they evaluate to true. Problem is, I can't seem to find any examples of the appropriate syntax. From the MySQL dev handbook, it seems like I could have multiple queries in the "statement_list," but so far I can't get it to work.
Here's what I'm trying to do:
SET agency =
COALESCE((SELECT org_agency_o_id
FROM orgs_agencies
WHERE org_agency_code = maj_agency_cat)
,(SELECT min(org_id)
FROM orgs
WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5))))
IF agency IS NULL THEN
-- execute multiple queries
INSERT INTO orgs (org_name
,org_name_length
,org_type
,org_sub_types)
VALUES (CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5))
,LENGTH(CONCAT('U.S. ',SUBSTRING(maj_agency_cat,5)))
,'org','Org,GovernmentEntity,Federal,Agency');
SET agency = LAST_INSERT_ID();
END IF;
The error:
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
'IF agency IS NULL THEN
at line 53
INSERT INTO orgs (org_name,org_name_length,org_type,'
Any ideas? I know it has to be something simple, so I would greatly appreciate anybody's input.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
据我所知,您遇到了一些问题:
;
终止SELECT
,最好使确保它只能通过执行LIMIT 1
选择一个值;如果您有一个像min()
这样的聚合函数,那么只能输出一个值。CREATE PROCEDURE ...
语法编写过程,请不要忘记在CREATE PROCEDURE ... END $$ 之前设置
主体和后面的DELIMITER $$
DELIMITER ;
。IF THEN ... END IF
块内有多个语句,最好将它们放在BEGIN ... END;
块内。FUNCTION name (arg1: INTEGER) RETURNS INTEGER
而不是PROCEDURE name (IN arg1 INTEGER, OUT Agency INTEGER)< /代码>。该功能更加通用。
You got a few issues as far as I can see:
;
SELECT
, better make sure it can only select one value by doing aLIMIT 1
; If you've got an aggregate function likemin()
then only one value can come out.CREATE PROCEDURE ...
syntax, don't forget to setDELIMITER $$
before theCREATE PROCEDURE ... END $$
body and aDELIMITER ;
after.IF THEN ... END IF
block, it's a good idea to put them inside aBEGIN ... END;
block.FUNCTION name (arg1: INTEGER) RETURNS INTEGER
instead of aPROCEDURE name (IN arg1 INTEGER, OUT agency INTEGER)
. The function is much more versatile.第一个 SET 语句后没有分号。
No semicolon after your first SET statement.