{"id":168,"date":"2015-02-04T10:09:27","date_gmt":"2015-02-04T15:09:27","guid":{"rendered":"http:\/\/passionwind.com\/techref\/?p=168"},"modified":"2017-06-17T10:29:36","modified_gmt":"2017-06-17T14:29:36","slug":"sql-conditional-record-update","status":"publish","type":"post","link":"http:\/\/techref.camellarry.com\/?p=168","title":{"rendered":"SQL Conditional Record Update"},"content":{"rendered":"<p>A Visual Basic example on how to update a record if it exists or insert it if it doesn&#8217;t.<\/p>\n<pre><span style=\"font-family: 'courier new', courier; font-size: 10pt;\">Sub RecordStandardizedComputer(WS As String)<\/span>\r\n <span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0Dim txt1 As String, txt2 As String, txt3 As String<\/span>\r\n <span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0Dim xSQL As String<\/span>\r\n\r\n<span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0txt3 = \"SELECT ComputerID from LOG_ComputersStandardized\" _\r\n &amp; \" where ComputerName = \" &amp; WS<\/span>\r\n\r\n<span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0txt2 = \"INSERT INTO LOG_ComputersStandardized (ComputerName, RunTime)\" _\r\n &amp; \"VALUES (\" &amp; WS &amp; \"','\" &amp; Now.ToString &amp; \"')\"<\/span>\r\n\r\n<span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0txt1 = \"UPDATE LOG_ComputersStandardized SET \" _<\/span>\r\n <span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0 &amp; \" DateTime = '\" &amp; Now.ToString &amp; \"'\" _<\/span>\r\n <span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0 &amp; \" WHERE ComputerName = '\" &amp; WS &amp; \"'\"<\/span>\r\n\r\n<span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0xSQL = \"IF NOT EXISTS (\" &amp; txt3 &amp; \")\" &amp; txt2 &amp; \" ELSE \" &amp; txt1<\/span>\r\n <span style=\"font-family: 'courier new', courier; font-size: 10pt;\">\u00a0ExecuteSqlCommand(xSQL)<\/span>\r\n <span style=\"font-family: 'courier new', courier; font-size: 10pt;\"> End Sub<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A Visual Basic example on how to update a record if it exists or insert it if it doesn&#8217;t. Sub RecordStandardizedComputer(WS As String) \u00a0Dim txt1 As String, txt2 As String, txt3 As String \u00a0Dim xSQL As String \u00a0txt3 = &#8220;SELECT ComputerID from LOG_ComputersStandardized&#8221; _ &amp; &#8221; where ComputerName = &#8221; &amp; WS \u00a0txt2 = &#8220;INSERT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[8,9],"tags":[29,58,70,77],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","category-sql","category-vb-net","tag-example","tag-programming","tag-sql","tag-vb-net"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9t3oE-2I","_links":{"self":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=168"}],"version-history":[{"count":7,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions"}],"predecessor-version":[{"id":319,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=\/wp\/v2\/posts\/168\/revisions\/319"}],"wp:attachment":[{"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/techref.camellarry.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}