{"id":6,"date":"2006-01-03T14:56:43","date_gmt":"2006-01-03T13:56:43","guid":{"rendered":"http:\/\/bar-solutions.com\/wordpress\/2006\/01\/03\/simple-comparison-sql\/"},"modified":"2006-01-05T09:48:37","modified_gmt":"2006-01-05T08:48:37","slug":"simple-comparison-sql","status":"publish","type":"post","link":"https:\/\/blog.bar-solutions.com\/?p=6","title":{"rendered":"Simple comparison SQL"},"content":{"rendered":"<p>I am comparing tables in two different schemas. Since I am doing the same thing over and over again I created a small script which might be useful to you too.<\/p>\n<p><font color=\"#0000f0\" size=\"1\">ACCEPT<\/font><font size=\"1\"> db1 <\/font><font color=\"#0000f0\" size=\"1\">CHAR<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">prompt<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">&#8216;owner 1 :&#8217;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">ACCEPT<\/font><font size=\"1\"> db2 <\/font><font color=\"#0000f0\" size=\"1\">CHAR<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">prompt<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">&#8216;owner 2 :&#8217;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">ACCEPT<\/font><font size=\"1\"> tabname <\/font><font color=\"#0000f0\" size=\"1\">CHAR<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">prompt<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">&#8216;tablename :&#8217;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"> &#038;db1..&#038;tabname minus &#038;db2..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db1..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">MINUS<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db2..&#038;tabname<\/font><font color=\"#ff0000\" size=\"1\">;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"> &#038;db2..&#038;tabname minus &#038;db1..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db2..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">MINUS<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db1..&#038;tabname<\/font><font color=\"#ff0000\" size=\"1\">;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"> count(*) &#038;db1..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">COUNT<\/font><font color=\"#ff0000\" size=\"1\">(*)<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db1..&#038;tabname<\/font><font color=\"#ff0000\" size=\"1\">;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"> count(*) &#038;db2..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">COUNT<\/font><font color=\"#ff0000\" size=\"1\">(*)<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db2..&#038;tabname<\/font><font color=\"#ff0000\" size=\"1\">;<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"> count(*) union<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">COUNT<\/font><font color=\"#ff0000\" size=\"1\">(*)<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#ff0000\" size=\"1\">(<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db1..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">UNION<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db2..&#038;tabname<br \/>\n<\/font><font color=\"#ff0000\" size=\"1\">);<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">PROMPT<\/font><font size=\"1\"> count(*) union all<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">COUNT<\/font><font color=\"#ff0000\" size=\"1\">(*)<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#ff0000\" size=\"1\">(<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db1..&#038;tabname<br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">UNION<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">ALL<\/font><font size=\"1\"><br \/>\n<\/font><font color=\"#0000f0\" size=\"1\">SELECT<\/font><font size=\"1\"> <\/font><font color=\"#ff0000\" size=\"1\">*<\/font><font size=\"1\"> <\/font><font color=\"#0000f0\" size=\"1\">FROM<\/font><font size=\"1\"> &#038;db2..&#038;tabname<br \/>\n<\/font><font color=\"#ff0000\" size=\"1\">);<\/font><\/p>\n<p>First the script asks for the schemas in which the table reside. Then it asks for the tablename you want to compare. After you have entered this information, the script performs a couple of simple sql statements.<\/p>\n<ol>\n<li>schema1.table minus schema2.table<\/li>\n<li>schema2.table minus schema1.table<\/li>\n<li>count of schema1.table<\/li>\n<li>count of schema2.table<\/li>\n<li>count of (schema1.table union schema2.table)<\/li>\n<li>count of (schema1.table union all schema2.table)<\/li>\n<\/ol>\n<p>The difference between <em>union<\/em> and <em>union all<\/em> is that <em>union<\/em> removes all duplicates from the result set, whereas <em>union all<\/em> just unions all records from both tables.<\/p>\n<p>Notice the double dot (..) notation in &#038;db1..&#038;tabname. If you don&#8217;t use this notation, then Oracle seems to forget the dot between the schemaname and the tablename.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am comparing tables in two different schemas. Since I am doing the same thing over and over again I created a small script which might be useful to you too. ACCEPT db1 CHAR prompt &#8216;owner 1 :&#8217; ACCEPT db2 CHAR prompt &#8216;owner 2 :&#8217; ACCEPT tabname CHAR prompt &#8216;tablename :&#8217; PROMPT PROMPT &#038;db1..&#038;tabname minus [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,9],"tags":[],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-oracle","category-sql"],"_links":{"self":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/6","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=6"}],"version-history":[{"count":0,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.bar-solutions.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}