tags:

views:

439

answers:

1

Hi all,

I have used this type of functionality before but is MSSQL but can't get it to work for Oracle, any tips?

DECLARE
    MY_TBL NUMBER := 1;

BEGIN

    IF(MY_TBL > 0) THEN
        SELECT * FROM MY_TBL ORDER BY MY_TBL_ID DESC;
    END IF;
END;

What I would like to have is a flag variable set to zero or one, if one display the results, if zero skip. This is just a simple script to have all my tables and select statements in one script ans flag the ones I need to see the results for. So if I have 5 tables I would use the ELSIF function to add more like this

DECLARE
    MY_TBL1 NUMBER := 1;
    MY_TBL2 NUMBER := 1;

BEGIN

    IF(MY_TBL1 > 0) THEN
        SELECT * FROM MY_TBL1 ORDER BY MY_TBL1_ID DESC;
    ELSIF(MY_TBL2 > 0) THEN
        SELECT * FROM MY_TBL2 ORDER BY MY_TBL2_ID DESC;
    END IF;
END;

and so on. Thanks for any help on this, --Phill

EDIT:

Here is what I have:

VAR result_set REFCURSOR

DECLARE
   my_tbl_1 NUMBER := 1;
   my_tbl_2 NUMBER := 0;
   my_tbl_3 NUMBER := 0;
BEGIN
   IF (my_tbl_1 > 0)
   THEN
      BEGIN
         OPEN :result_set FOR
            SELECT   *
                FROM my_tbl_1
            ORDER BY my_tbl_1_id DESC;
      END;
   ELSIF (my_tbl_2 > 0)
   THEN
      BEGIN
         OPEN :result_set FOR
            SELECT   *
                FROM my_tbl_2
            ORDER BY my_tbl_2_id DESC;
      END;
   ELSIF (my_tbl_3 > 0)
   THEN
      BEGIN
         OPEN :result_set FOR
            SELECT   *
                FROM my_tbl_3
            ORDER BY my_tbl_3_id DESC;
      END;
   END IF;
END;

PRINT result_set
+1  A: 

Oracle cannot return resultsets from a stored procedure as SQL Server does.

Declare cliend side cursor variables and return to them:

To display a cursor in SQL*Plus:

VAR cur1 REFCURSOR

DECLARE
        MY_TBL1 NUMBER := 1;
        MY_TBL2 NUMBER := 1;

BEGIN
        IF(MY_TBL1 > 0) THEN
        BEGIN
                OPEN :cur1
                FOR
                SELECT  *
                FROM    MY_TBL1
                ORDER BY
                        MY_TBL1_ID DESC;
        END;
        ELSIF (MY_TBL2 > 0) THEN
        BEGIN
                OPEN :cur1
                FOR
                SELECT  *
                FROM    MY_TBL2
                ORDER   BY
                        MY_TBL2_ID DESC;
        END;
        END IF;
END;
/

PRINT cur
Quassnoi
I was looking at the CURSOR but does it display to the screen?
Phill Pafford
What do you mean by "it"? Variables do not display on the screen, the client applications do. In `SQL*Plus`, you can declare a `CURSOR` variable and `PRINT` it.
Quassnoi
it = result set
Phill Pafford
Tried your method and still getting an error, Does the VAR cur1 REFCURSOR need to be before the DECLARE? and PRINT outside of the END? what about / is this a typo?
Phill Pafford
@Phill: what client application do you use?
Quassnoi
toad for Oracle version 9.6.1
Phill Pafford
it's asking for the VAR data type, I have tried a couple of different types but they all give an error
Phill Pafford
`@Phill`: Oracle cannot return a resultset right out of the procedure. You can create a `PIPELINE` function, but you won't be able to use any `DML` in it. You need find TOAD's equivalents of two things: declaring a cursor variable (`VAR cur1 REFCURSOR` in `SQL*Plus`) and fetching it ('PRINT cur')
Quassnoi
I don't have `TOAD` installed, so can't help you it this, sorry.
Quassnoi
I also can use DBVisulaizer (free version)
Phill Pafford
@Phill Pafford, Why don't you start SQL PLUS?
Theo
What's SQL PLUS??? New to the Oracle stuff but have good experience with MySQL
Phill Pafford
`@Phill`: it's a command line query tool for `Oracle`, bundled with every installation. `%ORACLE_HOME%/bin/sqlplus.exe` on `Windows`, `$ORACLE_HOME/bin/sqlplus` on `Unix`.
Quassnoi
The BEGIN/END are unnecessary in the IF/ELSE statement. You only need a single BEGIN/END pair to represent the body of the sproc. Also, you closed the sproc with END; / - this will probably give an error, it should be ";" or "/" not both.
OMG Ponies
`@rexem`: begin/end are unnecessary indeed, but I prefer using them (to add additional statements if needed). As for trailing `/`, `SQL*Plus` requires it to send the `SQL` buffer to the server. You need both `;` and `/`
Quassnoi