mysql 添加程序,多值插入工程在MySQL程序?

Multivalue insert example - it works manually but NOT in mySQL stored procedure.

INSERT INTO input_data1(mobile) VALUES (9619825525),(9619825255),(9324198256),(9013000002),(9999999450),(9999999876) ;

i am getting syntax error near "str" word in below proc, Can any one let me know how to implement this multi value INSERT work in procedure?

DELIMITER |

DROP PROCEDURE IF EXISTS mobile_series1;

CREATE PROCEDURE mobile_series1(IN str text)

LANGUAGE SQL READS SQL DATA

BEGIN

DROP TABLE IF EXISTS input_data1 ;

CREATE TEMPORARY TABLE input_data1 (mobile varchar(1000)) engine=memory;

INSERT INTO input_data1(mobile) VALUES str;

END |

DELIMITER ;

Thanks in Advance.

解决方案

I don't have a MySQL server so there's probably syntax errors and +1 errors (i.e. may not be capturing the last on the list, may not progress past the first item etc, problems fixed by putting a +1 in the code), but you basically want to replace your INSERT statement with something this.

DECLARE INT _CURSOR 0;

DECLARE INT _TOKENLENGTH 0;

DECLARE VARCHAR _TOKEN NULL;

SELECT LOCATE(str, ",", _CURSOR) - _CURSOR INTO _TOKENLENGTH;

LOOP

IF _TOKENLENGTH <= 0 THEN

SELECT RIGHT(str, _CURSOR) INTO _TOKEN;

INSERT INTO input_data1(mobile) VALUE _TOKEN;

LEAVE;

END IF;

SELECT SUBSTRING(str, _CURSOR, _TOKENLENGTH) INTO _TOKEN;

INSERT INTO input_data1(mobile) VALUE _TOKEN;

SELECT _CURSOR + _TOKENLENGTH + 1 INTO _CURSOR;

SELECT LOCATE(str, ",", _CURSOR + 1) - _CURSOR INTO _TOKENLENGTH;

END LOOP;

Your function call would then be something like

EXEC mobile_series1('9619825525,9619825255,9324198256')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值