Intro to Oracle for SQLServer Developers and DBAs

1. Oracle has something called V$ tables, which are essential similar to the Dynamic Management View (DMV) of SQLServer, Here are two examples:

select * from v$database;
select * from v$thread;

Result – this shows that my 11g Express Edition has both an instance name of “xe” and a database name of “XE”. See “Intro to Oracle SQL for SQLServer Developers and DBAs – Part 1” for an explanation of instances vs databases.
---------- --------- --------- ----------------- --------------
2749400775 XE        06-JAN-15            370965 06-JAN-15

---------- ------ -------- ---------- --------------------------------------------------------------------------------
1 OPEN   PUBLIC            2 xe

2. Oracle has an odd “fake” table called “dual”.

Oracle always requires a table name, so this is the dummy “catch-all” table”  The links below will you the full scoop and history of why it was called “dual”.

select 5+5 from dual;
select sysdate into myDate from dual;

Additional explanation of the “Dual” table;

3. The following example will illustrate many things I put together, from dozens of examples found in many different sources.

Sample Code

set serveroutput on
select 5+5 from dual;
select sysdate from dual;
   myDate date;
   myNumber number;
   myString varchar2(50) := 'Initial Value';
   select sysdate into myDate from dual;
   myNumber := 10;
   myNumber := myNumber + 8;
   myString := 'New Value';
   -- use this function/method to print
   DBMS_OUTPUT.put_line ('The date is ' || myDate);
   DBMS_OUTPUT.put_line ('myNumber is ' || myNumber);
   DBMS_OUTPUT.put_line ('myString is ' || myString);

SQL Output



anonymous block completed
The date is 09-JAN-15
myNumber is 18
myString is New Value

Dbms Output Window:

The date is 09-JAN-15
myNumber is 18
myString is New Value

1. “set serveroutput on” will bring the needed to use the DBMS_OUPUT_put_line into the SQL result window.  Otherwise, it will only show in the “Dbms Output” window.  “:DBMS_Output.put_line()” is basically the SQLServer Print command. Use || to concatenate, and to actually see the results of these ‘print’ statement, you may have to manually “view” and then “enable” the DBMS-Output window in the SQL “GUI” tool that you are using.

2. “from dual” see prior point above.

3.  declare/begin/end – is the structure – variables have local scope to the block in which they are declared, and these blocks can be nested.

4.  Semicolons to end the lines are very important!  This small sample gives you an idea of where they go, and where they don’t go.

5. “Number” is a data type  You could use “PLS_INTEGER” is you want an integer.  VarChar2(nn) is the standard string type.

6. := is the assignment operator, to set the value of something equal to another value

7.  Comments work the same between PL/SQL and Transact-SQL (T-SQL)