gpt4 book ai didi

MySQL 存储过程错误代码 : 1064

转载 作者:行者123 更新时间:2023-11-29 18:02:19 25 4
gpt4 key购买 nike

我正在尝试创建存储过程并收到错误:

Error Code: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'DECLARE @LoopCounter INT DEFAULT 0; DECLARE @MaxId INT DEFAULT 0; DECLARE ' at line 21

DELIMITER $$
USE dollar$$
DROP PROCEDURE IF EXISTS sp_get_products_google_feed$$
CREATE DEFINER=root@localhost PROCEDURE sp_get_products_google_feed()
BEGIN
DROP TABLE IF EXISTS tmp_Product_List;
CREATE TEMPORARY TABLE tmp_Product_List(
SELECT DISTINCT p.products_id AS PID, p.products_model AS ID, pd.products_name AS Title, pd.products_description AS Description,
'' AS Google_product_category, '' AS product_type, p.products_model AS link, p.products_image AS Image_link,
'new' AS Condition1, 'in stock' AS Availability, p.products_price AS Price, '' AS Sale_Price, '' AS Sale_price_effective_date,
p.products_upc AS GTin, p.manufacturers_id, '' AS MPN, '' AS Item_group_id, '' AS Gender, '' AS Age_group, '' AS Color, '' AS Size,
'Free' AS Shipping, '' AS Shipping_Weight
FROM
zc_products_to_categories pc, zc_products p, zc_products_description pd WHERE
pc.categories_id IN
(SELECT DISTINCT mg.sub_category_id AS id FROM tbl_map_google_category_master mg WHERE mg.category_id = 1
UNION
SELECT DISTINCT mg.sub_sub_category_id AS id FROM tbl_map_google_category_master mg WHERE mg.category_id = 1
ORDER BY id) AND
p.products_id = pc.products_id AND
p.products_id = pd.products_id AND
p.products_status = 1 ORDER BY PID);

DECLARE @LoopCounter INT DEFAULT 0;
DECLARE @MaxId INT DEFAULT 0;
DECLARE @GoogleCategoryid INT DEFAULT 0;

SELECT @LoopCounter = MIN(PID), @MaxId = MAX(PID) FROM tmp_Product_List;

WHILE (@LoopCounter IS NOT NULL AND @LoopCounter <= @MaxId)
BEGIN
SELECT @GoogleCategoryid = google_category_id FROM tbl_map_google_category_master
WHERE
category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = @LoopCounter) OR
sub_category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = @LoopCounter) OR
sub_sub_category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = @LoopCounter) LIMIT 0,1;

UPDATE tmp_Product_List SET Google_product_category = @GoogleCategoryid WHERE products_id = @LoopCounter;

SET @LoopCounter = @LoopCounter + 1
IF(@@ROWCOUNT = 0 )
BEGIN
SET @LoopCounter = @LoopCounter + 1
CONTINUE
END
END

SELECT * FROM tmp_Product_List;
END$$
DELIMITER ;

但是如果我从脚本中删除以下代码,它就会成功运行。尝试在完整脚本中查找错误,但没有成功。

            DECLARE @LoopCounter INT DEFAULT 0;
DECLARE @MaxId INT DEFAULT 0;
DECLARE @GoogleCategoryid INT DEFAULT 0;

SELECT @LoopCounter = MIN(PID), @MaxId = MAX(PID) FROM tmp_Product_List;

WHILE (@LoopCounter IS NOT NULL AND @LoopCounter <= @MaxId)
BEGIN
SELECT @GoogleCategoryid = google_category_id FROM tbl_map_google_category_master
WHERE
category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = @LoopCounter) OR
sub_category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = @LoopCounter) OR
sub_sub_category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = @LoopCounter) LIMIT 0,1;

UPDATE tmp_Product_List SET Google_product_category = @GoogleCategoryid WHERE products_id = @LoopCounter;

SET @LoopCounter = @LoopCounter + 1
IF(@@ROWCOUNT = 0 )
BEGIN
SET @LoopCounter = @LoopCounter + 1
CONTINUE
END
END

最佳答案

我已经解决了您遇到的一些问题。

第一个是在变量声明前加上@符号,我已经删除了它们。

第二个是在“WHILE ... BEGIN ... END”中。语法为 WHILE ... DO ... END WHILE。

第三个是 IF(@@ROWCOUNT) 之前缺少的分号。

下面我还没有修复的最后一个问题是 @@ROWCOUNT 的使用。 @@ROWCOUNT 不是 MySQL 中的变量。您可以找到替代方案here

DELIMITER $$
USE dollar$$
DROP PROCEDURE IF EXISTS sp_get_products_google_feed$$
CREATE DEFINER=root@localhost PROCEDURE sp_get_products_google_feed()
BEGIN
DECLARE LoopCounter INT DEFAULT 0;
DECLARE MaxId INT DEFAULT 0;
DECLARE GoogleCategoryid INT DEFAULT 0;
DROP TABLE IF EXISTS tmp_Product_List;
CREATE TEMPORARY TABLE tmp_Product_List(
SELECT DISTINCT p.products_id AS PID, p.products_model AS ID, pd.products_name AS Title, pd.products_description AS Description,
'' AS Google_product_category, '' AS product_type, p.products_model AS link, p.products_image AS Image_link,
'new' AS Condition1, 'in stock' AS Availability, p.products_price AS Price, '' AS Sale_Price, '' AS Sale_price_effective_date,
p.products_upc AS GTin, p.manufacturers_id, '' AS MPN, '' AS Item_group_id, '' AS Gender, '' AS Age_group, '' AS Color, '' AS Size,
'Free' AS Shipping, '' AS Shipping_Weight
FROM
zc_products_to_categories pc, zc_products p, zc_products_description pd WHERE
pc.categories_id IN
(SELECT DISTINCT mg.sub_category_id AS id FROM tbl_map_google_category_master mg WHERE mg.category_id = 1
UNION
SELECT DISTINCT mg.sub_sub_category_id AS id FROM tbl_map_google_category_master mg WHERE mg.category_id = 1
ORDER BY id) AND
p.products_id = pc.products_id AND
p.products_id = pd.products_id AND
p.products_status = 1 ORDER BY PID);



SELECT LoopCounter = MIN(PID), MaxId = MAX(PID) FROM tmp_Product_List;

WHILE (LoopCounter IS NOT NULL AND LoopCounter <= MaxId)
DO
SELECT GoogleCategoryid = google_category_id FROM tbl_map_google_category_master
WHERE
category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = LoopCounter) OR
sub_category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = LoopCounter) OR
sub_sub_category_id = (SELECT MAX(categories_id) FROM zc_products_to_categories WHERE products_id = LoopCounter) LIMIT 0,1;

UPDATE tmp_Product_List SET Google_product_category = GoogleCategoryid WHERE products_id = LoopCounter;

SET LoopCounter = LoopCounter + 1;
IF(@@ROWCOUNT = 0 )
BEGIN
SET LoopCounter = LoopCounter + 1
CONTINUE
END
END WHILE

SELECT * FROM tmp_Product_List;
END$$
DELIMITER ;

关于MySQL 存储过程错误代码 : 1064,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48227364/

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