I have the following tables for example:
- Table 1: id, name
- Table 2: id, school, t1Id
The requirement is:
- firstly
select t1id from Table1 where name = @name
- If the record exists,
insert Table2 value (@school,t1id)
- If the record doesn't exists,
insert Table1 value(@name)
and then return the id of this new-inserted record to executeinsert Table2 value (@school,t1id)
My question is:
How to achieve this with one SQL statement or transaction without using stored procedure?
I'm using Spring but right now I separate the executions of these SQL statements and interact with database too many times.
So I want to do that in one execution.