-2

I have 2 table is USER and USER_CUSTOMER and I want to write a insert query in DAO class to create a new user but don't know how.

This is the table relationship

Anyone have a good idea or have a link to solve this problem please help

Dale K
  • 25,246
  • 15
  • 42
  • 71
ImNewBie
  • 11
  • 1
  • 1
    To insert two records "at the same time": `INSERT`, `INSERT`, `COMMIT`. Nobody else can see either insert until commit statement, and both inserts will appear at the same time. They either both succeed, or neither does (assuming correct `ROLLBACK` handling). – Andreas Mar 04 '19 at 05:55
  • See: [What is a database transaction?](https://stackoverflow.com/q/974596/5221149) – Andreas Mar 04 '19 at 05:57

1 Answers1

0

If you want to handle this at the database level, then an after insert trigger, which fires after the new record has been inserted into the USER table, might make sense:

CREATE TRIGGER [dbo].[after_user_insert] ON [dbo].[USER] FOR INSERT
AS 
BEGIN
    INSERT INTO [dbo].[USER_CUSTOMER] (...)
    SELECT user_id, ...
    FROM INSERTED
END

You would have to fill in the missing pieces of the above insert query with your actual logic. The key point is that the INSERTED variable is available inside the trigger, and should give you access to the user record which was just inserted. Presumably some of that information would be repeated in the new record in the USER_CUSTOMER table.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360