Here's an example of using the output
clause, like Oded suggested. First, some setup code:
if exists (select * from tempdb.sys.tables where name like '#tmp%')
drop table #tmp
create table #tmp (id int identity, FirstName varchar(50), LastName varchar(50))
if exists (select * from sys.procedures where name = 'TestProcedure')
drop procedure TestProcedure
if exists (select * from sys.types where name = 'TestTableType')
drop type TestTableType
create type TestTableType as table (FirstName varchar(50), LastName varchar(50))
go
Now we can create the stored procedure:
create procedure dbo.TestProcedure
@List TestTableType readonly
as
insert #tmp
(FirstName, LastName)
output inserted.*
select FirstName
, LastName
from @List l
go
Note the output
clause, which tells SQL Server to return the inserted rows back to the client, including the value of the identity column. Now that the code is all set up, we can test it:
declare @List TestTableType
insert @List values ('Rick','Cain'),
('Herman', 'Gingrich'),
('Newt', 'Paul'),
('Ron', 'Perry')
exec dbo.TestProcedure @List
select * from #tmp
You'll see that the values returned by the procedure match the values in #tmp
exactly.