简体   繁体   中英

How to insert data from one table to another using transactions in SQL?

This is table where I want to insert data

group_id  | customer_id 
-----------------------
3         |  14
3         |  8
4         |  14

To insert one value code would be like this:

BEGIN TRANSACTION
    INSERT INTO results(group_id INT, customer_id INT)
    VALUES(4, 12)
COMMIT;

What is the correct way to insert multiple customer_id values that I get from complex select statement? Can it be done like this? Maybe it is better to insert data in local temporary table and only then insert in results table?

DECLARE @group_insert INT;
SET @group_insert = 5;

BEGIN TRANSACTION
    INSERT INTO results(group_id INT, customer_id INT)
    SELECT @group_insert, customer_id  ---actual select would have more joins and where conditions
    FROM another_table
    WHERE customer_id > 10
COMMIT;


Results table should look like this after insert:

group_id  | customer_id 
-----------------------
3         |  14
3         |  8
4         |  14
5         |  15
5         |  16
5         |  17
DECLARE @group_insert INT;
SET @group_insert = 5;

BEGIN TRANSACTION
    INSERT INTO results(group_id , customer_id )
    SELECT @group_insert, customer_id  
    FROM another_table
    WHERE customer_id > 10;
COMMIT;

If you want to select group_insert coilumn from another_table then:

BEGIN TRANSACTION
    INSERT INTO results(group_id , customer_id )
    SELECT group_insert, customer_id  
    FROM another_table
    WHERE customer_id > 10;
COMMIT;

I think you may have oversimplified but as a guess here's a code which disallows duplicate cids in another table and a procedure which works out the next id for insert and the min cid for input deduping as it goes.

DROP TABLE IF EXISTS T,T1;
DROP PROCEDURE IF EXISTS P;

CREATE TABLE T (ID INT,CID INT);
CREATE TABLE T1(CID INT PRIMARY KEY);

DROP PROCEDURE IF EXISTS P;
DELIMITER $$
CREATE PROCEDURE P()
BEGIN
 SELECT COALESCE(MAX(ID),0) + 1 INTO @NEXTID FROM T;
 SET @MAXCID = NULL;
 IF @NEXTID = 1 THEN
    SET @MAXCID = 0;
  ELSE
    SELECT MIN(CID) INTO @MAXCID FROM T1 WHERE NOT EXISTS(SELECT CID FROM T WHERE T.CID = T1.CID);
 END IF;
 
 IF @MAXCID IS NOT NULL THEN
  INSERT INTO T
    SELECT @NEXTID,CID
     FROM T1 
     WHERE CID >= @MAXCID;
 END IF;
 
     
END $$
DELIMITER ;
TRUNCATE TABLE T;
TRUNCATE TABLE T1;

INSERT IGNORE INTO T1 VALUES (1);
CALL P();


INSERT IGNORE INTO T1 VALUES (2),(3);
CALL P();


INSERT IGNORE INTO T1 VALUES (2),(3),(4),(5);
CALL P();

 MariaDB [SANDBOX]> select * from t1;
+-----+
| CID |
+-----+
|   1 |
|   2 |
|   3 |
|   4 |
|   5 |
+-----+
5 rows in set (0.002 sec)

MariaDB [SANDBOX]> select * from t;
+------+------+
| ID   | CID  |
+------+------+
|    1 |    1 |
|    2 |    2 |
|    2 |    3 |
|    3 |    4 |
|    3 |    5 |
+------+------+
5 rows in set (0.001 sec)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM