16

我的代码:

DECLARE report_date DATETIME;
set report_date='2013-01-17 00:00:00';

SELECT  *
  FROM `NMPP`.`capacitypersecond` 
  WHERE `StreamDT` >=report_date and `StreamDT` < '2013-01-18 00:00:00'  ;

SELECT  *
  FROM `NMPP`.`capacityperHr` 
  WHERE `StreamDT` >=report_date and `StreamDT` < '2013-01-18 00:00:00'  ;

SELECT  *
  FROM `NMPP`.`capacityperDay` 
  WHERE `TJLDate` >=report_date and `TJLDate` < '2013-01-18 00:00:00'  ;

-

DECLARE report_date DATETIME;
/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DECLARE report_date DATETIME' at line 1 */
/* Affected rows: 0  Found rows: 0  Warnings: 0  Duration for 0 of 5 queries: 0.000 sec. */
4

3 回答 3

16

或与演员:

set @report_date = cast('2013-01-17 00:00:00' as datetime);
于 2013-10-30T16:00:29.853 回答
9

摆脱declare

set @report_date = '2013-01-17 00:00:00';

SELECT  *
  FROM `NMPP`.`capacitypersecond` 
  WHERE `StreamDT` >= @report_date and `StreamDT` < '2013-01-18 00:00:00'  ;
于 2013-01-18T09:52:11.070 回答
1

所有的 DECLARE 都应该在触发器的开始处,存储过程。

这不是一种类似 C++ 的语言,您可以混合使用声明和语句,而更像 C,所有声明必须在所有语句之前完成。

于 2017-03-15T07:38:13.340 回答