MySQL:存储过程中的 IF / THEN 语句

发布于 2024-11-06 20:45:51 字数 1192 浏览 1 评论 0原文

我正在编写一个使用多个 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
INSERT INTO orgs (org_name,org_name_length,org_type,'
at line 53

Any ideas? I know it has to be something simple, so I would greatly appreciate anybody's input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

囍笑 2024-11-13 20:45:51

据我所知,您遇到了一些问题:

  1. 正如 David 指出的那样,每个语句都需要由 ; 终止
  2. 如果您执行 SELECT,最好使确保它只能通过执行 LIMIT 1 选择一个值;如果您有一个像 min() 这样的聚合函数,那么只能输出一个值。
  3. 如果您使用 CREATE PROCEDURE ... 语法编写过程,请不要忘记在 CREATE PROCEDURE ... END $$ 之前设置 DELIMITER $$ 主体和后面的 DELIMITER ;
  4. 如果 IF THEN ... END IF 块内有多个语句,最好将它们放在 BEGIN ... END; 块内。
  5. 如果您有一个返回值,就像这里的 Agency 一样,为什么不将其设为 FUNCTION name (arg1: INTEGER) RETURNS INTEGER 而不是 PROCEDURE name (IN arg1 INTEGER, OUT Agency INTEGER)< /代码>。该功能更加通用。
DELIMITER $
CREATE PROCEDURE name(arg1 INTEGER, arg2 INTEGER, ...)
BEGIN

  SELECT SET agency = 
    COALESCE((SELECT org_agency_o_id 
            FROM orgs_agencies 
            WHERE org_agency_code = maj_agency_cat) LIMIT 1,
    (SELECT min(org_id) FROM orgs     
    WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5)))); 

  IF agency IS NULL THEN BEGIN
    -- 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; END IF;
END $

DELIMITER ;

You got a few issues as far as I can see:

  1. As David pointed out, each and every statement needs to be terminated by a ;
  2. If you do a SELECT, better make sure it can only select one value by doing a LIMIT 1; If you've got an aggregate function like min() then only one value can come out.
  3. If you writing the procedure using the CREATE PROCEDURE ... syntax, don't forget to set DELIMITER $$ before the CREATE PROCEDURE ... END $$ body and a DELIMITER ; after.
  4. If you have multiple statements inside your IF THEN ... END IF block, it's a good idea to put them inside a BEGIN ... END; block.
  5. If you have a return value, like agency here, why not make it a FUNCTION name (arg1: INTEGER) RETURNS INTEGER instead of a PROCEDURE name (IN arg1 INTEGER, OUT agency INTEGER). The function is much more versatile.
DELIMITER $
CREATE PROCEDURE name(arg1 INTEGER, arg2 INTEGER, ...)
BEGIN

  SELECT SET agency = 
    COALESCE((SELECT org_agency_o_id 
            FROM orgs_agencies 
            WHERE org_agency_code = maj_agency_cat) LIMIT 1,
    (SELECT min(org_id) FROM orgs     
    WHERE org_name LIKE CONCAT('U.S.',SUBSTRING(maj_agency_cat,5)))); 

  IF agency IS NULL THEN BEGIN
    -- 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; END IF;
END $

DELIMITER ;
一梦等七年七年为一梦 2024-11-13 20:45:51

第一个 SET 语句后没有分号。

No semicolon after your first SET statement.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文