{"id":1035,"date":"2010-01-26T14:32:34","date_gmt":"2010-01-26T14:32:34","guid":{"rendered":"http:\/\/www.mjtnet.com\/blog\/?p=1035"},"modified":"2010-01-26T17:04:38","modified_gmt":"2010-01-26T17:04:38","slug":"get-auto-generated-id-created-by-insert-sql","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2010\/01\/26\/get-auto-generated-id-created-by-insert-sql\/","title":{"rendered":"Get Auto-Generated ID Created By INSERT SQL"},"content":{"rendered":"<p>If you have a database table with an auto-generated numerical index field, you may need to retrieve the value of this field after an INSERT statement.<\/p>\n<p>In Microsoft Access such a field uses an &#8220;AutoNumber&#8221; data type.  In MySQL it&#8217;s known as an &#8220;auto_increment&#8221; field.<\/p>\n<p>We often see these types of fields where a relationship exists between two tables, for example between a &#8220;customers&#8221; table and &#8220;customer_orders&#8221; table.  The &#8220;customers&#8221; table may have an AutoNumber field which the system automatically increments when a record is inserted, and in the &#8220;customer_orders&#8221; table you&#8217;ll see a &#8220;CustomerID&#8221; field which uses this same value to map one or more orders to one customer.<\/p>\n<p>It&#8217;s a convenient way to have the database itself automatically create a unique identifier.<\/p>\n<p>If you ever need to use Macro Scheduler to transfer data into such a database you can use the built-in database functions to handle the INSERTS.  But when it comes to inserting the data into the second table you&#8217;re going to need a way to get the ID that was automatically generated for the first.<\/p>\n<p>Some database technologies give you a function to do just that.  MySQL has a function called LAST_INSERT_ID() which you can call with a SELECT statement:<\/p>\n<pre name=\"code\" class=\"macroscript\">DBExec>dbID,INSERT into customers (name,address1,address2) VALUES (\"Mr Customer\",\"..\",\"..\"),res \r\nDBQuery>dbID,SELECT LAST_INSERT_ID(),last_id,nr,nf,0<\/pre>\n<p>This will return the ID in LAST_ID_1_1 and you can then use it in your next INSERT for the detail records.<\/p>\n<p>See: <a href=\"http:\/\/dev.mysql.com\/doc\/refman\/5.0\/en\/information-functions.html#function_last-insert-id\">MySQL: last_insert_id<\/a><\/p>\n<p>Microsoft Access does the same with a function called @@identity:<\/p>\n<pre name=\"code\" class=\"macroscript\">DBExec>dbID,INSERT into customers (name,address1,address2) VALUES (\"Mr Customer\",\"..\",\"..\"),res \r\nDBQuery>dbID,SELECT @@identity,last_id,nr,nf,0<\/pre>\n<p>See: <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ks9f57t0(VS.71).aspx\">Retrieving Identity or Autonumber Values<\/a><\/p>\n<p>Your particular flavour of database might have something similar.  If not you could take a risk and do this:<\/p>\n<pre name=\"code\" class=\"macroscript\">DBQuery>mdbH,SELECT TOP 1 ID FROM customers ORDER BY ID DESC,last_id,nr,nf,0<\/pre>\n<p>This will return the largest ID &#8211; the latest one.  But if you&#8217;re working on a database that has many users updating it simultaneously there&#8217;s a danger you could retrieve the ID generated by someone else&#8217;s INSERT.  If it&#8217;s just you, then it&#8217;s probably OK, but you&#8217;ve been warned.<\/p>\n<p>For more information on Macro Scheduler&#8217;s database functions see:<br \/>\n<a href=\"http:\/\/www.mjtnet.com\/blog\/2008\/04\/15\/using-macro-schedulers-database-functions\/\">Using Macro Scheduler&#8217;s Database Functions<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you have a database table with an auto-generated numerical index field, you may need to retrieve the value of this field after an INSERT statement. In Microsoft Access such a field uses an &#8220;AutoNumber&#8221; data type. In MySQL it&#8217;s known as an &#8220;auto_increment&#8221; field. We often see these types of fields where a relationship [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1035"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=1035"}],"version-history":[{"count":8,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1035\/revisions"}],"predecessor-version":[{"id":1045,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/1035\/revisions\/1045"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=1035"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=1035"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=1035"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}