I am experimenting with table valued parameters (TVP) and how to use them from C# code. One specific aspect of TVP is giving me trouble: When passing a list of data to the Stored Procedure and wanting to update that list after the data has come back from the database.
Here is a sample class that would be contained within a list:
public class Phone
{
int _phoneID;
string _phoneNumber;
Guid _correlationID;
}
When passing this list to a stored procedure that uses TVP, how would I be able to insert the _phoneID and the _phoneNumber into the database and, after the insert, update the list of Phones with the _phoneID based on the _correlationID?
Here is a sample table, type, and stored procedure:
Create Table PhoneTable
(
PhoneID int identity(1,1),
PhoneNumber varchar(20)
)
GO
Create Type PhoneType As Table
(
PhoneNumber varchar(20),
CorrelationID uniqueidentifier
)
GO
Create Procedure usp_Phone_Insert
@Input PhoneType ReadOnly
AS
Declare @Output Table (PhoneID int, PhoneNumber varchar(20))
Insert Phone (PhoneNumber)
Output
inserted.PhoneID,
inserted.PhoneNumber
Into @Output
Select
PhoneNumber
From
@Input
Select PhoneID, PhoneNumber From @Output
GO
The reason for a correlation id is for the ability to track the objects all the way from the application to the database and back.