I found that instead of re-creating the temp table, I can delete from it after the last inserted value is used.
I think (unless I am corrected), it is more efficient to delete temp table than recreating it after each insert.
>So, Hugo correctly caught my mistake.
>
>One more question, please.
>
>If I want the temp table @InsertOutput to be "reused" on every insert (in my Stored Procedure, the Insert happens more than one time), and I want the @InsertOutput to have just one record, is the only option to drop this table before creating it? And therefore, creating it multiple times?
>
>
>>You are right. Thank you for catching my mistake.
>>
>>>You are missing the into clause in your second insert
>>>
>>>insert into #myTest (col1) output inserted.colident into @InsertOutput values ('1st')
>>>select * from @InsertOutput
>>>insert into #myTest (col1) output inserted.colident into @InsertOutput values ('2nd')
>>>
>>>
>>>>>Hi,
>>>>>
>>>>>The case: a record is inserted into table A. And then a record is inserted in table B. I would like to "capture" the value of the identity column in Table 1 to a column in table B.
>>>>>
>>>>>Question 1. Does Identity get a value before the table is written?
>>>>>
>>>>>Question 2. If the answer to question 1 is Yes, where do I put the OUTPUT in the following Insert:
>>>>>
>>>>>insert into TableA (column1, column2, column3) values (Col1Value, Col2Value, Col3Value)
>>>>>
>>>>>
>>>>>TIA
>>>>
>>>>I think the approach suggested by Naomi will indeed take me 10 years to learn. So, I am back to attempting the OUTPUT.
>>>>
>>>>Here is my example (below are the results that I don't understand)
>>>>
>>>>if object_id('tempdb..#myTest', N'U') is not null drop table #myTest;
>>>>
>>>>declare @InsertOutput table
>>>>(
>>>> new_ident int
>>>>)
>>>>
>>>>create table #myTest
>>>>(
>>>> col1 varchar(50),
>>>> colident int identity(1,1) not null
>>>>)
>>>>insert into #myTest (col1) output inserted.colident into @InsertOutput values ('1st')
>>>>select * from @InsertOutput
>>>>insert into #myTest (col1) output inserted.colident values ('2nd')
>>>>select * from @InsertOutput
>>>>select * from #myTest
>>>>
>>>>
>>>>The select * from #myTest show correct results: two records and the colindent is correctly showing 2.
>>>>
>>>>But the select * from @InsertOutput shows just one record and one value, new_ident equal to 1
>>>>
>>>>Why? Could you tell me what I am doing wrong?
>>>>
>>>>Thanks
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham