'Insert Into using a variable with multiple values as a parameter
I'm having trouble using my array as a parameter. I've imploded my array so it will display values with parentheses around them for example, list1 = (1),(2),(3)
BEGIN
CREATE TEMPORARY TABLE tmp (id Int NOT NULL PRIMARY KEY);
INSERT INTO tmp (id) VALUES list1; //wont compile procedure
INSERT INTO tmp (id) VALUES (list1); //compiles however I can't put my array because the parentheses around the parameter ?
select * from tmp
END
Parameter -> Direction = IN, Name = list1, Type = Varchar, Length = 8000, Options=Charset
I'm not sure what I'm doing wrong, any help would greatly be appreciated
UPDATE:
I dont think your understanding what I'm trying to accomplish. I have multiple values in list1 that im trying to insert. list1= (1),(2),(3) However the compiler only allows me to use list1 with parentheses around it even though I padded my string already with the neccessary parentheses. So then I tried using '1),(2),(3' but it only outputs 1 which is the first value
I dont have a problem insert into a temporary table, I have a problem using an array(list1) for the insertion....
Solution 1:[1]
Not knowing the full context, I don't know if this is the best solution, but it is a solution.
I've had success in similar case by using prepared statement. In your case, it could look like
SET @stmt := CONCAT("INSERT INTO t1 (id) VALUES ", list1);
PREPARE p_stmt FROM @stmt;
EXECUTE p_stmt;
DEALLOCATE PREPARE p_stmt;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Richard St-Cyr |
