--创建a表
CREATE TABLE a
(aid int,
adsc varchar(50)
);
--创建b表
CREATE TABLE a
(bid int,
bdsc varchar(50)
);
--插入数据
insert into a values(1,'a1');
insert into a values(2,'a2');
insert into a values(3,'a3');
insert into a values(4,'a4');
insert into a values(5,'a5');
--创建存储过程,将a表数据插入b表
CREATE PROCEDURE test()
BEGIN
DECLARE v_a INT;
DECLARE v_b VARCHAR;
SELECT aid INTO v_a FROM a;
IF v_a=1 THEN SET v_b='b1'
ELSEIF v_a=2 THEN SET v_b='b1'
ELSE v_b='bn'
END IF;
INSERT INTO b VALUES (v_a,v_b);
END;
运行提示报错:
[Err] 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 '; SELECT aid INTO v_a FROM a; IF v_a=1 THEN v_b='b1' ELSEIF v_a=2 THEN SET v' at line 4
------解决方案-------------------- DELIMITER $$ DROP PROCEDURE IF EXISTS test$$ CREATE PROCEDURE test() BEGIN DECLARE v_a INT; DECLARE v_b VARCHAR(10); SELECT aid INTO v_a FROM a; IF v_a=1 THEN SET v_b='b1' ; ELSEIF v_a=2 THEN SET v_b='b1'; ELSE SET v_b='bn'; END IF; INSERT INTO b VALUES(v_a,v_b); END$$ DELIMITER ;
------解决方案-------------------- 创建表b那块是复制错误吧?另外我也不知道对不对,varchar后面是不是必须跟着大小?比如(20)什么的
------解决方案--------------------