title: How to get primery key value to foreign key
body: <p>I have 3 tables one is <code>User</code> and <code>doctor</code>, <code>patient</code>. <code>User</code> table has common attributes such as <code>ID</code>, <code>Name</code>, <code>Email</code>. I have connected both tables using primary key of the user table and foreign key of the doctor table. I use 2 SQL server queries to insert data. </p>
  
  <p>Foreign key is not automatically get the value from user table <code>ID</code> column. Instead of the <code>doctor</code> table foreign key column update as 'null' with other inserted data.</p>
  
  <p>I wanna get the user table <code>PK</code> value to doctor table <code>FK</code>.</p>
  
  <p>This is how i insert data into database in asp.net</p>
  
  <pre><code>sda.InsertCommand = new SqlCommand("Insert into sysUser(userPassword, userType,firstName,lastName,age,dateOfBirth,gender,telephoneNumber,email,userAddress,country) VALUES (@signUpPw, @signUserType,@signUpFname,@signUpLname, @signupAge, @signupDateOfBirth,@signupGender,@signUpTp, @signUpEmail, @signUpAddr, @signUpCountry);", con);
  
              sda.InsertCommand.Parameters.Add("@signUpPw", SqlDbType.VarChar).Value = newDoctor.getUserPassword();
              sda.InsertCommand.Parameters.Add("@signUserType", SqlDbType.VarChar).Value = "Doctor";/*userType*/
              sda.InsertCommand.Parameters.Add("@signUpFname", SqlDbType.VarChar).Value = newDoctor.firstName;
              sda.InsertCommand.Parameters.Add("@signUpLname", SqlDbType.VarChar).Value = newDoctor.lastName;
              sda.InsertCommand.Parameters.Add("@signupAge", SqlDbType.VarChar).Value = newDoctor.age;
  
              sda.InsertCommand.Parameters.Add("@signupDateOfBirth", SqlDbType.VarChar).Value = newDoctor.dateOfBirth;
              sda.InsertCommand.Parameters.Add("@signupGender", SqlDbType.VarChar).Value = newDoctor.gender;
              sda.InsertCommand.Parameters.Add("@signUpTp", SqlDbType.VarChar).Value = newDoctor.telephoneNumber;
              sda.InsertCommand.Parameters.Add("@signUpEmail", SqlDbType.VarChar).Value = newDoctor.email;
              sda.InsertCommand.Parameters.Add("@signUpAddr", SqlDbType.VarChar).Value = newDoctor.userAddress;
              sda.InsertCommand.Parameters.Add("@signUpCountry", SqlDbType.VarChar).Value = newDoctor.country;
  
                con.Open();
                int id = sda.InsertCommand.ExecuteNonQuery();
  
  
              sda.InsertCommand = new SqlCommand("Insert into doctor(userID,docTitle, specialityArea, qualifications) VALUES (@signupTitle, @SignupSpecialityArea, @signupQualifications)", con);
  
              sda.InsertCommand.Parameters.Add("@signupTitle", SqlDbType.VarChar).Value = newDoctor.title;
              sda.InsertCommand.Parameters.Add("@SignupSpecialityArea", SqlDbType.VarChar).Value = newDoctor.specialArea;
              sda.InsertCommand.Parameters.Add("@signupQualifications", SqlDbType.VarChar).Value = newDoctor.qualifications;
  
  
              sda.InsertCommand.ExecuteNonQuery();
              con.Close();
  </code></pre>
  
  <p>and here is the SQL server database</p>
  
  <pre><code>create table sysUser
  (userID INT IDENTITY(1,1) NOT NULL PRIMARY KEY, 
  userPassword varchar(20) NOT NULL, 
  userType varchar(20) NOT NULL, 
  firstName varchar(30) NOT NULL, 
  lastName varchar(30) NOT NULL, 
  age varchar(20) NOT NULL, 
  dateOfBirth varchar(20) NOT NULL, 
  gender varchar(20) NOT NULL, 
  telephoneNumber varchar(20) NOT NULL, 
  email varchar(50) NOT NULL, 
  userAddress varchar(100) NOT NULL, 
  country varchar(50) NOT NULL); 
  
  create table doctor(
  docID INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
  userID int NOT NULL,
  docTitle varchar(20),
  specialityArea varchar(200),
  qualifications varchar(100)
  FOREIGN KEY (userID) REFERENCES sysUser (userID) ON UPDATE NO ACTION ON DELETE CASCADE
  );
  </code></pre>
  
  <p>here is the trigger</p>
  
  <pre><code>CREATE TRIGGER userID_copyTO_doc_FK ON sysUser
  FOR INSERT
  AS
  BEGIN
  declare @id int
  select @id = userID from inserted  
  insert into doctor(userID)
  select (@id)
  END
  </code></pre>
  
  <p>when trigger fired it insert a row with userID to the doctor table (other columns are null )
  then doctor table insert query runs, it inserts all the details except userID. </p>
  
  <p>Im getting 2 rows, i want an one row. :( </p>