gpt4 book ai didi

mysql - 我的mysql用户定义函数有什么错误?

转载 作者:行者123 更新时间:2023-11-29 09:37:08 27 4
gpt4 key购买 nike

mysql 8.0第6行语法错误,创建的函数如下

CREATE FUNCTION getStokMiktar(p_sto_id INT, p_dep_id INT)
RETURNS DOUBLE
BEGIN
DECLARE ret DOUBLE

SET ret = (SELECT SUM(CASE WHEN sth_depoid_cikis = p_dep_id THEN -sth_miktar
WHEN sth_depoid_giris = p_dep_id THEN sth_miktar
ELSE 0
END CASE)
FROM stokhareketleri
WHERE
(sth_depoid_cikis = p_dep_id OR sth_depoid_giris = p_dep_id)
AND sth_stokid = p_sto_id)

RETURN ret
END

我的表格结构是:

CREATE TABLE `stokhareketleri` (
`sth_id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`sth_stokid` INT(11) NOT NULL,
`sth_giriscikis` TINYINT(4) NOT NULL,
`sth_evraktipi` INT(11) NOT NULL,
`sth_depoid_cikis` INT(11) NOT NULL,
`sth_depoid_giris` INT(11) NOT NULL,
`sth_miktar` DOUBLE NOT NULL,
PRIMARY KEY (`sth_id`)
)
COLLATE='utf8_turkish_ci'
ENGINE=InnoDB
;

第 6 行的错误是什么?提前致谢。

最佳答案

正如@Salmon提到的,试试这个

delimiter $$
CREATE FUNCTION getStokMiktar(p_sto_id INT, p_dep_id INT)
RETURNS DOUBLE
BEGIN
DECLARE ret DOUBLE;

SELECT
SUM(CASE WHEN sth_depoid_cikis = p_dep_id THEN - sth_miktar
WHEN sth_depoid_giris = p_dep_id THEN sth_miktar
ELSE 0 END) INTO ret
FROM stokhareketleri
WHERE p_dep_id IN (sth_depoid_cikis,sth_depoid_giris)
AND sth_stokid = p_sto_id;

RETURN ret;
END $$

关于mysql - 我的mysql用户定义函数有什么错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57344167/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com