This question is really two parts. The first part is just inserting into a real table with a temp table and having the output inserted into another temp table is not working. Here is the example.
Insert Into student
Output INSERTED.*
Into #StudentsOutput
Select *
From #StudentsResult
The error from above when I hover over it.
Invalid Object name '#StudentsOutput'
All msdn examples I saw could do this, unless because it involves two temp tables you cannot do this.
Also note that I tried stating the columns specifically for the student
table, and from the #StudentsResult
table. This yielded the same results from above. So for brevity I am only showing the above tsql
The next issue is I would like the new temp table #StudentsOutput
to contain the old student id. Since I am going to modify other tables that need the reference to the old student id. Example below.
Insert Into student
Output INSERTED.student_id, s.student_id as [old_student_id]
Into #StudentsOutput
Select *
From #StudentsResult as s
This would be perfect for what I need a lookup of the old value to the new value! Error below.
The multi-part identifier 's.student_id' could not be bound.