用户在mysql pocedure中定义的变量增量

时间:2018-05-31 12:22:07

标签: mysql

我正在尝试在mysql过程中使用用户定义的变量。它正在递增并根据值递减。两个select语句在其中连接并且变量在两个语句中使用。此外,函数将是在赋值变量中放置而不是静态值。任何人都可以帮助我犯错的地方?由于此错误,我无法创建程序。 谢谢。

错误: #1064 - 您的SQL语法出错;检查与MySQL服务器版本对应的手册,以便在第59行的“END”附近使用正确的语法

DELIMITER $$
CREATE DEFINER=`local`@`localhost` PROCEDURE `sp_select_customer_account_lesuire`(IN `date_from` VARCHAR(10), IN `date_to` VARCHAR(10), IN `account_id` INT(11) UNSIGNED, IN `company_name` INT(11) UNSIGNED, IN `start_row` INT(11) UNSIGNED, IN `end_row` INT(11) UNSIGNED)
    NO SQL
BEGIN
SET @runningBalance := -199999; /*need a function for dynamic value*/
SELECT
c.`customer_account_id` as 'id',
c.`received` as 'date',
c.`description` as 'desc',
'' as 'sales_type',
NULL as 'sales_us',
NULL as 'sales_rate',
NULL as 'sales_yen',
CONCAT('$ ',FORMAT(c.`usd_amount`,0)) as 'us',
c.`rate` as 'rate',
CONCAT('¥ ',FORMAT(c.`yen_amount`,0)) as 'yen',
(@runningBalance := @runningBalance-c.`yen_amount`) as 'balance'

FROM `customer_account` c

LEFT JOIN `customer_detail` cd ON
cd.`customer_detail_id` = c.`customer_detail_id`

WHERE
(
    (account_id='' OR c.customer_account_id=account_id)
    AND
    (cd.customer_detail_id=company_name)
    AND
    (c.received BETWEEN (case WHEN (date_from="" OR date_from="0000-00-00") THEN '1990-01-01' ELSE date_from END) AND (case WHEN (date_to="" OR date_to="0000-00-00") THEN CURRENT_DATE ELSE date_from END))
)
GROUP BY c.customer_account_id
UNION
SELECT
cs.`invoice_id`,
cs.`sales_date`,
CONCAT('INV#',cs.`invoice_no`),
(case WHEN cs.`sales_type`='1' THEN 'FOB' WHEN cs.`sales_type`='2' THEN 'C&F' WHEN cs.`sales_type`='3' THEN 'CIF' WHEN cs.`sales_type`='4' THEN 'Dealer' WHEN cs.`sales_type`='5' THEN 'Auction'END),
case WHEN cs.`currency`='usd' THEN CONCAT('$ ',FORMAT((cs.`grand_total`/cs.`exchange_rate`),0)) ELSE NULL END,
case WHEN cs.`exchange_rate` > 1 THEN cs.`exchange_rate` ELSE NULL END,
CONCAT('¥ ',FORMAT(cs.`grand_total`,0)),
NULL,
NULL,
NULL,
(@runningBalance := @runningBalance+cs.`grand_total`)

FROM `car_sales` cs

LEFT JOIN `customer_detail` cd ON
cd.`customer_detail_id` = cs.`buyer_name`

WHERE 
(
    (cd.customer_detail_id=company_name)
    AND
    (cs.sales_date BETWEEN (case WHEN (date_from="" OR date_from="0000-00-00") THEN '1990-01-01' ELSE date_from END) AND (case WHEN (date_to="" OR date_to="0000-00-00") THEN CURRENT_DATE ELSE date_from END))
)
ORDER BY 2 ASC
LIMIT start_row, end_row
END$$
DELIMITER ;

2 个答案:

答案 0 :(得分:0)

你错过了;在LIMIT线上,在END之前。

LIMIT start_row, end_row;

答案 1 :(得分:0)

将半号放在END;

   DELIMITER $$
CREATE DEFINER=`local`@`localhost` PROCEDURE `sp_select_customer_account_lesuire`(IN `date_from` VARCHAR(10), IN `date_to` VARCHAR(10), IN `account_id` INT(11) UNSIGNED, IN `company_name` INT(11) UNSIGNED, IN `start_row` INT(11) UNSIGNED, IN `end_row` INT(11) UNSIGNED)
    NO SQL
BEGIN
SET @runningBalance := -199999; /*need a function for dynamic value*/
SELECT
c.`customer_account_id` AS 'id',
c.`received` AS 'date',
c.`description` AS 'desc',
'' AS 'sales_type',
NULL AS 'sales_us',
NULL AS 'sales_rate',
NULL AS 'sales_yen',
CONCAT('$ ',FORMAT(c.`usd_amount`,0)) AS 'us',
c.`rate` AS 'rate',
CONCAT('¥ ',FORMAT(c.`yen_amount`,0)) AS 'yen',
(@runningBalance := @runningBalance-c.`yen_amount`) AS 'balance'

FROM `customer_account` c

LEFT JOIN `customer_detail` cd ON
cd.`customer_detail_id` = c.`customer_detail_id`

WHERE
(
    (account_id='' OR c.customer_account_id=account_id)
    AND
    (cd.customer_detail_id=company_name)
    AND
    (c.received BETWEEN (CASE WHEN (date_from="" OR date_from="0000-00-00") THEN '1990-01-01' ELSE date_from END) AND (CASE WHEN (date_to="" OR date_to="0000-00-00") THEN CURRENT_DATE ELSE date_from END))
)
GROUP BY c.customer_account_id
UNION
SELECT
cs.`invoice_id`,
cs.`sales_date`,
CONCAT('INV#',cs.`invoice_no`),
(CASE WHEN cs.`sales_type`='1' THEN 'FOB' WHEN cs.`sales_type`='2' THEN 'C&F' WHEN cs.`sales_type`='3' THEN 'CIF' WHEN cs.`sales_type`='4' THEN 'Dealer' WHEN cs.`sales_type`='5' THEN 'Auction'END),
CASE WHEN cs.`currency`='usd' THEN CONCAT('$ ',FORMAT((cs.`grand_total`/cs.`exchange_rate`),0)) ELSE NULL END,
CASE WHEN cs.`exchange_rate` > 1 THEN cs.`exchange_rate` ELSE NULL END,
CONCAT('¥ ',FORMAT(cs.`grand_total`,0)),
NULL,
NULL,
NULL,
(@runningBalance := @runningBalance+cs.`grand_total`)

FROM `car_sales` cs

LEFT JOIN `customer_detail` cd ON
cd.`customer_detail_id` = cs.`buyer_name`

WHERE 
(
    (cd.customer_detail_id=company_name)
    AND
    (cs.sales_date BETWEEN (CASE WHEN (date_from="" OR date_from="0000-00-00") THEN '1990-01-01' ELSE date_from END) AND (CASE WHEN (date_to="" OR date_to="0000-00-00") THEN CURRENT_DATE ELSE date_from END))
)
ORDER BY 2 ASC
LIMIT start_row, end_row;
END$$
DELIMITER ;