PL/SQL Does not return Datasets in the same way that other RDBMS do (like SQL Server, where you can just pop a random SELECT statement anywhere in a block or procedure and it will output the results to the grid).
Oracle will only output to the screen as a standard SELECT query or via a refcursor.
What you're doing is known as an anonymous block. Anonymous Blocks do not display the results of a SELECT statement. They exist to "get work done."
I wrote a blog post that explains this a bit, you're welcome to take a look: https://sqldevdba.com/f/t-sql-vs-plsql-series-part-2-select-into
For your issue:
You have a few options, including:
- Output the Select statement using a RefCursor.
(https://oracle-base.com/articles/misc/using-ref-cursors-to-return-recordsets)
- Write the results to a table (Using Insert INTO ... SELECT FROM), then Select from that table when done. (This is my preferred approach when running scheduled items like reports).
- Printing each record to the DBMS_OUTPUT (yuck)
For option 2 (which I'd likely take but of course you don't have to, it would be something like:
--If you have an existing table (TableName) that you always want to use
DECLARE
v_dbver BINARY_INTEGER;
BEGIN
SELECT TO_NUMBER(SUBSTR(version,1,2)) INTO v_dbver FROM v$instance;
DBMS_OUTPUT.PUT_LINE('Version ='||v_dbver);
--This table needs to exist already, so you'll have to have created it ahead of time
TRUNCATE TABLE TableNameHere; --Truncate the table to remove artifacts
IF v_dbver < 12 THEN
DBMS_OUTPUT.PUT_LINE('Running against 11g');
--11g
INSERT INTO TableName
(NAME,OPEN_MODE,DBID)
SELECT NAME,OPEN_MODE,DBID
FROM v$database;
ELSIF v_dbver >= 12 THEN
DBMS_OUTPUT.PUT_LINE('Running against 12c');
--12c
INSERT INTO TableName
(NAME,OPEN_MODE,DBID,con_id)
SELECT NAME,OPEN_MODE,DBID,con_id
FROM v$database;
END IF;
END;
--If you want it to DYNAMICALLY create the table:
DECLARE
v_dbver BINARY_INTEGER;
BEGIN
SELECT TO_NUMBER(SUBSTR(version,1,2)) INTO v_dbver FROM v$instance;
DBMS_OUTPUT.PUT_LINE('Version ='||v_dbver);
--This table needs to exist already, so you'll have to have created it ahead of time
TRUNCATE TABLE TableNameHere; --Truncate the table to remove artifacts
IF v_dbver < 12 THEN
DBMS_OUTPUT.PUT_LINE('Running against 11g');
--11g
CREATE TABLE TableName_Timestamp --This will auto-create the table
AS
SELECT NAME,OPEN_MODE,DBID
FROM v$database;
ELSIF v_dbver >= 12 THEN
DBMS_OUTPUT.PUT_LINE('Running against 12c');
--12c
CREATE TABLE TableName_Timestamp --This will auto-create the table
AS
SELECT NAME,OPEN_MODE,DBID,con_id
FROM v$database;
END IF;
END;