Skip to main content

Posts

Showing posts with the label case

Reduce the volume of PL/SQL code you write with these tips

I'm not known for being concise. I'm best known in the world of Oracle Database for my "magnum opus" Oracle PL/SQL Programming , which checks in at 1340 pages (the index alone is 50 pages long). But I've picked up a few tips along the way for writing PL/SQL code that is, well, at least not as long, as verbose, as it could have been. And certainly shorter than my books. :-) You probably have some ideas of your own; please offer them in comments and I will add them to the post. Qualified Expressions (new to 18c) In the bad old days before Oracle Database 18c was released (and is now available for free in its "XE" form ), if you wanted to initialize an associative array with values, you had to do in the executable section as follows: DECLARE TYPE ints_t IS TABLE OF INTEGER INDEX BY PLS_INTEGER; l_ints ints_t; BEGIN l_ints (1) := 55; l_ints (2) := 555; l_ints (3) := 5555; FOR indx IN 1 .. l_in...

PL/SQL 101: Writing conditional logic in PL/SQL

PL/SQL offers a number of options when it comes to writing conditional logic, as in variations on "If this then that". This post reviews them, and provides examples. You can choose from the following: IF statement - IF, IF-THEN, IF-ELSE, IF-ELSIF-ELSE CASE statement - simple and searched CASE CASE expression - the expression "version" of the statement (and one of my favorite elements of PL/SQL) IF Statement It's hard to imagine a programming language without a version of IF, and PL/SQL is no exception.  The IF statement either runs or skips a sequence of one or more statements, depending on a condition. The IF statement can take one of three forms, but they all have this in common: they start with "IF" and then end with "END IF;". IF THEN The simplest IF statement: if the boolean expression evaluates to TRUE then execute the statements between THEN and END IF. Example: BEGIN IF l_hire_date < SYSDATE THEN ...