{"id":84120,"date":"2019-04-30T16:18:05","date_gmt":"2019-04-30T16:18:05","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=84120"},"modified":"2021-07-14T13:06:51","modified_gmt":"2021-07-14T13:06:51","slug":"oracle-for-absolute-beginners-problem-solving-as-a-new-developer","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/databases\/oracle-databases\/oracle-for-absolute-beginners-problem-solving-as-a-new-developer\/","title":{"rendered":"Oracle for Absolute Beginners: Problem-Solving as a New Developer"},"content":{"rendered":"<p>Hi. \u00a0My name is David. If you\u2019ve heard of me, there\u2019s a good chance it\u2019s from my <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/oracle\/oracle-for-absolute-beginners-part-1-databases\/\">Oracle for Absolute Beginners series<\/a> which I write for Simple Talk. (Either that, or you got here from googling the <a href=\"https:\/\/www.clevelandbrowns.com\/team\/players-roster\/david-njoku\/\">famous NFL sportsman<\/a> who has the same name as me. \u00a0Poor guy, I bet journalists get us mixed up and ask him Oracle questions after every touchdown! \u201cYou played well today \u2026 but can you explain Oracle\u2019s implementation of JSON?\u201d)<\/p>\n<p>I have been mentoring some new developers recently, and it has made me realise that I need to revisit my series. Yes, it does a good job of teaching new Oracle developers the basics of the language, but is that enough? \u00a0That\u2019s like teaching someone the alphabet and expecting them to turn into Shakespeare!<\/p>\n<p>If you are an absolute beginner, you do not just need to learn the <em>what;<\/em> you also need someone to teach you <em>how<\/em> to become a developer. What tools do you need? \u00a0How do you use them? Also, when you are assigned a task, how do you go about attacking it?<\/p>\n<p>Most importantly, what do you do when you run into your first hiccup? Because no matter how well you\u2019ve learned your lessons, once you start writing, running, and maintaining code, you will run into problems; you will fail.<\/p>\n<p>The boxer Mike Tyson once said that everyone has a plan \u2026 until they get punched in the face.<\/p>\n<p>I need to train you on what to do when Oracle punches you in the face.<\/p>\n<p>I spend more time looking at error messages than I do looking at my wife. Every developer does. \u00a0I used to hate them; they used to scare me. I used to see them as a judgment on me, on my skills as a developer. I used to think that the fact that my code was erroring meant that I had failed, that I was inadequate. \u00a0Trust me; you will feel that way too.<\/p>\n<p>However, I don\u2019t feel that way now, and you don\u2019t have to either. That\u2019s because I\u2019ve learned the simple steps to follow when my Oracle code breaks down and begins to pelt me with errors.<\/p>\n<p>It is time, dear absolute beginner, that you do, too.<\/p>\n<h2>Debugging<\/h2>\n<p>Switch on <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/oracle\/introduction-to-instrumentation\/\">debugging<\/a>; the messages you receive will walk you through your code. In other words, you\u2019ll get the first debug message first, the second one second, and the third will show up third. Find the juncture where this deviates, and you\u2019ve found the line of code that is erroring. Still not sure what I mean? Don\u2019t worry too much about it now; I\u2019ll give you an example a little later.<\/p>\n<p>One thing that you should bear in mind is that debug messages contained within a loop will be printed once for each iteration of the loop. What that means is that, if your code goes around your loop ten times, your debug message will be output ten times.<\/p>\n<p>On the other hand, messages within a conditional statement will only be output if the code wanders down that branch of the statement. \u00a0Look at the following, for example:<\/p>\n<pre class=\"lang:tsql theme:ssms2012-simple-talk\">IF 1 = 2 THEN\r\n\u00a0\u00a0\u00a0&lt;output debug message&gt;\r\nELSE\r\n\u00a0\u00a0\u00a0\u00a0&lt;output other debug message&gt;\r\nEND IF;\r\n<\/pre>\n<p>The first debug message will never be output since 1 will never be equal to 2. (Despite whatever the old <a href=\"https:\/\/en.wikipedia.org\/wiki\/2_Become_1\">Spice Girls song<\/a> said.)<\/p>\n<p>If your code was not written with <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/oracle\/introduction-to-instrumentation\/\">instrumentation<\/a> already baked in, there\u2019s no need to panic. (And in case you were wondering what <em>instrumentation<\/em> means, in this instance, it\u2019s just a long-winded way of saying <em>debugging<\/em>.) \u00a0As long as you\u2019re working in a development or test environment, you can probably add some debugging now, even if only temporarily.<\/p>\n<p>Do you know about <code>dbms_output.put_line<\/code>? No? It\u2019s a godsend. It\u2019s a standard procedure that enables you to send messages from within PL\/SQL. It\u2019s great for debugging.<\/p>\n<p>Here\u2019s how you\u2019d use it. (This is that example that I promised you a few paragraphs ago.)<\/p>\n<pre class=\"lang:tsql theme:ssms2012-simple-talk\">BEGIN\r\n\u00a0\u00a0\u00a0dbms_output.put_line('start of the procedure');\r\n\u00a0\u00a0\u00a0dbms_output.put_line('before insert statement');\r\n\r\n\u00a0\u00a0\u00a0INSERT INTO table\u2026\r\n\r\n\u00a0\u00a0\u00a0dbms_output.put_line('after insert statement');\r\nEND;<\/pre>\n<p>&nbsp;<\/p>\n<p>When you run your procedure, your messages will be printed to the screen. If, for example, you receive the <em>start of procedure<\/em> message, and the <em>before insert statement<\/em> one, but not the <em>after insert statement<\/em> message, then you know that the error is in your insert statement.<\/p>\n<p>It\u2019s really that simple.<\/p>\n<p>Obviously, there\u2019s a lot more you can do with debugging. If your procedure accepts parameters, output their values in debug messages. Also, if you run a cursor or call a function to populate variables, you should output the new values of those variables in debug messages. So yes, there\u2019s a lot you can do with debug messages (as a matter of fact, I\u2019ve written <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/oracle\/introduction-to-instrumentation\/\">a whole article on the subject<\/a>), but at the very heart of it is a simple truth. When you find the spot where your debug messages either terminate abruptly or deviate from the expected path, then you have found the cause of the error.<\/p>\n<p>Oh, one last thing. If you\u2019re running your code using SQL Plus, and you want to use <code>dbms_output.put_line<\/code>, you\u2019ll need to call the following statement before executing your code:<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:tsql theme:ssms2012-simple-talk\">SET SERVEROUTPUT ON;<\/pre>\n<p>This command instructs SQL Plus to print all debug messages to the screen. Throughout your career you will forget SET SERVEROUT ON; a number of times (everyone does!). On those days, you\u2019ll sit there for five minutes wondering why you\u2019re not getting any debug messages, and then you\u2019ll realise what you\u2019ve done. You\u2019ll feel like a fool. We\u2019ve all been there.<\/p>\n<p>If your <code>dbms_output.put_line<\/code> messages were meant to be temporary, do remember to remove them when you\u2019re done.<\/p>\n<p>Excellent debugging should take the guesswork out of error hunting. When I use <code>dbms_output.put_line<\/code>, I use it liberally &#8211; the way an 8-year-old puts ketchup on his French fries. Everywhere.<\/p>\n<h2>Exceptions<\/h2>\n<p>If your code is not working as expected, but you don\u2019t have errors screaming at you, there\u2019s a chance you have \u00a0<em>When Others <\/em>\u00a0exception-handling in place that is holding a pillow over your errors\u2019 mouths and shutting them up. You do not need <em>When Others<\/em> exception handling, but if you must have it, then re-raise your errors. \u00a0Alternatively, you can use our friend <code>dbms_output.put_line<\/code> and print out one of the following: <code>SQLERRM<\/code> or <code>SQLCODE<\/code>. \u00a0<code>SQLERRM<\/code> will give you the error message; <code>SQLCODE<\/code> will strip out the verbiage and just give you the error number.<\/p>\n<p>&nbsp;<\/p>\n<pre class=\"lang:tsql theme:ssms2012-simple-talk\">BEGIN \r\n\r\n\u00a0\u00a0\u2026\r\nEXCEPTION\r\n\u00a0\u00a0WHEN OTHERS THEN\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dbms_output.put_line('The error is '||SQLERRM);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0dbms_output.put_line('The error code is '||SQLCODE);\r\nEND;<\/pre>\n<h2>Take a break<\/h2>\n<p>If you still can\u2019t figure out where your error is coming from, take a break. Come back to it in five minutes with fresh eyes. Go make a coffee. \u00a0If you smoke, go and have a cigarette. I don\u2019t smoke, so I hide in an empty meeting room and spend five minutes scrolling through Twitter. Now that I think of it, that\u2019s probably worse for me than smoking!<\/p>\n<h2>Look at your code with fresh eyes<\/h2>\n<p>Several simple tricks will help you achieve this. The very best one is to speak to someone. I\u2019ve found that often the simple act of explaining my problem to another developer is all it takes for the solution to come to me. Talking about a problem brings it into clearer focus.<\/p>\n<p>There are other ways of doing this. \u00a0Sometimes I skip the code and read <a href=\"https:\/\/www.red-gate.com\/simple-talk\/sql\/oracle\/how-to-make-comments-the-most-important-code-you-write\/\">the comments<\/a> instead. \u00a0Sometimes I get a notepad and write it all out as pseudocode.<\/p>\n<p>I try to force myself to eliminate assumptions. \u00a0It is always that line of code that you think will never fail that is the source of the error.<\/p>\n<h2>Google<\/h2>\n<p>There is nothing new under the sun; no matter how obscure your problem is, someone else has encountered it before &#8211; and they\u2019ve probably blogged about it too!<\/p>\n<p>\nIf you\u2019re looking for quick answers, there are a number of reliable sites.<\/p>\n<ul>\n<li><a href=\"https:\/\/asktom.oracle.com\/pls\/apex\/f?p=100:1000::::::\">Asktom.oracle.com<\/a> is excellent. It\u2019s run by a guy named Tom Kyte, and he\u2019s been answering Oracle questions on his site for 20 years, if not longer. Sometimes he gets grumpy when he\u2019s asked what he thinks is a silly question, but you don\u2019t have to ask questions, you can just search the archives for the problems that match yours.<\/li>\n<li><a href=\"https:\/\/stackoverflow.com\/questions\/tagged\/oracle\">Stack Overflow<\/a> is this massive resource of questions and answers that cover every aspect of development. The quality of the answers can, sometimes, be variable.<\/li>\n<li><a href=\"https:\/\/community.oracle.com\/community\/groundbreakers\/database\/developer-tools\/sql_and_pl_sql\">Oracle Community<\/a> is a site where Oracle developers ask and answer questions. \u00a0Some of the best Oracle developers in the world are active members of the community.<\/li>\n<li><a href=\"http:\/\/www.dba-oracle.com\/\">Burleson Consulting<\/a> is a weird-looking site that is styled like it just stepped out of the 1990s. However, it does often contain useful, clearly-presented information. \u00a0Even if it looks like it\u2019s out of the 1990s, its SEO must be cutting edge because it always comes near the top of every Google search.<\/li>\n<\/ul>\n<h2>Build your knowledge<\/h2>\n<p>Copying and pasting fixes from Stack Overflow might help you at the moment, but the only way to get better at problem-solving is to build your knowledge. There are several great resources for that: Simple Talk is excellent (and it\u2019s not just because I\u2019m on it!). \u00a0Also, Google a guy named Steven Feuerstein. He\u2019s one of the best PL\/SQL writers out there.<\/p>\n<h2>Learn from your mistakes<\/h2>\n<p>This is often the hardest thing. \u00a0The number of times I\u2019ve thought \u201cif only I\u2019d put a bit more debugging in this package\u201d or \u201cwhy on earth didn\u2019t I comment this code?\u201d And then I\u2019d go straight ahead and NOT put in additional debugging or commenting. Don\u2019t be like me. \u00a0Don\u2019t be like me at all.<\/p>\n<h2>Conclusion<\/h2>\n<p>It doesn\u2019t matter what platform you use when you code, errors will happen. They don\u2019t mean you are a failure; you need to learn how to use the tools to troubleshoot, debug, and fix the problems. Here, I\u2019ve tried to show you a few techniques that have helped me over the years.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learning programming logic and syntax is just the beginning. In this article, David Njoku talks about how to debug and troubleshoot errors as a new developer.&hellip;<\/p>\n","protected":false},"author":221907,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[53,143533],"tags":[124952],"coauthors":[48557],"class_list":["post-84120","post","type-post","status-publish","format-standard","hentry","category-featured","category-oracle-databases","tag-redgate-deploy"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/84120","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/221907"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=84120"}],"version-history":[{"count":4,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/84120\/revisions"}],"predecessor-version":[{"id":84122,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/84120\/revisions\/84122"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=84120"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=84120"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=84120"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=84120"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}