MySQL存储过程,其参数从Select声明

时间:2014-11-16 19:00:17

标签: mysql opencart

我正在尝试为我的Opencart商店创建MySQL存储过程。为了使产品可见,它必须在3个表中

  1. oc_product
  2. oc_product_description
  3. oc_product_to_store
  4. 所以我插入第一张表,收集ID&使用该ID插入下表。

    如果我在存储过程之外执行此操作,则所有单个步骤都可以正常工作。但是,一旦我把它们放在一起(下面),我就会收到这个错误:

    #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET @vProductID = (SELECT `product_id` FROM `oc_product` WHERE UPC='1234567890' ' at line 9
    

    我的存储过程如下所示:

    CREATE DEFINER=`myname`@`localhost` PROCEDURE `uspAddNewProduct`(IN `vModel` VARCHAR(500), IN `vSKU` VARCHAR(500), IN `vUPC` VARCHAR(500), IN `vMPN` VARCHAR(500), IN `vQTY` INT(10), IN `vPrice` DECIMAL(18,2), IN `vProductID` INT, IN `vProductName` VARCHAR(500), IN `vProductDescription` VARCHAR(500), IN `vMetaTitle` VARCHAR(500))
    
    
    -- INSERTS THE BULK OF THE DATA INTO THE MAIN PRODUCT TABLE
    INSERT INTO `oc_product` (`model`, `sku`, `upc`, `mpn`, `location`, `quantity`, `stock_status_id`, `image`, `shipping`, `price`, `tax_class_id`,`date_available`, `minimum`, `status`, `date_added`, `date_modified`, `weight_class_id`)
    VALUES (vModel,vSKU,vUPC,vMPN,'NYC',vQTY,'5','catalog/product/' + vUPC + '.jpg','1',vPrice,'9',NOW(),'1','1',NOW(),NOW(),'1')
    
    -- SELECTS THE RECENTLY CREATED PRODUCT ID
    SET @vProductID = (SELECT `product_id` FROM `oc_product` WHERE UPC='1234567890' ORDER BY   `product_id` DESC LIMIT 1)
    
    -- INSERTS INTO A DESCRIPTION TABLE (BASED ON THE PID ABOVE)
    INSERT INTO `oc_product_description` (`product_id`, `language_id`, `name`, `description`, `meta_title`)
    VALUES (vProductID, '1', vProductName, vProductDescription, vMetaTitle)
    
    -- ASSOCIATES THE PRODUCT WITH THE MAIN STORE (BASED ON THE PID ABOVE)
    INSERT INTO  `oc_product_to_store` (`product_id`, `store_id`) VALUES (vProductID, '1')
    

2 个答案:

答案 0 :(得分:0)

您可以使用以下任何一种

SET vProductID = (SELECT `product_id` 
FROM `oc_product` 
WHERE UPC='1234567890' 
ORDER BY `product_id` DESC LIMIT 1)

SELECT `product_id` into vProductID 
FROM `oc_product` 
WHERE UPC='1234567890' 
ORDER BY `product_id` DESC LIMIT 1

答案 1 :(得分:0)

注意变量vProductID,是一个存储过程参数,然后在SET中重新定义。请注意,vProductID@vProductID是不同的变量。

尝试:

DELIMITER //

CREATE DEFINER=`myname`@`localhost` PROCEDURE `uspAddNewProduct`(
  IN `vModel` VARCHAR(500),
  IN `vSKU` VARCHAR(500),
  IN `vUPC` VARCHAR(500),
  IN `vMPN` VARCHAR(500),
  IN `vQTY` INT(10),
  IN `vPrice` DECIMAL(18,2),
  IN `vProductID` INT,
  IN `vProductName` VARCHAR(500),
  IN `vProductDescription` VARCHAR(500),
  IN `vMetaTitle` VARCHAR(500)
)
BEGIN
  -- INSERTS THE BULK OF THE DATA INTO THE MAIN PRODUCT TABLE
  INSERT INTO `oc_product` (
    `model`,
    `sku`,
    `upc`,
    `mpn`,
    `location`,
    `quantity`,
    `stock_status_id`,
    `image`,
    `shipping`,
    `price`,
    `tax_class_id`,
    `date_available`,
    `minimum`,
    `status`,
    `date_added`,
    `date_modified`,
    `weight_class_id`
  ) VALUES (
    `vModel`,
    `vSKU`,
    `vUPC`,
    `vMPN`,
    'NYC',
    `vQTY`,
    '5',
    CONCAT('catalog/product/', `vUPC`, '.jpg'),
    '1',
    `vPrice`,
    '9',
    NOW(),
    '1',
    '1',
    NOW(),
    NOW(),
    '1'
  );

  -- SELECTS THE RECENTLY CREATED PRODUCT ID
  -- SET @vProductID = (SELECT `product_id` FROM `oc_product` WHERE UPC='1234567890' ORDER BY   `product_id` DESC LIMIT 1)
  SET `vProductID` := (
              SELECT `product_id`
              FROM `oc_product`
              WHERE `UPC` = '1234567890'
              ORDER BY `product_id` DESC LIMIT 1
  );

  -- INSERTS INTO A DESCRIPTION TABLE (BASED ON THE PID ABOVE)
  INSERT INTO `oc_product_description` (
    `product_id`,
    `language_id`,
    `name`, `description`,
    `meta_title`
  ) VALUES (
    `vProductID`,
    '1',
    `vProductName`,
    `vProductDescription`,
    `vMetaTitle`
  );

  -- ASSOCIATES THE PRODUCT WITH THE MAIN STORE (BASED ON THE PID ABOVE)
  INSERT INTO `oc_product_to_store` (
    `product_id`,
    `store_id`
  ) VALUES (
    `vProductID`,
    '1'
  );
END//

DELIMITER ;