Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The solution to this problem depends on whether the "parent" table (i.e. the one with the IDENTITY column) has a natural key (i.e. one or more fields which, when combined, are guaranteed to be unique, <i>other than the surrogate primary key</i>).</p> <p>For example, in this case, is the combinaton of Name and Address aways going to be unique?</p> <p>If the answer is yes then you can simply insert into EmpBasic without bothering to output and store the generated IDs. You can then insert into EmpProject joining back on to EmpBasic using the natural key (e.g. name and address) to fnd the correct EmpID.</p> <pre><code>Insert into EmpBasic Select T1Name, T1Address from EmpFull_Temp Where ObjectID &lt; 106 Insert into EmpProject Select A.EmpID, B.T1EmpProject from EmpBasic as A, EmpFull_Temp as B Where A.Name = B.Name And A.Address = B.Address </code></pre> <p>If the answer is no then there is no easy solution I know of - in SQL Server 2005 (I've no idea if this is any different in 2008), it's not possible to OUTPUT values that are not inserted. I've got around this issue in the past by using one of the other fields (e.g. Name) to temporarily store the original ID (in this case, ObjectID), use that to join when inserting the child records as described above and then gone back to update the parent records o remove/replace the temporary values. It's not nice but I've not found a better way.</p> <pre><code>Insert into EmpBasic Select cast(ObjectID as varchar(50)) as name, T1Address from EmpFull_Temp Where ObjectID &lt; 106 Insert into EmpProject Select A.EmpID, B.T1EmpProject from EmpBasic as A, EmpFull_Temp as B Where A.Name = cast(B.ObjectID as varchar(50)) Update EmpBasic Set Name = B.T1Name from EmpBasic as A, EmpFull_Temp as B Where A.Name = cast(B.ObjectID as varchar(50)) </code></pre> <p>Please note: I've not tested the sample SQL given above but I hope it gives you an idea of how you might approach this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload