if / else块内的create table问题

时间:2013-04-04 17:30:45

标签: mysql sql stored-procedures

我正在尝试在MySQL存储过程中创建一个表,具体取决于某些传递参数的值。我的代码如下:

DELIMITER //

CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
    BEGIN    
        IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
        ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
    END

我收到一条错误消息:#1064 - 您的SQL语法错误;检查与MySQL服务器版本对应的手册,以便在第5行的''附近使用正确的语法。

我不确定我在这里做错了什么。语法看起来很好,查询似乎很好。

1 个答案:

答案 0 :(得分:1)

您错过了END IF声明。

CREATE PROCEDURE testifelse(IN CurrentGPA varchar(40), IN CurrentGPAValue varchar(40))
    BEGIN    
        IF CurrentGPAValue IS NULL THEN CREATE TABLE tableCurrentGPA(SELECT DISTINCT u.user_id AS uid FROM users u);
        ELSE CREATE TABLE tableCurrentGPA(SELECT DISTINCT udata.user_id AS uid FROM userdata udata WHERE udata.userDataTypeName = CurrentGPA AND udata.userDataText = CurrentGPAValue);
        END IF;
    END