{"id":1615,"date":"2015-09-23T12:10:10","date_gmt":"2015-09-23T12:10:10","guid":{"rendered":"https:\/\/ibex.tech\/cloud\/?p=1615"},"modified":"2022-02-17T07:14:02","modified_gmt":"2022-02-17T07:14:02","slug":"get-autoincrement-value-for-a-new-record","status":"publish","type":"post","link":"https:\/\/ibex.tech\/cloud\/sql-server\/queries-microsoft-sql-server\/insert\/get-autoincrement-value-for-a-new-record","title":{"rendered":"Get AutoIncrement Value For A New Record"},"content":{"rendered":"<p>\nIf your table&nbsp;has a column &nbsp;that is auto assigned a value when a record is created, e.g. an autoincrement&nbsp;ID value, you can get it using scope_iendity() as follows:\n<\/p>\n<pre>\r\n<code>\r\n\tint RecordId;\r\n\tSqlCommand1-&gt;CommandType = CommandType::Text;\r\n\tSqlCommand1-&gt;CommandText = &quot;INSERT INTO MyTable (SomeRow1, SomeRow2) VALUES (@MyValue1, @MyValue2); SELECT CAST(scope_identity() AS int)&quot;;\r\n\tSqlCommand1-&gt;Parameters-&gt;AddWithValue(&quot;@MyValue1&quot;, MyValue1);\r\n\tSqlCommand1-&gt;Parameters-&gt;AddWithValue(&quot;@MyValue2&quot;, Convert::ToString(MyValue2));\r\n\tRecordId = (Int32)SqlCommand1-&gt;ExecuteScalar();\t\t\/\/ExecuteScalar returns the auto assigned value for an auto incrment column from the scope_identity() select query\r\n<\/code><\/pre>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If your table&nbsp;has a column &nbsp;that is auto assigned a value when a record is created, e.g. an autoincrement&nbsp;ID value, you can get it using scope_iendity() as follows: int RecordId; SqlCommand1-&gt;CommandType = CommandType::Text; SqlCommand1-&gt;CommandText = &quot;INSERT INTO MyTable (SomeRow1, SomeRow2) VALUES (@MyValue1, @MyValue2); SELECT CAST(scope_identity() AS int)&quot;; SqlCommand1-&gt;Parameters-&gt;AddWithValue(&quot;@MyValue1&quot;, MyValue1); SqlCommand1-&gt;Parameters-&gt;AddWithValue(&quot;@MyValue2&quot;, Convert::ToString(MyValue2)); RecordId = (Int32)SqlCommand1-&gt;ExecuteScalar(); \/\/ExecuteScalar [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-1615","post","type-post","status-publish","format-standard","hentry","category-insert"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1615","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/comments?post=1615"}],"version-history":[{"count":1,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1615\/revisions"}],"predecessor-version":[{"id":1616,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/posts\/1615\/revisions\/1616"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/media?parent=1615"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/categories?post=1615"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/cloud\/wp-json\/wp\/v2\/tags?post=1615"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}