MYSQL 如何声明一个日期时间变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14396196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:15:09  来源:igfitidea点击:

MYSQL how to declare a datetime variable?

mysqlsql

提问by u1230329

My code:

我的代码:

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. */

采纳答案by palindrom

get rid of declare:

摆脱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'  ;

回答by spacepille

or with cast:

或与演员:

set @report_date = cast('2013-01-17 00:00:00' as datetime);

回答by Pazhani Samy

All the DECLARE should be at the start of the trigger,stored procedure.

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

This is not a C++ like language where you can mix declarations and statements, but more like C, where all declarations must be done before all statements.

这不是像 C++ 那样可以混合声明和语句的语言,而是更像 C,所有声明必须在所有语句之前完成。