存储过程:多个查询以防/ if else

时间:2013-02-11 09:50:34

标签: mysql

BEGIN CASE assetTypeNo 
WHEN 0 THEN 
( 
SELECT * 
FROM 
books 
) 
WHEN 1 
THEN 
SELECT * 
FROM magazines 
END; 
END

在mySQL中创建存储过程。这可能吗?如果是这样。请帮忙。我无法在任何地方找到答案。

3 个答案:

答案 0 :(得分:0)

试试这个,

DELIMITER $$
CREATE PROCEDURE ProcName
BEGIN 
IF assetTypeNo = 0 THEN 
   SELECT * 
   FROM ;
ELSEIF assetTypeNo = 1 THEN
   SELECT * 
   FROM magazines 
END; 
END $$
DELIMITER ;

答案 1 :(得分:0)

语法不正确。试试这个脚本 -

BEGIN

  CASE assetTypeNo
  WHEN 0 THEN 
    SELECT * FROM books;
  WHEN 1 THEN 
    SELECT * FROM magazines;
  END CASE; 

END

答案 2 :(得分:0)

试试这个。这应该工作。

CREATE DEFINER = 'root'@'%' PROCEDURE `test_proc`(
        IN assetTypeNo INTEGER(11)
    )
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN

    CASE assetTypeNo
      WHEN 0 THEN SELECT * FROM `books`;
      WHEN 1 THEN SELECT * FROM `magazines` ;
      ELSE
        BEGIN
        END;
    END CASE; 

END;