Note that there are some explanatory texts on larger screens.

plurals
  1. POGet SCOPE_IDENTITY value when inserting bulk records for SQL TableType
    primarykey
    data
    text
    <p>I have following table structure, for convenience purpose I am only marking individual columns</p> <ul> <li><code>Table_A</code> (<code>Id, Name, Desc</code>)</li> <li><code>Table_1</code> (<code>Id</code> <em>this is identity column</em>, <code>Name</code>....)</li> <li><code>Table_2</code> (<code>Id</code> <em>this is identity column</em>, <code>Table_A_Id</code>, <code>Table_1_Id</code>)</li> </ul> <p>The relationship between Table_1 and Table_2 is <code>1...*</code></p> <p>Now I have created a table type for <code>Table_A</code> called <code>TType_Table_A</code> (which only contains <code>Id</code> as column and from my C# app I send multiple records). I have achieved this bulk insert functionality as desired.</p> <p>What I need is when I insert records into <code>Table_2</code> from <code>TType_Table_A</code> say with below statements, I would like to capture the <code>Id</code> of <code>Table_2</code> for each record inserted</p> <pre><code>declare @count int = (select count(*) from @TType_Table_A); --a variable declared for TType_Table_A if(@count &gt; 0) begin insert into Table_2(Table_A_Id,Table_1_Id) SELECT @SomeValue, @SomeValueAsParameter FROM @TType_Table_A; end; </code></pre> <p>Now say if 2 records are inserted, I would like to capture the <code>Id</code> for each of these 2 records.</p> <p>Any input/help is appreciated</p> <p>This is what I know how it can be achieved, but I want to reduce DB calls from my app or user cursor in stored procedure</p> <p>Insert record in Table_1 and return back the Id Loop.....through records and insert record in Table_2 and return back the Id</p> <p>OR</p> <p>Use cursor in stored procedure when inserting/selecting from TableType</p>
    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. 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