1

In MySQL I'm trying to write a Stored Procedure which does the following:

  1. Run a SELECT query which returns 1 row with 2 columns (so two pieces of information).
  2. Run an INSERT query which includes the two previous values returned and a few parameters which were passed into the stored procedure.

I was originally thinking I could store the two pieces of information from Step 1 into a variable, and then pass those into Step 2. After some reading though it seems that a variable in MySQL can only hold 1 piece of data. How can I get the 2 pieces of information from Step 1 into my INSERT statement in Step 2?

Thank you!

1

3 Answers 3

2

You can use INSERT INTO ... SELECT. Example,

INSERT INTO table1 
SELECT col1, 
       col2, 
       value1, 
       value2 
FROM   table2 

Here you select col1, col2 from table2 and add 2 new custom values value1 and value2 to the result set. This new 4 columns get inserted in to the table1

Sign up to request clarification or add additional context in comments.

Comments

1

You can use SELECT ... INTO clause, which will select values from specified table to variables you define in stored proc.

Refer to manual: http://dev.mysql.com/doc/refman/5.0/en/select-into.html

Comments

0

create something like this:

CREATE PROCEDURE InsertThis(IN paramA varchar(50), IN paramB int, INT paramC varchar(50))
BEGIN
     INTO INTO tableB(fieldA, fieldB, fieldC, fieldD)
     SELECT paramA as fieldA, paramB as fieldB, fieldC, fieldD
     FROM tableA
     WHERE fieldC = paramC;
END

2 Comments

Where condition is not needed here
It's only a sample bro. aside from using LIMIT what else can OP use to display 2 rows?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.