-3
/*Table structure for table `testtable` */

DROP TABLE IF EXISTS `testtable`;

CREATE TABLE `testtable` (
  `tid` bigint(20) unsigned NOT NULL auto_increment,
  `tcode` bigint(20) default NULL,
  `tname` varchar(50) default NULL,
  `tdate` date default NULL,
  `ttime` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
  `tnumeric` decimal(10,2) default NULL,
  `tdecimal` decimal(8,2) default NULL,
  PRIMARY KEY  (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=110 DEFAULT CHARSET=latin1;

/*Data for the table `testtable` */

insert  into `testtable`(`tid`,`tcode`,`tname`,`tdate`,`ttime`,`tnumeric`,`tdecimal`) values (100,80,'test name text1','2012-11-03','2012-11-03 00:00:00','123.23','456.23'),(101,81,'test name text2','2012-11-04','2012-11-04 00:00:00','78.23','456.23'),(102,82,'test name text3','2012-11-05','2012-11-05 00:00:00','456.23','456.23'),(103,83,'test name text4','2012-11-06','2012-11-06 00:00:00','81.23','456.23'),(104,84,'test name text5','2012-11-20','2013-03-20 16:13:25','333.23','456.23'),(105,84,'test name text5','2012-11-15','2013-04-09 18:45:21','333.23','456.23'),(106,84,'test name text5','2013-03-01','2013-03-20 16:16:11','333.23','456.23'),(107,84,'test name text5','2012-11-10','2013-03-20 16:13:14','333.23','456.23'),(108,84,'test name text5','2012-11-07','2012-11-07 00:00:00','0.00','456.23'),(109,84,'test name text5','2012-11-22','2013-03-20 16:16:02','333.23','0.00');


----------------------------------------------------------------------------------------

CREATE DEFINER=`root`@`localhost` PROCEDURE `testProc`(
IN tdate VARCHAR(15)

)
BEGIN
    DECLARE a,b,c DECIMAL(10,2);

    -- SELECT * FROM `testtable` WHERE `tdate` >='2012-11-06';
    -- SELECT * FROM `testtable` WHERE `tdate` between STR_TO_DATE('06/11/2012', '%d/%m/%Y') and STR_TO_DATE('30/11/2012', '%d/%m/%Y');
    -- SELECT * FROM `testtable` WHERE `tdate` < STR_TO_DATE('2012-11-06', '%Y-%d-%m');

    SELECT * FROM `testtable` WHERE `tdate` < tdate;

    -- select STR_TO_DATE(tdate, '%d/%m/%Y');
    -- select * from `testtable`;

    END$$
4

1 回答 1

2

它不起作用,因为在参数名称列名称之间发生了名称冲突。更改参数名称,它肯定会工作,

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `testProc`
(
     IN _tdate date
)
BEGIN
    DECLARE a,b,c DECIMAL(10,2);
    SELECT * FROM `testtable` WHERE `tdate` < _tdate;
END$$
DELIMITER ;

我敢打赌,无论您在参数中传递什么值,您当前的过程都会返回一个空集。这是因为tdate总是等于tdate而不是tdate < _tdate

还将参数的数据类型更改为datenot VARCHAR(15)

于 2013-04-09T13:45:08.537 回答