我正在尝试在表中输入一行,该行从其他几个表中收集信息以及外部参数。存储过程应该看起来像这样,尽管要长得多:
DROP PROCEDURE IF EXISTS sp1;
DELIMITER $$
CREATE PROCEDURE sp1(IN para1 INT, IN para2 INT,IN para3 INT)
BEGIN
INSERT INTO table100 VALUES(
NULL, #AaS_id, Auto_Increment
DEFAULT, #AaS_ts, Def
SELECT info1 FROM table2 WHERE id1 = para1,
SELECT info2 FROM table3 WHERE id2 = para2,
para3);
END$$
DELIMITER ;
SQL不喜欢“ SELECT”。有什么帮助,因为这种方法行不通?确实如此。
答案 0 :(得分:1)
一种选择是将两个子查询的结果存储到变量中,然后将它们插入:
DROP PROCEDURE IF EXISTS sp1;
DELIMITER $$
CREATE PROCEDURE sp1 (IN para1 INT, IN para2 INT,IN para3 INT)
BEGIN
DECLARE i1 VARCHAR(100)
DECLARE i2 VARCHAR(100)
SELECT info1 INTO i1 FROM table2 WHERE id1 = para1
SELECT info2 INTO i2 FROM table3 WHERE id2 = para2
INSERT INTO table100
VALUES (NULL, DEFAULT, i1, i2, para3);
END$$
DELIMITER ;