SP调用期间遇到错误代码1048

时间:2016-05-05 19:38:47

标签: mysql stored-procedures insert

当我尝试运行SP时,我遇到2列的“列不能为空”错误。

源表A定义如下:

CREATE TABLE `test_data`.`offer` (
  `id` INT NOT NULL,
  `hotel_id` INT NOT NULL,
  `currency_id` INT NOT NULL,
  `source_system_code` VARCHAR(64) NOT NULL,
  `available_cnt` INT NOT NULL,
  `sellings_price` FLOAT NOT NULL,
  `checkin_date` DATE NOT NULL,
  `checkout_date` DATE NOT NULL,
  `valid_offer_flag` TINYINT(1) NOT NULL,
  `offer_valid_from` DATETIME NOT NULL,
  `offer_valid_to` DATETIME NOT NULL,
  `breakfast_included_flag` TINYINT(1) NOT NULL,
  `insert_datetime` DATETIME NOT NULL,
  PRIMARY KEY (`id`));

我要插入数据的表B:

 CREATE TABLE `calculate_USD` (
`counter` int(11) NOT NULL AUTO_INCREMENT,
`dates` date DEFAULT NULL,
`id` int(11) NOT NULL, 
`hotel_id` int(11) NOT NULL,
`original_price` int(11) NOT NULL,
`currency_id` int(11) NOT NULL,
PRIMARY KEY (`counter`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;

以下是插入的SP:

CREATE PROCEDURE `test_procedure`()
BEGIN
DECLARE aid INT;
DECLARE avalid_from_date DATE;
DECLARE avalid_to_date DATE;
DECLARE hotel_id INT;
DECLARE original_price float;
DECLARE currency_id int;
DECLARE tempdt DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE getdates CURSOR FOR 
SELECT id,offer_valid_from,offer_valid_to,hotel_id,sellings_price,currency_id 
from offer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN getdates;

read_loop: LOOP
FETCH getdates 
INTO aid,avalid_from_date,avalid_to_date,hotel_id,original_price,currency_id;

    IF done THEN
  LEAVE read_loop;  
END IF;

set tempdt=date(avalid_from_date);

WHILE (tempdt <= date(avalid_to_date)) do
  insert into calculate_USD(dates,id,hotel_id,original_price,currency_id) 
  values(tempdt,aid,hotel_id,original_price,currency_id);
  set tempdt=tempdt+INTERVAL 1 DAY;
end while;


 END LOOP;
 CLOSE getdates;
 END
 //

当我打电话给SP时 hotel_id&amp; currency_id列正在发起错误代码。

如果我为它们分配两个NULL默认值,则SP会运行,hotel_id&amp;插入的currency_id值都是NULL,如此(其余都可以):

counter  |    dates   |  id   | hotel_id | original_price | currency_id |
  1      | 2015-4-30  | 342523|   NULL   |     200        |    NULL     |
  2      | 2015-4-30  | 342524|   NULL   |     112        |    NULL     |

这很奇怪,因为两者在源表中都有有效的数据。

请告知。

2 个答案:

答案 0 :(得分:0)

可以解决的一个选项是:

 
.
.
.
DECLARE `getdates` CURSOR FOR 
SELECT
  `id`,
  `offer_valid_from`,
  `offer_valid_to`,
  -- `hotel_id`,
  `offer`.`hotel_id`,
  `sellings_price`,
  -- `currency_id`
  `offer`.`currency_id`
FROM
  `offer`;
.
.
.

答案 1 :(得分:0)

所以我明白了。

我所要做的就是重新命名hotel_id&amp; SP中的currency_id变量与其他东西一起工作。

更新了SP:

delimiter //

CREATE PROCEDURE `test_procedure`()
BEGIN
DECLARE aid INT;
DECLARE avalid_from_date DATE;
DECLARE avalid_to_date DATE;
DECLARE sp_hotel_id INT;
DECLARE original_price float;
DECLARE sp_currency_id int;
DECLARE tempdt DATE;
DECLARE done INT DEFAULT FALSE;
DECLARE getdates CURSOR FOR 
SELECT id,offer_valid_from,offer_valid_to,hotel_id,sellings_price,currency_id 
from offer;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

OPEN getdates;

read_loop: LOOP
FETCH getdates 
INTO aid,avalid_from_date,avalid_to_date,sp_hotel_id,original_price,sp_currency_id;

    IF done THEN
  LEAVE read_loop;  
END IF;

set tempdt=date(avalid_from_date);

WHILE (tempdt <= date(avalid_to_date)) do
  insert into calculate_USD(dates,id,hotel_id,original_price,currency_id) 
  values(tempdt,aid,sp_hotel_id,original_price,sp_currency_id);
  set tempdt=tempdt+INTERVAL 1 DAY;
end while;


 END LOOP;
 CLOSE getdates;
 END
 //
 call test_procedure();