Friday, January 17, 2025

How do you insert the same record multiple times in SQL?

 To insert the same record multiple times in SQL, you can use the INSERT INTO statement in combination with multiple VALUES clauses. Each VALUES clause will represent one record. Here's an example:

INSERT INTO table_name (column1, column2, column3)
VALUES
    (value1, value2, value3),
    (value1, value2, value3),
    (value1, value2, value3);

In this example, you are inserting the same set of values (value1, value2, value3) three times into the table_name.

If you want to insert a record multiple times in different queries (for example, for a batch operation), you can use a LOOP in a stored procedure or script to repeat the insert operation. For example, in MySQL:

DELIMITER $$

CREATE PROCEDURE insert_multiple_records()
BEGIN
    DECLARE counter INT DEFAULT 1;
    
    WHILE counter <= 10 DO
        INSERT INTO table_name (column1, column2, column3)
        VALUES (value1, value2, value3);
        
        SET counter = counter + 1;
    END WHILE;
END $$

DELIMITER ;

This will insert the same record 10 times into the table_name.

No comments:

Post a Comment