I would like to know more about inserting into one table a value right after creating the first one,
Scenario:
I have two tables Patient with PID as the PK, and the PatientRecord with RID as the PK and PID as a FK to Patient.
What I want to do is to insert the latest PID into the PatientRecord right after successfully creating it.
Initial SQL:
INSERT INTO record (@PID,@NID,@DID)
values((SELECT TOP (1) PERCENT 'PID' FROM patient),2,1)
The values 2, and 1 do already exist inside the Doctor and Nurse tables.
I realize that I am making a huge mistake with that query, but with a little help of explanation about where did i go wrong and/or a direct guidance will help a lot.
Code snippets will be much appreciated.
I suggest that you use an identity field for the primary key in the patient table. Then you can use something like..
insert into Patient values ('some patient values')insert into PatientRecord values (ident_current('Patient'), 'some patient record values')