I want to insert data into multiple related tables in one go - in a single transaction. I am using Postgres and Dapper ORM. Here is my query:
WITH userins AS (
INSERT INTO public."user"
(
"FirstName",
"LastName",
"Email",
"PasswordHash",
"Address",
"City",
"State",
"Zip",
"Phone",
"IsEnabled",
"IsVerified",
"IsDeleted",
"CreatedDate",
)
VALUES (@FirstName, @LastName, @Email, @PasswordHash, @Address, @City, @State, @Zip, @Phone, @IsEnabled, ?IsVerified, ?IsDeleted, @CreatedDate);
RETURNING id AS user_id
)
, clientins AS (
INSERT INTO public.client( "Description", "PlanCustomerId", "IsActive", "IsDeleted", "CreatedBy", "CreatedDate", userid)
VALUES(@Description, @PlanCustomerId, @IsActive,@IsDeleted, user_id, @CreatedDate,user_id) RETURNING id as client_id;
)
, clientsubins
(
INSERT INTO public.client_subscription(
"PlanId",
"IsActive",
"StartDate",
"EndDate",
"IsDeleted",
"CreatedBy",
"CreatedDate",
clientid,
subscriptionid)
VALUES (@PlanId, @IsActive, @StartDate, @EndDate, @IsDeleted, user_id, @CreatedDate, client_id, @subscriptionid);
)
RETURNING client_id
Will this query work or what I need to change?