如果有一个结果集,则Mysql返回过程结果集

时间:2013-09-02 19:22:36

标签: mysql sql stored-procedures resultset

您好我想在mysql中编写一个存储过程,它返回不同类型的表,但每次只返回一行。我没有可能使用可以重写多个结果集的函数。但如果我可以使用这样的东西会很好:

main:BEGIN
    IF EXISTS(SELECT 1 
                FROM Ban
               WHERE (`Ban`.`Name` = _IP OR `Ban`.`IP` = _IP)
                 AND `Ban`.`Date` > _Date LIMIT 1)
    THEN BEGIN
        SELECT 0 AS LoginType, `Ban`.`RegID`, `Ban`.`Name`, `Ban`.`Reason`,
               `Ban`.`Date`, `Eredmeny`.`Name`, `Ban`.`UnbanDate`, `Ban`.`BanType` 
          FROM `Ban` 
          LEFT OUTER JOIN (SELECT DISTINCT RegID, Name 
                             FROM ((SELECT Players.RegID,Players.Name FROM Players)
                                    UNION ALL
                                   (SELECT PlayersArchive.RegID, PlayersArchive.Name
                                     FROM PlayersArchive)) Eredmeny) AS Eredmeny
                       ON Ban.RegID = Eredmeny.RegID
         WHERE (`Ban`.`Name` = _Name OR `Ban`.`IP` = _IP)
           AND `Ban`.`Date` > _Date LIMIT 1;
        LEAVE main;
        END;
    END IF;

    IF EXISTS(SELECT 1 FROM `Players` WHERE `Name` = _Name LIMIT 1)
    THEN BEGIN
        SELECT 1 AS LoginType,Players.*
          FROM `Players` WHERE `Name` = _Name LIMIT 1;
        LEAVE main;
    END;
    END IF;

    IF EXISTS(SELECT 1 FROM `PlayersArchive` WHERE `Name` = _Name LIMIT 1)
    THEN BEGIN
        SELECT 2 AS LoginType,PlayersArchive.*
          FROM `PlayersArchive`
         WHERE `Name` = _Name LIMIT 1;
        LEAVE main;
    END;
    END IF;  

    IF EXISTS(SELECT 1 FROM `PreReg` WHERE `Name` = _Name LIMIT 1)
    THEN BEGIN
        SELECT 3 AS LoginType,`Name`, `Password`, `Email`, `RegDate`, `Level`,
               `StuntDone`, `DriftDone`, `ParkourDone`, `RolePlayDone`,
               `DeathmatchDone`, `PosX`, `PosY`, `PosZ`, `PosA`
          FROM `PreReg`
         WHERE `Name` = _Name LIMIT 1;
        LEAVE main;
    END;
    END IF;

    SELECT 4 AS LoginType LIMIT 1;
END

另一个想法是创建一个函数,并在函数中创建if块中的四个表填充表并返回表但我真的不知道它是否可行或是否可行。但是,如果是下一个问题是我不想添加每个新列,如果有一个新的,所以它不能自动我认为。

_Name is a VARCHAR(24)
_IP is VARCHAR(16)
_Date is INT(11)

0 个答案:

没有答案