控制流程函数
CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result ...] [ELSE result] END CASE WHEN [condition] THEN result [WHEN [condition] THEN result ...] [ELSE result] END 
现在我要创建1个存储过程,根据入参deffer int,执行不同的SQL语句。
大概这样:CASE deffer WHEN 1 THEN
BEGIN
DELTE FROM TB1;
END;
WHEN 2 THEN
BEGIN
DELTE FROM TB2;
END;
WHEN 3 THEN
BEGIN
DELTE FROM TB3;
END;
WHEN 4 THEN
BEGIN
DELTE FROM TB4;
END;
WHEN 5 THEN
BEGIN
DELTE FROM TB5;
END;END;但这样的语法视乎不对,该如何修改?

解决方案 »

  1.   


    if deffer =1  then
     delete from tb1;
    elseif deffer =2 then
     delete from tb2;
    end if;
      

  2.   

    参考手册中的例子。 另外MYSQL中没有DELTE FROM TB4;,只有 delete from TB412.8.6.2. CASE Statement
    CASE case_value
        WHEN when_value THEN statement_list
        [WHEN when_value THEN statement_list] ...
        [ELSE statement_list]
    END CASEOr: CASE
        WHEN search_condition THEN statement_list
        [WHEN search_condition THEN statement_list] ...
        [ELSE statement_list]
    END CASEThe CASE statement for stored programs implements a complex conditional construct. If a search_condition evaluates to true, the corresponding SQL statement list is executed. If no search condition matches, the statement list in the ELSE clause is executed. Each statement_list consists of one or more statements. If no when_value or search_condition matches the value tested and the CASE statement contains no ELSE clause, a Case not found for CASE statement error results. Each statement_list consists of one or more statements; an empty statement_list is not allowed. To handle situations where no value is matched by any WHEN clause, use an ELSE containing an empty BEGIN ... END block, as shown in this example: DELIMITER |CREATE PROCEDURE p()
      BEGIN
        DECLARE v INT DEFAULT 1;    CASE v
          WHEN 2 THEN SELECT v;
          WHEN 3 THEN SELECT 0;
          ELSE
            BEGIN
            END;
        END CASE;
      END;
      |
      

  3.   

    昨天句子太长了,没测试,
    写的太急了,DELETE都写错了。
      

  4.   

    已经测试过了,可以加在BEGIN...END之间。