Insert on 2 Related tables question
I have got a question regarding an insert accross 2 related tables. What I am wondering is the following and here is the senario
2 SQL server Tables. Tbl1 Has got (PK)ID,Field1,Field2 and ID increments by 1. tbl2 got FK ID & Field2. (1toM on ID) Now my asp form contains fields being filled in on both tbls. Now if I do the insert, I Insert Into Tbl1, Set my rs=nothing THEN I select the ID of the inserted record using a where clause containing all the fields with values inserted in the first insert(risk1). Now I got the ID value and then I do an insert into the second table.
My worry is that that the first select to get the ID of the inserted record might not return the correct ID as it is possible but unlikely that any other record will match the where clause. The second worry is that between the first and second insert somethin might happen and the second record does not get insertted which leaves me with inconsistent data.
Now the only thing I can think off is to make a sp that does all of this at once and then commit this as 1 transaction. Then if something fails I can roll all of it back and raise an error.
Is there any other way I could do this as 1 transaction? Matbe a nested insert and select or something? Or am I just paraniod? :)
Any sugestions welcome.