日期:2014-05-16  浏览次数:20703 次

mysql存储过程查询赋值
数据库:mysql
表1:CREATE TABLE `t_test1` (
  `test1` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  `test2` varchar(100) COLLATE utf8_bin DEFAULT NULL
)
表2:CREATE TABLE `test2` (
  `test` varchar(100) DEFAULT NULL
)

存储过程
begin
DECLARE times varchar(100);
DECLARE beginTime varchar(100);
DECLARE endTime varchar(100);
SELECT test1 into beginTime FROM t_timelimit;
SELECT test2 into endTime FROM t_timelimit;
INSERT INTO test2 (test) values (beginTime);
INSERT INTO test2 (test) values (endTime);
end

问题是 执行完了 在test2里 没有值 只有两个NULL
为什么into赋值没赋进去呢?

------解决方案--------------------
给出你的 create procedure 语句,另外把表的 insert into 语句也给出。

如果你的t_timelimit表中一条记录都没有,那自然SELECT test1 into beginTime FROM t_timelimit; 也取不到记录,结果beginTime为NULL。
------解决方案--------------------
SELECT test1 into beginTime FROM t_timelimit;
SELECT test2 into endTime FROM t_timelimit;


结果集放入变量中?
------解决方案--------------------
关键查询出的SELECT test1 FROM t_timelimit;SELECT test2 from t_timelimit;
是否有值这是关键。如果查询出的都没有值的话,放入表中也是为空的。
------解决方案--------------------
SELECT test2 into endTime FROM t_timelimit;
1.表中查不到数据是null
2.要保证 查出的结果集只有一行