I have modified the code from @Shadow. you must also include the schema name.
and also put it in backquotes to prevent spaces in the fieldnames.
...
DECLARE s_sql VARCHAR(1000); --length should depend ON how many fields you have
SELECT
CONCAT(
'insert into tablename ('
,GROUP_CONCAT(CONCAT('`',COLUMN_NAME,'`'))
,') values ('
,GROUP_CONCAT(CONCAT('`OLD`.`',COLUMN_NAME,'`')),
')'
)
INTO s_sql
FROM information_schema.columns
WHERE
TABLE_NAME='your_table'
AND
TABLE_SCHEMA='your_schema'
GROUP BY TABLE_NAME;
prepare stmt from s_sql;
execute stmt;
deallocate prepare stmt;
...
sample
MariaDB [your_schema]> SELECT
-> CONCAT(
-> 'insert into tablename ('
-> ,GROUP_CONCAT(CONCAT('`',COLUMN_NAME,'`'))
-> ,') values ('
-> ,GROUP_CONCAT(CONCAT('`OLD`.`',COLUMN_NAME,'`')),
-> ')'
-> )
-> INTO @s_sql
-> FROM information_schema.columns
-> WHERE
-> TABLE_NAME='your_table'
-> AND
-> TABLE_SCHEMA='your_schema'
-> GROUP BY TABLE_NAME;
Query OK, 1 row affected (0.00 sec)
MariaDB [your_schema]> select @s_sql;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| @s_sql |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| insert into tablename (`sr_no`,`scholar_no`,`paid_amount`,`due_amount`,`fee_date`,`section_id_fk`) values (`OLD`.`sr_no`,`OLD`.`scholar_no`,`OLD`.`paid_amount`,`OLD`.`due_amount`,`OLD`.`fee_date`,`OLD`.`section_id_fk`) |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
MariaDB [your_schema]>