I've been trying out table-valued parameters along with ADO.NET support in Orcas and came across an interesting dilemma. It centers around INSERTs using TVP against a table with an identity column. ADO.NET can use DataTable, IDataReader or IList<SqlDataRecord> to represent a TVP parameter. Let's say that I want a TVP and a procedure for insert that looks like this:
CREATE TYPE dbo.JobsTableWithIdentity AS TABLE ( job_id smallint identity primary key, job_desc varchar(50), min_lvl tinyint, max_lvl tinyint);
The "insert proc" would look like this:
CREATE PROCEDURE dbo.InsertJobsID (@tvp1 dbo.JobsTableWithIdentity readonly)asINSERT INTO dbo.Jobs (job_desc, min_lvl, max_lvl) SELECT job_desc, min_lvl, max_lvl from @tvp1;
Using this in ADO.NET (with either DataTable or IDataReader as a parameter) produces the error: "INSERT into an identity column not allowed on table variables. The data for table-valued parameter "@tvp1" doesn't conform to the table type of the parameter." But I didn't DO an insert into an identity column in the proc. And this works just fine in T-SQL:
DECLARE @t dbo.JobsTableWithIdentity;INSERT @t VALUES('hi', 10, 10);INSERT @t VALUES('hi2', 10, 10);EXEC InsertJobsID @t;
So its ADO.NET "deciding" this is an error. The ADO.NET workaround (if I do want to start with a DataTable that contains the identity column, add rows to it, and call Update) is this:
CREATE TYPE dbo.JobsTableWithoutIdentity AS TABLE ( -- job_id smallint identity primary key, job_desc varchar(50), min_lvl tinyint, max_lvl tinyint);
-- sproc dbo.InsertJobsNoID changed accordingly
// and then, in ADO.NET code// DataTable "t" contains a real jobs table, to which I've added rows
DataTable added = t.GetChanges(DataRowState.Added);added.Columns.Remove("job_id");da.InsertCommand.CommandText = "dbo.InsertJobsNoID";da.InsertCommand.CommandType = CommandType.StoredProcedure;da.InsertCommand.Parameters.AddWithValue("@tvp1", added);
But, should I have to do this? Or modify the T-SQL code, given I've not used the IDENTITY column? But, I will need this column to UPDATE (or MERGE) using the TVP. A dilemma...
Theme design by Jelle Druyts
Pick a theme: BlogXP sqlx BlogXP sqlx
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Bob Beauchemin
E-mail