I have two tables with identical schemas, I want to copy the newly inserted record from table1 to table2 in a trigger, my code is below and works, but only copies the id field
BEGIN
insert into Table2 (id) values (new.id);
END
However the table many columns, is there a way to copy the entire record into the table2 without specifying all the columns and values, like below
BEGIN
insert into Table2 SELECT * FROM NEW;
END
Thanks
Your second SQL is fine but you have to do one query first for using the star in your select into :
SET IDENTITY_INSERT NEW ON
And then :
BEGIN
insert into Table2 SELECT * FROM NEW;
END
And dont forget to put it OFF at the end
EDIT 1
If it's not working, maybe try to put some priority on your sql :
INSERT INTO `Table2` (SELECT * FROM `NEW`);
EDIT 2
If it's still not working, maybe you could try to create the table with all the element directly :
CREATE TABLE Table2 [AS] SELECT * FROM NEW;
这有效
insert into Table2 SELECT * FROM Table1 where id=new.id;
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.