I need to insert a row to 2 tables with matching ids. When inserting to groups table, the id is auto increased.
I want to insert a new row in 'groups' table, get the id from the inserted row and then use it to insert a new row to the 'users_in_groups' table.
What I have:
INSERT INTO groups (name, owner_id ) VALUES ('groupname1', 'userid1'); INSERT INTO users_in_groups (user_id, group_id) VALUES ('userid1', group_id_from_above); I tried
INSERT INTO groups (name, owner_id ) OUTPUT Inserted.id VALUES ('group1', 'user1'); But I do not know how to make the 2 'insert' work on 1 query. Is it even possible?
OUTPUTsyntax is SQL Server.