mySql存储过程不返回数据

时间:2013-09-06 04:19:29

标签: mysql mysql-workbench

我习惯于在MS SQL中工作,并且正在努力让mySql存储过程正常工作!

我知道有数据可以提取,但没有任何回报。

DELIMITER //
/*  Author: Jessie Brown     
    Date:   8/17/13 
    Notes:  Template Creation

    Comments: Comments created in the header area will NOT be stored in the database write
              Comments created AFTer the begin statement will be retained within the database write
*/

-- checks to see if stored procedure exists
-- if exists the procedure is dropped and recreated

DROP PROCEDURE IF EXISTS `reportLogExceptions`//

-- Creates new procedure

CREATE PROCEDURE reportLogExceptions (IN logId varchar(50),
    IN groupId varchar(50),
    IN fromDate varchar (50),
    IN toDate varchar(50))

  BEGIN
      /*
        report name: Log Exceptions
        Test Query: CALL reportLogExceptions('1','1','1378385650','1378472050')
      --------------------------------------------------------------------------
        Declare variables*/

      DECLARE logInS varchar(50);
      DECLARE groupIdS varchar (50);

    /*
      Sets the variables to use for the SELECT query
    */
      CASE
        WHEN logInS = '-1' THEN
        SET logInS = ''%'';
         ELSE SET logInS = logInS;
      END CASE;

      CASE
        WHEN groupIdS = '-1' THEN
        SET groupIdS = ''%'';
          Else SET groupIdS = groupIdS;   END Case;

  SELECT g.groupId,g.name AS groupName,l.logId,l.name AS logName, i.itemID,
      i.name AS itemName, le.userName,completed, i.optimalMin,i.optimalMax ,value
  FROM groups g
      JOIN LOGS l ON g.groupID = l.groupId
      JOIN logExceptions le ON l.logID = le.logID
      JOIN items i ON l.logId = i.logId
  WHERE  l.logID LIKE logInS
      AND g.groupID LIKE groupIdS 
      AND le.completed
        BETWEEN FROM_UNIXTIME(fromDate / 1000)
        AND FROM_UNIXTIME (toDate / 1000);
  END//

DELIMITER ;

调用我正在使用的存储过程

CALL reportLogExceptions('-1','-1','    1359676800','1362095999')

1 个答案:

答案 0 :(得分:0)

一方面,CASE语句中的逻辑无效。

您可以分别将logIdgroupId与自己进行比较,而不是引用输入参数logInSgroupIdS。因此,您总是将NULL s作为这些变量中的值。

这是显示

SQLFiddle 演示

你很可能有这个

SET logInS   = CASE logId   WHEN '-1' THEN '%' ELSE logId END;
SET groupIdS = CASE groupId WHEN '-1' THEN '%' ELSE groupId END;

这是 SQLFiddle 演示。

您对所有参数使用VARCHAR的选择似乎也很奇怪。使用适当的数据类型(例如INTlogIdgroupIdDATETIMETIMESTAMPINTfromDate和{{toDate 1}}取决于logExceptions.completed列使用的数据类型。

相关问题