尝试在存储过程中的MySQL临时表中执行3个插入命令,但是select命令只显示执行/插入的第一个插入。
CREATE TEMPORARY TABLE IF NOT EXISTS OtDates (OtDatesList Date, TotalOTMinutes int);
#Fist Insert
insert into OtDates(OtDatesList,TotalOTMinutes) values('2017-04-15', 400);
#Second Insert
insert into OtDates(OtDatesList,TotalOTMinutes) values(@DateOtHalf, @TotalOTMinutesHalf);
#Third Insert
insert into OtDates(OtDatesList,TotalOTMinutes) values(@DateOtFull, @TotalOTMinutesFull);
选择命令仅返回2017-04-15,400,而第二和第三个插入中的变量值未列出。如果我删除第一个插入命令,则第二个插入命令而不是第三个插入命令。
编辑2:
提供完整的存储过程
CREATE DEFINER=`root`@`localhost` PROCEDURE `Test0`()
begin
select @DateOtHalf:=DateofIn, @TotalOTMinutesHalf:=TotalOTMinutes from hrtpunch where TotalOTMinutes >= @ExtraWorkHalfDayInMin and TotalOTMinutes < @ExtraWorkFullDayInMin and EmpID = P_EmpID;
select @DateOtFull:=DateofIn, @TotalOTMinutesFull:=TotalOTMinutes from hrtpunch where TotalOTMinutes >= @ExtraWorkFullDayInMin and EmpID = P_EmpID;
CREATE TEMPORARY TABLE IF NOT EXISTS OtDates (OtDatesList Date, TotalOTMinutes int);
#Fist Insert
insert into OtDates(OtDatesList,TotalOTMinutes) values('2017-04-15', 400);
#Second Insert
insert into OtDates(OtDatesList,TotalOTMinutes) values(@DateOtHalf, @TotalOTMinutesHalf);
#Third Insert
insert into OtDates(OtDatesList,TotalOTMinutes) values(@DateOtFull, @TotalOTMinutesFull);
select * from OtDates;
end
编辑3:从“hrtpunch”提供样本数据和表格结构
创建声明
CREATE TABLE `hrtpunch` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`TotalOTMinutes` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=464 DEFAULT CHARSET=utf8;
示例数据插入
insert into hrtpunch (TotalOTMinutes) values(61);
insert into hrtpunch (TotalOTMinutes) values(600);
insert into hrtpunch (TotalOTMinutes) values(301);
insert into hrtpunch (TotalOTMinutes) values(0);
为ExtraWorkHalfDayInMin返回的Int值为160,ExtraWorkFullDayInMin为240
答案 0 :(得分:0)
试试这样:
CREATE TEMPORARY TABLE IF NOT EXISTS OtDates (OtDatesList Date, TotalOTMinutes int);
INSERT INTO OtDates (OtDatesList,TotalOTMinutes) VALUES
('2017-04-15', 400), (@DateOtHalf, @TotalOTMinutesHalf), (@DateOtHalf, @TotalOTMinutesHalf)
修改强>
对于SQL 2005,上面的代码不起作用,所以试试这个:
INSERT INTO OtDates (OtDatesList,TotalOTMinutes)
SELECT '2017-04-15', 400
UNION ALL
SELECT @DateOtHalf, @TotalOTMinutesHalf
UNION ALL
SELECT @DateOtHalf, @TotalOTMinutesHalf
答案 1 :(得分:0)
解决!
DROP TEMPORARY TABLE IF EXISTS `OtDates`;
CREATE TEMPORARY TABLE IF NOT EXISTS OtDates (OtDatesList Date, TotalOTMinutes int);
insert into OtDates(OtDatesList,TotalOTMinutes) select @DateOtHalf:=DateofIn, @TotalOTMinutesHalf:=TotalOTMinutes from hrtpunch where TotalOTMinutes >= @ExtraWorkHalfDayInMin and TotalOTMinutes < @ExtraWorkFullDayInMin and EmpID = P_EmpID;
insert into OtDates(OtDatesList,TotalOTMinutes) select @DateOtFull:=DateofIn, @TotalOTMinutesFull:=TotalOTMinutes from hrtpunch where TotalOTMinutes >= @ExtraWorkFullDayInMin and EmpID = P_EmpID;