0

我习惯于在 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')
4

1 回答 1

0

一方面,您的CASE陈述中的逻辑是无效的。

而不是引用输入参数logId,而是分别groupId比较logInSgroupIdS自己。因此,您总是将NULLs 作为这些变量中的值。

这是SQLFiddle演示,它表明

你很可能打算拥有这个

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

这是SQLFiddle演示。

此外,您选择使用VARCHAR所有参数似乎很奇怪。使用适当的数据类型(例如for and ,或INTfor logIdandgroupId取决于DATETIME列使用的数据类型)。TIMESTAMPINTfromDatetoDatelogExceptions.completed

于 2013-09-06T06:06:41.307 回答