DB2 Loop Insert: Add Large Data Efficiently
In DB2, you can use looping statements and INSERT statements to add large amounts of data. Here is an example code demonstrating how to use a loop to add data to a table.
-- 创建一个测试表
CREATE TABLE test_table (
id INT,
name VARCHAR(50)
);
-- 设置循环次数
DECLARE i INT DEFAULT 1;
DECLARE total_rows INT DEFAULT 1000;
-- 开始循环插入数据
WHILE i <= total_rows DO
INSERT INTO test_table (id, name) VALUES (i, 'Name ' || i);
SET i = i + 1;
END WHILE;
-- 输出成功信息
SELECT 'Data inserted successfully' AS message;
In the example above, a table named “test_table” is first created and then 1000 pieces of data are inserted into the table using a loop statement. Within the loop, each time data is inserted, the “id” field increases by 1 and the “name” field is set to “Name” followed by the current loop count. After all the data has been inserted, a success message is displayed.
Please adjust the number of iterations, table structure, and data insertion logic according to actual needs.