{"id":780,"date":"2007-08-02T21:49:00","date_gmt":"2007-08-02T21:49:00","guid":{"rendered":"\/blogs\/bobb\/post\/Katmai-Using-Table-Valued-Parameters-with-ADONET.aspx"},"modified":"2007-08-02T21:49:00","modified_gmt":"2007-08-02T21:49:00","slug":"katmai-using-table-valued-parameters-with-ado-net","status":"publish","type":"post","link":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/","title":{"rendered":"Katmai: Using Table-Valued Parameters with ADO.NET"},"content":{"rendered":"<p>\nI&#39;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, DbDataReader or IList&lt;SqlDataRecord&gt; to represent a TVP parameter. Let&#39;s say that I want a TVP and a procedure&nbsp;for insert that looks like this:\n<\/p>\n<p>\nCREATE TYPE dbo.JobsTableWithIdentity AS TABLE (<br \/>\n&nbsp; job_id smallint identity primary key,<br \/>\n&nbsp; job_desc varchar(50),<br \/>\n&nbsp; min_lvl tinyint,<br \/>\n&nbsp; max_lvl tinyint<br \/>\n);\n<\/p>\n<p>\nThe &quot;insert proc&quot; would look like this:\n<\/p>\n<p>\nCREATE PROCEDURE dbo.InsertJobsID (@tvp1 dbo.JobsTableWithIdentity readonly)<br \/>\nas<br \/>\nINSERT INTO dbo.Jobs (job_desc, min_lvl, max_lvl)<br \/>\n&nbsp; SELECT job_desc, min_lvl, max_lvl from @tvp1;\n<\/p>\n<p>\nUsing this in ADO.NET (with either DataTable or DbDataReader as a parameter) produces the error: &quot;INSERT into an identity column not allowed on table variables. The data for table-valued parameter &quot;@tvp1&quot; doesn&#39;t conform to the table type of the parameter.&quot; But I didn&#39;t&nbsp;DO an insert into an identity column in the proc. And this works just fine in T-SQL:\n<\/p>\n<p>\nDECLARE @t dbo.JobsTableWithIdentity;<br \/>\nINSERT @t VALUES(&#39;hi&#39;, 10, 10);<br \/>\nINSERT @t VALUES(&#39;hi2&#39;, 10, 10);<br \/>\nEXEC InsertJobsID @t;\n<\/p>\n<p>\nSo its ADO.NET &quot;deciding&quot; 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:\n<\/p>\n<p>\nCREATE TYPE dbo.JobsTableWithoutIdentity AS TABLE (<br \/>\n&nbsp; &#8212; job_id smallint identity primary key,<br \/>\n&nbsp; job_desc varchar(50),<br \/>\n&nbsp; min_lvl tinyint,<br \/>\n&nbsp; max_lvl tinyint<br \/>\n);\n<\/p>\n<p>\n&#8212; sproc dbo.InsertJobsNoID changed accordingly\n<\/p>\n<p>\n\/\/ and then, in ADO.NET code<br \/>\n\/\/ DataTable &quot;t&quot; contains a real jobs table, to which I&#39;ve added rows\n<\/p>\n<p>\nDataTable added = t.GetChanges(DataRowState.Added);<br \/>\nadded.Columns.Remove(&quot;job_id&quot;);<br \/>\nda.InsertCommand.CommandText = &quot;dbo.InsertJobsNoID&quot;;<br \/>\nda.InsertCommand.CommandType = CommandType.StoredProcedure;<br \/>\nda.InsertCommand.Parameters.AddWithValue(&quot;@tvp1&quot;, added);\n<\/p>\n<p>\nBut, should I have to do this? Or modify the T-SQL code, given I&#39;ve not used the IDENTITY column? But, I will need this column to UPDATE (or MERGE) using the TVP. A dilemma&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;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, DbDataReader or IList&lt;SqlDataRecord&gt; to represent a TVP parameter. Let&#39;s say that I want a TVP and a procedure&nbsp;for insert that [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12,29,40],"tags":[],"class_list":["post-780","post","type-post","status-publish","format-standard","hentry","category-data-access","category-sql-server-2008","category-transact-sql"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.9.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Katmai: Using Table-Valued Parameters with ADO.NET - Bob Beauchemin<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Katmai: Using Table-Valued Parameters with ADO.NET - Bob Beauchemin\" \/>\n<meta property=\"og:description\" content=\"I&#039;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, DbDataReader or IList&lt;SqlDataRecord&gt; to represent a TVP parameter. Let&#039;s say that I want a TVP and a procedure&nbsp;for insert that [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/\" \/>\n<meta property=\"og:site_name\" content=\"Bob Beauchemin\" \/>\n<meta property=\"article:published_time\" content=\"2007-08-02T21:49:00+00:00\" \/>\n<meta name=\"author\" content=\"Bob Beauchemin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Bob Beauchemin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/\",\"name\":\"Katmai: Using Table-Valued Parameters with ADO.NET - Bob Beauchemin\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\"},\"datePublished\":\"2007-08-02T21:49:00+00:00\",\"dateModified\":\"2007-08-02T21:49:00+00:00\",\"author\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Data Access\",\"item\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Katmai: Using Table-Valued Parameters with ADO.NET\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website\",\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/\",\"name\":\"Bob Beauchemin\",\"description\":\"SQL Server Blog\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e\",\"name\":\"Bob Beauchemin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g\",\"caption\":\"Bob Beauchemin\"},\"sameAs\":[\"http:\/www.sqlskills.com\/blogs\/bobb\/\"],\"url\":\"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Katmai: Using Table-Valued Parameters with ADO.NET - Bob Beauchemin","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/","og_locale":"en_US","og_type":"article","og_title":"Katmai: Using Table-Valued Parameters with ADO.NET - Bob Beauchemin","og_description":"I&#39;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, DbDataReader or IList&lt;SqlDataRecord&gt; to represent a TVP parameter. Let&#39;s say that I want a TVP and a procedure&nbsp;for insert that [&hellip;]","og_url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/","og_site_name":"Bob Beauchemin","article_published_time":"2007-08-02T21:49:00+00:00","author":"Bob Beauchemin","twitter_misc":{"Written by":"Bob Beauchemin","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/","name":"Katmai: Using Table-Valued Parameters with ADO.NET - Bob Beauchemin","isPartOf":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website"},"datePublished":"2007-08-02T21:49:00+00:00","dateModified":"2007-08-02T21:49:00+00:00","author":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e"},"breadcrumb":{"@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/katmai-using-table-valued-parameters-with-ado-net\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/"},{"@type":"ListItem","position":2,"name":"Data Access","item":"https:\/\/www.sqlskills.com\/blogs\/bobb\/category\/data-access\/"},{"@type":"ListItem","position":3,"name":"Katmai: Using Table-Valued Parameters with ADO.NET"}]},{"@type":"WebSite","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#website","url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/","name":"Bob Beauchemin","description":"SQL Server Blog","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqlskills.com\/blogs\/bobb\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/62bfa986c5b5d28fcffd8b4fc409c73e","name":"Bob Beauchemin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.sqlskills.com\/blogs\/bobb\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f80e6cc667410857fa6a21931dc528b8092f4d112bf7a8ff7c267674d44ee37?s=96&d=mm&r=g","caption":"Bob Beauchemin"},"sameAs":["http:\/www.sqlskills.com\/blogs\/bobb\/"],"url":"https:\/\/www.sqlskills.com\/blogs\/bobb\/author\/bobb\/"}]}},"_links":{"self":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/780","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/comments?post=780"}],"version-history":[{"count":0,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/posts\/780\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/media?parent=780"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/categories?post=780"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sqlskills.com\/blogs\/bobb\/wp-json\/wp\/v2\/tags?post=780"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}