mysql使用存储过程insert

delimiter //
CREATE PROCEDURE test1 () BEGIN
	DECLARE
		v_skuid INT;
	
	SET v_skuid = 10000101;
	WHILE
			v_skuid < 50110811 DO
			INSERT INTO s_search_index_goods (
				goods_id,
				sku_id,
				pharmacy_id,
				city,
				pharmacy_name,
				cooperate_type,
				pharmacy_model,
				product_id,
				quantity,
				is_import,
				base_org_code,
				otc_can_buy,
				STATUS,
				pharmacy_type,
				price,
				freeze_size,
				market_price,
				is_delete,
				brand_id,
				b2c_directory_id,
				pop_third_id,
				is_promotion,
				otc_mark,
				suitable_people,
				is_sync,
				sales_volume,
				package_specifications,
				approval_number,
				product_priority,
				created_at,
				change_at 
			) SELECT
			g.id goodsId,
			g.sku_id skuId,
			g.pharmacy_id shopId,
			p.city,
			p.NAME shopName,
		CASE
				
				WHEN p.pharmacy_model = 3 THEN
				- 1 * p.cooperate_type ELSE p.cooperate_type 
			END cooperateType,
	p.pharmacy_model pharmacyModel,
	g.product_id productId,
	g.quantity,
	a.is_import isImport,
	a.base_org_code baseOrgCode,
CASE
		
		WHEN d.otc_can_buy IS NOT NULL THEN
		d.otc_can_buy ELSE a.otc_can_buy 
	END otcCanBuy,
CASE
		
		WHEN p.STATUS = 1 THEN
		g.STATUS ELSE 1 
	END STATUS,
	p.pharmacy_type pharmacyType,
	g.price price,
	g.freeze_size freezeSize,
	u.market_price marketPrice,
	g.is_delete isDelete,
	a.brand_id brandId,
	a.b2c_directory_id thridCategoryId,
	pop.pop_directory_id popThirdId,
	g.is_promotion isPromotion,
	a.otc_mark otcMark,
	a.suitable_people suitablePeople,
	a.is_sync isSync,
	c.sales_volume saleVolume,
	u.package_specifications packageSpec,
	u.approval_number approvalNumber,
	a.priority productPriority,
	now(),
	now() 
FROM
	c_pharmacy_goods g
	LEFT JOIN p_pop_directory_product pop ON pop.sku_id = g.sku_id 
	AND pop.pharmacy_id = g.pharmacy_id
	LEFT JOIN p_product a ON g.product_id = a.id
	INNER JOIN p_sku u ON u.id = g.sku_id 
	AND u.using_attributes IN ( 1, 3, 4 ) 
	AND u.STATUS = 1
	LEFT JOIN c_sales_volume c ON c.p_sku_id = g.sku_id
	LEFT JOIN p_pharmacy p ON g.pharmacy_id = p.id
	LEFT JOIN s_search_otc_canbuy d ON p.city = d.city 
	AND g.product_id = d.product_id 
	AND g.sku_id = d.sku_id 
WHERE
	g.product_id > 0 
	AND g.sku_id = v_skuid;

SET i = i + 1;
COMMIT;

END WHILE;

END // 
delimiter;

  

posted on 2021-09-16 16:14  伸展代码舒适区  阅读(488)  评论(0编辑  收藏  举报

导航