Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first way I thought of doing this is to use a <code>AFTER INSERT TRIGGER</code> on the table, capture the entire rowset inserted, and get the identities from there.</p> <p><strong>EDIT</strong> Of course, you can do this by capturing OUTPUT (doh)</p> <p><a href="http://msdn.microsoft.com/en-us/library/ms177564.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/ms177564.aspx</a></p> <p>Here is a working sample, Since you hadn't provided sample data, I created my own Allery and ETVault tables, so use the example in a brand new database.</p> <pre><code>DROP TABLE Allergy GO CREATE TABLE Allergy ( ID INTEGER IDENTITY (1, 1), NAME VarChar (20), Category VarChar (20), Composition VarChar (20) ) GO DROP TABLE ETVault Go CREATE TABLE ETVault ( ID INTEGER IDENTITY (1, 1), Strain INT, NAME VarChar (20), Category VarChar (20), Composition VarChar (20) ) GO INSERT INTO dbo.ETVault ( Strain, NAME, Category, Composition ) VALUES ( 1, '1', '1', '1'), ( 2, '1', '1', '1'), ( 3, '1', '1', '1'), ( 4, '1', '1', '1'), ( 5, '1', '1', '1'), ( 6, '1', '1', '1'), ( 7, '1', '1', '1'), ( 8, '1', '1', '1') DECLARE @CaptureTable TABLE (ID INT) INSERT INTO dbo.Allergy ( NAME, Category, Composition ) OUTPUT INSERTED.Id INTO @CaptureTable SELECT dbo.ETVault.NAME, Category, Composition FROM dbo.ETVault Where Strain In (6, 2, 4) SELECT * FROM @CaptureTable </code></pre> <p>It gives you the output</p> <pre><code>(8 row(s) affected) (3 row(s) affected) ID ----------- 1 2 3 (3 row(s) affected) </code></pre>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    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