【发布时间】:2015-12-22 23:44:54
【问题描述】:
很抱歉发布这么低质量的问题,但我真的很茫然。我无法创建这个存储过程,因为工作台一直告诉我我有语法错误......这两个错误被 *** 包围并且在第 6 行。
当我尝试保存存储过程时,名称在workbench中自动解析为_SYNTAX_ERROR,sql失败。
CREATE PROCEDURE fetchBudgetInfo(IN year YEAR, IN costCenter INT(11))
BEGIN
BEGIN
SELECT gls.gl, gls.name AS gl_name, IFNULL(budgets.total, 0) as totalBudgeted,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap1,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year)***Error HERE (missing closing bracket)*** / 12) * 100, 2), 0 ***Error HERE (missing semicolon)***) AS ap1Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap1Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 2 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap2,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 2 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap2Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 2 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap2Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 3 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap3,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 3 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap3Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 3 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap3Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 4 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap4,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 4 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap4Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 4 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap4Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 5 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap5,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 5 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap5Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 5 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap5Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 6 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap6,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 6 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap6Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 6 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap6Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 7 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap7,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 7 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap7Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 7 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap7Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 8 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap8,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 8 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap8Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 8 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap8Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 9 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap9,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 9 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap9Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 9 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap9Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 10 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap10,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 10 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap10Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 10 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap10Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 11 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap11,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 11 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap11Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 11 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap11Year,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 12 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END), 2), 0) AS ap12,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 12 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) / 12) * 100, 2), 0) AS ap12Ap,
IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 12 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / (SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year) * 100, 2), 0) AS ap12Year,
IFNULL(ROUND(SUM(totalCost), 2), 0) AS totalCost,
IFNULL(ROUND(SUM(totalCost) / (SELECT total FROM budgets WHERE budgets.costCenter = costCenter AND budgets.gl = gls.id) * 100, 2), 0) AS totalPercent
FROM gls
LEFT JOIN requests ON requests.glid = gls.id AND requests.costCenter = costCenter AND requests.status = 'approved' AND (SELECT YEAR(ap.start_date) FROM ap WHERE ap.id = requests.ap) = year
LEFT JOIN budgets ON budgets.gl = gls.id AND budgets.costCenter = costCenter
GROUP BY gls.id;
END
编辑
这也失败了....
CREATE PROCEDURE fetchBudgetInfo(IN year YEAR, IN costCenter INT(11))
BEGIN
SELECT gls.id, gls.gl, gls.name AS gl_name, budgets.total as totalBudgeted
GROUP BY gls.id;
END
【问题讨论】:
-
你能在 SP 之外作为独立的 sql 运行吗?
-
当我得到神秘错误(我得到它们)时我会做什么.. 删除选择部分的前半部分.. 看看它是否在 SP 之外编译和运行。然后我继续加一半……直到找到让我死去的那一行。
-
如果我删除了有问题的行,我会在下一个 3 块中的下一行得到相同的错误。
-
我试图复制到工作台,但我没有有问题的表。 IFNULL(ROUND(SUM(CASE WHEN requests.ap = (SELECT id FROM ap WHERE ap.ap = 1 AND YEAR(ap.start_date) = year) THEN totalCost ELSE 0 END) / ((SELECT total FROM budgets WHERE costCenter = costCenter AND gl = gls.id AND YEAR(budgets.year) = year)***此处错误(缺少右括号)*** / 12) * 100, 2), 0 此处错误(缺少分号)) AS ap1Ap,在你的代码帖子中......你确实有这个措辞......
-
我应该指出,这个确切的查询适用于 Windows 机器,而不是 Linux 机器
标签: mysql stored-procedures mysql-workbench