My first days in programming started off with Oracle v 7x and I have to admit, it's my first love. For the last two years I've been either administrating and/or programming SQL Server (which I also love) but I've gotten a little homesick lately. Let me run through a few of the basics of programming Oracle.
As you may be aware, SQL is a wonderful language but it's , well, not the most modern thing. The first implementation was created before I was born and pretty much the same as it was then. If I sound like I'm giving SQL short shrift, I'm not. On the contrary, many things are classics like SQL, the Porsche 911 and Jaguar X series come to mind. They look pretty much like they did 30 years ago, and they are still way cool. The same holds for SQL. However, SQL lacks many constructs that have become staples of modern programming and every real database implements its own flavor of SQL. For Oracle, that's PL/SQL (Procedural Language/Structured Query Language).
For most basic queries, you can get away with good ol' SQL, but I'll walk you through a few other examples.
| Scenario 1 (Basic Select Statement). SELECT FirstValue, SecondValue FROM myTable Where Condition1 = 'Whatever' AND Condition2 = 'SomethingElse' OR Condition3 = 'SomethingTotallyDifferent' |
| DECLARE v_Whatever NUMBER(4); DECLARE v_SomethingElse VARCHAR2(50); BEGIN SELECT FirstValue, SecondValue FROM myTable Where Condition1 = v_Whatever AND Condition2 = v_SomethingElse; END; |
| DECLARE v_Whatever NUMBER(4); DECLARE v_SomethingElse VARCHAR2(50); BEGIN SELECT FirstValue, SecondValue INTO v_Whatever, v_SomethingElse FROM myTable WHERE Condition1 = 'Whatever'; END; |
| DECLARE v_Whatever NUMBER(4); DECLARE v_SomethingElse VARCHAR2(50); BEGIN SELECT FirstValue, SecondValue INTO v_SomethingElse FROM myTable Where Condition1 = v_Whatever; END; |
| SCENARIO 2: Basic Insert Statement Let's say we want to use PL/SQL to insert a few variables into a table. DECLARE v_Whatever NUMBER(4); DECLARE v_SomethingElse VARCHAR2(50); BEGIN INSERT INTO myTable(Column1, Column2) VALUES (v_Whatever, v_SomethingElse); END; |
| SCENARIO 3: Basic Update Statement DECLARE v_Whatever NUMBER(4); DECLARE v_SomethingElse VARCHAR2(50); DECLARE v_SomeEvalValue NUMBER(2) ; BEGIN UPDATE myTable SET myFirstColumn = v_Whatever , mySecondColumn = v_SomethingElse WHERE someField = v_SomeEvalValue; END; SCENARIO 4: Deleting Data DECLARE v_SomeEvalValue NUMBER(2) BEGIN DELETE FROM myTable WHERE someField = v_SomeEvalValue; END; |