I'm trying to insert one record into Postgresql, get inserted id, and use it in following 4 other inserts. I can't get it to work. This is what I have so far:
do $$
declare
gameId integer
begin
INSERT INTO public."Games" ("SeasonId", "StartTime", "GameStatusId", "AwayTeamId", "HomeTeamId", "IsVenueNeutral", "ModifiedOn", "Comment")
values(15, '2021-05-20 22:30:00.000', 1, 11, 12, false, '2022-01-29 20:20:00.000', 'Test')
returning "GameId" into gameId ;
INSERT INTO public."GameScores" ("GameId", "Period", "AwayScore", "HomeScore")
VALUES(gameId , 1, 10, 10),
VALUES(gameId , 2, 10, 10),
VALUES(gameId , 3, 10, 10),
VALUES(gameId , 4, 10, 10);
end $$;
I'm not even sure if I need to wrap it in do and end. All I want to is to generate a script to insert a bunch of data in one go. In some cases I need to reuse id of previously inserted record.
Thanks for help