{"id":329,"date":"2009-09-27T17:38:26","date_gmt":"2009-09-27T15:38:26","guid":{"rendered":"http:\/\/bar-solutions.com\/weblog\/?p=329"},"modified":"2009-09-27T17:38:26","modified_gmt":"2009-09-27T15:38:26","slug":"drop-if-exists","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=329","title":{"rendered":"Drop if exists&hellip;"},"content":{"rendered":"<p>When creating an update script for the database the DBA always nag me about objects to be dropped that don\u2019t exist. My view is that the end result is the same, you are dropping an objects to remove it, so in the end it\u2019s gone. If it\u2019s not there in the first place, then it\u2019s not there after the statement ran (gone as well).<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:711aa8fb-54f9-4d15-9a4e-d824a258c16f\" class=\"wlWriterEditableSmartContent\">\n<pre style=\"background-color:#FFFFFF;overflow: auto;;font-family:Courier New;font-size:11.25\"><span style=\"color: #0000FF;\">drop<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">table<\/span><span style=\"color: #000000;\"> patrick purge\n \nORA<\/span><span style=\"color: #808080;\">-<\/span><span style=\"color: #800000; font-weight: bold;\">00942<\/span><span style=\"color: #000000;\">: <\/span><span style=\"color: #0000FF;\">table<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #808080;\">or<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">view<\/span><span style=\"color: #000000;\"> does <\/span><span style=\"color: #808080;\">not<\/span><span style=\"color: #000000;\"> exist<\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>The DBA doesn\u2019t want to see any ORA messages, so if you want your script to drop the object you first have to check for the existence of the object and if it does, then drop it. If it doesn\u2019t then there is nothing to do. (Oracle) MySQL has the possibility to drop if exists. That\u2019s why I created a utility that can be used to suppress any error message. Not, if there are dependencies to an object to be dropped, you will still get errors, but these are errors you want to see and which means \u2018back to the drawing board\u2019 for your script.<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:0da8ca99-4f0d-44d1-84d9-81a8ceb6fdc2\" class=\"wlWriterEditableSmartContent\">\n<pre style=\"background-color:#FFFFFF;overflow: auto;;font-family:Courier New;font-size:11.25\"><span style=\"color: #0000FF;\">create<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #808080;\">or<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF00FF;\">replace<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">procedure<\/span><span style=\"color: #000000;\"> drop_if_exists(object_name_in <\/span><span style=\"color: #808080;\">in<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">varchar2<\/span><span style=\"color: #000000;\">,\n                                           purge_in       <\/span><span style=\"color: #808080;\">in<\/span><span style=\"color: #000000;\"> boolean <\/span><span style=\"color: #0000FF;\">default<\/span><span style=\"color: #000000;\"> true)\n  authid <\/span><span style=\"color: #FF00FF;\">current_user<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">as<\/span><span style=\"color: #000000;\">\n  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> Procedure checks for the existance of an object and then drops it.<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> %param object_name_in The name of the object to be dropped<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> %param purge_in If the object to be dropped is a table, it can be purged from the Recycle bin<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\">                {*} True (Default)<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\">                {*} False<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> %author Patrick Barel<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">  <\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> %version 1.0<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #000000;\">\n  l_type user_objects.object_type<\/span><span style=\"color: #808080;\">%<\/span><span style=\"color: #000000;\">type;\n  l_sql  <\/span><span style=\"color: #0000FF;\">varchar2<\/span><span style=\"color: #000000;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">32767<\/span><span style=\"color: #000000;\">);\n<\/span><span style=\"color: #0000FF;\">begin<\/span><span style=\"color: #000000;\">\n  <\/span><span style=\"color: #0000FF;\">begin<\/span><span style=\"color: #000000;\">\n    <\/span><span style=\"color: #0000FF;\">select<\/span><span style=\"color: #000000;\"> uo.OBJECT_TYPE\n      <\/span><span style=\"color: #0000FF;\">into<\/span><span style=\"color: #000000;\"> l_type\n      <\/span><span style=\"color: #0000FF;\">from<\/span><span style=\"color: #000000;\"> user_objects uo\n     <\/span><span style=\"color: #0000FF;\">where<\/span><span style=\"color: #000000;\"> uo.<\/span><span style=\"color: #FF00FF;\">OBJECT_NAME<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF00FF;\">upper<\/span><span style=\"color: #000000;\">(object_name_in);\n  exception\n    <\/span><span style=\"color: #0000FF;\">when<\/span><span style=\"color: #000000;\"> others <\/span><span style=\"color: #0000FF;\">then<\/span><span style=\"color: #000000;\">\n      dbms_output.put_line(<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">Nothing to do<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n  <\/span><span style=\"color: #0000FF;\">end<\/span><span style=\"color: #000000;\">;\n  <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> l_type <\/span><span style=\"color: #0000FF;\">is<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #808080;\">not<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">null<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">then<\/span><span style=\"color: #000000;\">\n    l_sql :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">DROP <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #000000;\"> l_type <\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #000000;\"> object_name_in;\n    <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\"> (l_type <\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">TABLE<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">) <\/span><span style=\"color: #808080;\">and<\/span><span style=\"color: #000000;\"> purge_in <\/span><span style=\"color: #0000FF;\">then<\/span><span style=\"color: #000000;\">\n      l_sql :<\/span><span style=\"color: #808080;\">=<\/span><span style=\"color: #000000;\"> l_sql <\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\"> PURGE<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000FF;\">end<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\">;\n    <\/span><span style=\"color: #0000FF;\">execute<\/span><span style=\"color: #000000;\"> immediate l_sql;\n    dbms_output.put_line(object_name_in <\/span><span style=\"color: #808080;\">||<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\"> dropped<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">);\n  <\/span><span style=\"color: #0000FF;\">end<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">if<\/span><span style=\"color: #000000;\">;\n<\/span><span style=\"color: #0000FF;\">end<\/span><span style=\"color: #000000;\">;\n<\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>A way to call this procedure is like this:<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:cdbd773a-0821-447c-96dc-3bcc44701cb5\" class=\"wlWriterEditableSmartContent\">\n<pre style=\"background-color:#FFFFFF;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;overflow: auto;\"><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> Drop table<\/span><span style=\"color: #008080;\">\n--<\/span><span style=\"color: #008080;\">drop table AMIS_LOG purge;<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #0000FF;\">exec<\/span><span style=\"color: #000000;\"> drop_if_exists(object_name_in <\/span><span style=\"color: #808080;\">=&gt;<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #FF0000;\">AMIS_LOG<\/span><span style=\"color: #FF0000;\">'<\/span><span style=\"color: #000000;\">)\n<\/span><span style=\"color: #008080;\">--<\/span><span style=\"color: #008080;\"> Create table<\/span><span style=\"color: #008080;\">\n<\/span><span style=\"color: #0000FF;\">create<\/span><span style=\"color: #000000;\"> <\/span><span style=\"color: #0000FF;\">table<\/span><span style=\"color: #000000;\"> AMIS_LOG\n(\n  log_id             <\/span><span style=\"color: #0000FF;\">number<\/span><span style=\"color: #000000;\">,\n...<\/span><\/pre>\n<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http:\/\/dunnhq.com --><\/div>\n<p>If you have any suggestions for this script, let me know. This is a very simple version which doesn\u2019t do all the exception handling possible.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When creating an update script for the database the DBA always nag me about objects to be dropped that don\u2019t exist. My view is that the end result is the same, you are dropping an objects to remove it, so in the end it\u2019s gone. If it\u2019s not there in the first place, then it\u2019s [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,5],"tags":[],"class_list":["post-329","post","type-post","status-publish","format-standard","hentry","category-oracle","category-plsql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=329"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/329\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}