【发布时间】:2022-01-16 20:33:37
【问题描述】:
我有一个数据库“短途旅行”,其中包含有关客户、公共汽车、司机、账单和短途旅行的信息。我需要创建一个函数来计算一个客户指定月份的付款。我ve already created it. Also ive 创建了一个为所有客户调用该函数的过程。但是 MySQL Workbench 告诉我有一个错误。帮我解决它
USE excursions_test;
DELIMITER $$
CREATE FUNCTION sum_bill(excursion_id INT, for_begin DATE, for_end DATE) RETURNS INT
DETERMINISTIC
BEGIN
SELECT excStart = excursion.start_date, excEnd = excursion.end_date, excPrice =( excursion.excursion_duration*excursion.number_of_tourists*bus_model.fuel_for_km*excursion.distance)*1.5,
IF(excStart < for_begin OR excEnd > for_end, 0,
IF(excStart <= for_begin OR excEnd >= for_end AND excEnd <= for_end, DATEDIFF(for_begin, excEnd)*excPrice/30,
IF(excStart >= for_begin AND excEnd >= for_begin AND excEnd <= for_end, DATEDIFF(excStart, excEnd)*excPrice/30,
IF(excStart >= for_begin AND excEnd >= for_end, DATEDIFF(excStart, for_end)*excPrice/30,
IF(excStart <= for_begin AND excEnd >= for_end, DATEDIFF(for_begin, for_end)*excPrice/30, 0)))))
FROM excursion JOIN bus_model ON excursion.bus_model_id = bus_model.bus_model_id
WHERE excursion.excursion_id = excursion_id;
RETURN 0;
END $$
DELIMITER ;
DELIMITER $$
CREATE PROCEDURE bill_creator(for_begin DATE, for_end DATE)
BEGIN
INSERT INTO bill(excursion_id, start_date, end_date, amount)
SELECT excursion.excursion_id, for_begin, for_end, dbo.sum_bill(dbo.excursion.excursion_id, for_begin, for_end)
FROM excursion
WHERE (excursion.start_date >= for_begin AND excursion.end_date <= for_end)
OR (excursion.end_date >= for_begin AND excursion.end_date <= for_end)
OR (excursion.start_date<= for_begin AND excursion.end_date>= for_begin);
END $$
DELIMITER ;
CALL bill_creator ('2021-10-01', '2021-10-31')
错误:不允许从函数返回结果集
【问题讨论】:
-
你确定这是mysql吗?使用dbo表示sql_server..
-
函数的意义何在?结果总是返回 0。它有一个 select 语句,它只返回一个结果集,根据错误消息,这是不允许的。
-
如果你是从 ms sql server 迁移代码到 mysql,那么你需要将所有代码转换为 mysql 语法,而不仅仅是部分代码!