MySQL stored proc to read from a table and insert into another table

Viewed 310

I have 2 databses in the same host. My requirement is to

  1. read data from db1.test table
  2. Massage data. Replace state_cd with state_id from state table
  3. insert into db2.sample
  4. Get the auto generated id after saving the data int sample and store the id in some other table.

Table :- db1.test

CREATE TABLE `db1.test` (
  `Id` int NOT NULL,
  `Name` varchar(100) DEFAULT NULL,
  `Email` varchar(50) DEFAULT NULL,
  `Address1` varchar(100) DEFAULT NULL,
  `Address2` varchar(50) DEFAULT NULL,
  `City` varchar(50) DEFAULT NULL,
  `State` varchar(2) DEFAULT NULL,
  `ZipCode` varchar(10) DEFAULT NULL,
  `Phone` varchar(25) DEFAULT NULL,
  `MobilePhone` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`Id`)
)

db2.sample

CREATE TABLE `db1.sample` (
  `sample_id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(100) DEFAULT NULL,
  `email` varchar(50) DEFAULT NULL,
  `phone` varchar(25) DEFAULT NULL,
  `mobile` varchar(25) DEFAULT NULL,
  `address1` varchar(100) DEFAULT NULL,
  `address2` varchar(50) DEFAULT NULL,
  `city` varchar(50) DEFAULT NULL,
  `state_id` int DEFAULT NULL,
  `zip_code` varchar(10) DEFAULT NULL,
  `created_time` datetime DEFAULT NULL,
  `updated_time` datetime DEFAULT NULL,
  PRIMARY KEY (`sample_id`)
)

db2.state

CREATE TABLE `db1.state` (
      `state_id` int NOT NULL AUTO_INCREMENT,
      `code` char(2) DEFAULT NULL,
      `name` varchar(50) DEFAULT NULL,
      PRIMARY KEY (`state_id`)
    )

Data in db1.test

-----------------------------------------------------------------------
| Id | Name | Email         | Address1     | Address2 | City          | 
--------------------------------------------------------
State | ZipCode |  Phone            | MobilePhone      |
--------------------------------------------------------------------------
| 123 | FName | test@test.com | 1234 Test Dr | Unit 5   | San Fransisco | 
---------------------------------------------------------
CA    |   94020 | +1 (111) 222 3333 | +1 (111) 222 3333 |

Data in db2.state

--------------------------------
| state_id | code | name       |
--------------------------------
| 1        | CA   | California |
--------------------------------
| 1        | NY   | New York   |
--------------------------------

Expected Data in db2.sample


| sample_id | name | email         | address1     | address2 | city          | 
--------------------------------------------------------
state | zip_code |  phone            | mobile     |
--------------------------------------------------------------------------
| 1 | FName | test@test.com | 1234 Test Dr | Unit 5   | San Fransisco | 
---------------------------------------------------------
1    |   94020 | +1 (111) 222 3333 | +1 (111) 222 3333 |

My attempt so far,

DELIMITER //
CREATE PROCEDURE LoadOrganization()
BEGIN
 DECLARE done BOOLEAN DEFAULT false;
 DECLARE test_ID INT;
 DECLARE test_Name VARCHAR(100);
 DECLARE test_Email VARCHAR(50);
 DECLARE test_Address1 VARCHAR(100);
 DECLARE test_Address2 VARCHAR(50);
 DECLARE test_City VARCHAR(50);
 DECLARE test_State VARCHAR(2);
 DECLARE test_ZipCode VARCHAR(10);
 DECLARE test_Phone VARCHAR(25);
 DECLARE test_MobilePhone VARCHAR(25);
 DECLARE sample_ID INT;
 DECLARE cur CURSOR FOR SELECT Id, Name, Email, Address1, Address2, City, State, ZipCode, Phone, MobilePhone 
  FROM db1.test;
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
 
 open cur;
    cur_loop: LOOP
       FETCH cur INTO test_Id, test_Name, test_Email, test_Address1, test_Address2, test_City, test_State, test_ZipCode, test_Phone,  test_MobilePhone;
       IF done THEN
           LEAVE cur_loop;
       END IF;     
       -- cursor loop statements
       select state_id into org_state_id from db2.state where state_cd = practice_state;
       insert into db2.sample(name,email,phone,mobile,address1,address2,city,state_id,zip_code,created_time,updated_time) 
       values(test_Name, test_Email, test_Phone, test_MobilePhone, test_Address1, test_Address2,  test_City, org_state_id, test_ZipCode, now(),now());
    end LOOP;
 close cur;
END //

DELIMITER ;

call LoadOrganization();

It works as expected.

How to get the inserted auto generated key from db2.sample without a select statement as the columns are not unique?

0 Answers
Related