RE:- HELP! Cursor results displayed in table

  • I have the following code below that prints a set of results to screen using put_line but I need to display the results as a table. Is it possible to do this or can someone suggest another better way of acheiving this. Any help would be appreciated. If any more detail is required please ask.

    WHENEVER sqlerror exit failure

    set linesize 300

    set feed off

    set serveroutput on

    set verify off

    set trimspool on

    set termout off

    spool ss.txt

    DECLARE

    -- CURSORS

    CURSOR C1 IS

    select location_name, address from location where location_name like 'HOR%';

    CURSOR C2 IS

    SELECT OPERATOR_CLASS FROM OPERATOR_CLASS_DFN where STATUS = 0 and OPERATOR_CLASS NOT IN ('Analyst','DCC','Viewer','Incident Updater','Schedule Creator');

    CURSOR C3 (s_client VARCHAR2, s_operator_class VARCHAR2) IS

    SELECT CLIENT

    from CONNECTION_OPERATOR_CLASS

    where client = s_client

    and for_operator_class = s_operator_class

    and ALLOW_OR_DENY = 'DENY'

    and status = 0;

    -- VARIABLES

    v_location_name location.LOCATION_NAME%TYPE;

    v_address location.ADDRESS%TYPE;

    v_operator_class operator_class_dfn.OPERATOR_CLASS%TYPE;

    s_client connection_operator_class.CLIENT%TYPE;

    s_operator_class connection_operator_class.FOR_OPERATOR_CLASS%TYPE;

    r_client connection_operator_class.CLIENT%TYPE;

    -- MAIN BODY

    BEGIN

    OPEN C1;

    LOOP

    FETCH C1 into v_location_name, v_address;

    EXIT WHEN C1%NOTFOUND;

    OPEN C2;

    LOOP

    FETCH C2 into v_operator_class;

    EXIT WHEN C2%NOTFOUND;

    OPEN C3 (v_location_name,v_operator_class);

    r_client := NULL;

    FETCH C3 into r_client;

    IF r_client IS NULL

    THEN

    dbms_output.put_line ('NOT FOUND :'||v_location_name||'|'||v_address||'|'||v_operator_class||'|');

    END IF;

    CLOSE C3;

    END LOOP;

    CLOSE C2;

    END LOOP;

    CLOSE C1;

    END;

    /

    exit;

  • Hi Chris,

    The SQL is quite hard to read (and I have never come across case statements & cursors used like that before) but rather than using a cursor couldn't you write this as a SQL statement doing outer joins on the tables to achieve the same results?

    To be honest I only every really use cursors for breaking large transactions into smaller ones (inserting data per month etc).

    In answer to your question I guess you could insert you results into a temp table than report from that(?)

    Mack

  • Thanks for your reply.

    You have answered another question I had inadvertently. I thought this statement was over complicated and that using joins to achieve the same results would be a better option. I think I will just start from scratch with this one.

    Chris 🙂

  • christopher.grant (10/31/2012)


    I have the following code below that prints a set of results to screen using put_line but I need to display the results as a table. Is it possible to do this or can someone suggest another better way of acheiving this. Any help would be appreciated. If any more detail is required please ask.

    This isn't sql server code, this is Oracle code. You will find far more help on an Oracle forum. This forum is dedicated to SQL Server.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Mackers (10/31/2012)


    Hi Chris,

    The SQL is quite hard to read (and I have never come across case statements & cursors used like that before) but rather than using a cursor couldn't you write this as a SQL statement doing outer joins on the tables to achieve the same results?

    To be honest I only every really use cursors for breaking large transactions into smaller ones (inserting data per month etc).

    In answer to your question I guess you could insert you results into a temp table than report from that(?)

    Mack

    Next time you feel you need a cursor for this type of an operation, start a new post on SSC. From the description I doubt you need a cursor for that type of thing either. 😛

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (10/31/2012)


    Mackers (10/31/2012)


    Hi Chris,

    The SQL is quite hard to read (and I have never come across case statements & cursors used like that before) but rather than using a cursor couldn't you write this as a SQL statement doing outer joins on the tables to achieve the same results?

    To be honest I only every really use cursors for breaking large transactions into smaller ones (inserting data per month etc).

    In answer to your question I guess you could insert you results into a temp table than report from that(?)

    Mack

    Next time you feel you need a cursor for this type of an operation, start a new post on SSC. From the description I doubt you need a cursor for that type of thing either. 😛

    Unlike to SQL Server, ORACLE cursors usually do not have performance problems. However, I've seen few cases where rewriting CURSORS into set-based queries did bring some performance benefits even in Oracle.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (11/2/2012)


    Sean Lange (10/31/2012)


    Mackers (10/31/2012)


    Hi Chris,

    The SQL is quite hard to read (and I have never come across case statements & cursors used like that before) but rather than using a cursor couldn't you write this as a SQL statement doing outer joins on the tables to achieve the same results?

    To be honest I only every really use cursors for breaking large transactions into smaller ones (inserting data per month etc).

    In answer to your question I guess you could insert you results into a temp table than report from that(?)

    Mack

    Next time you feel you need a cursor for this type of an operation, start a new post on SSC. From the description I doubt you need a cursor for that type of thing either. 😛

    Unlike to SQL Server, ORACLE cursors usually do not have performance problems. However, I've seen few cases where rewriting CURSORS into set-based queries did bring some performance benefits even in Oracle.

    To wit, Oracle can't return the results of SELECT to a GUI and unless MS came up with a really improved drive, you have to build a "Reference Cursor" for that. Most people simplify that problem by including the stored procedures in a PACKAGE that contains a "global reference cursor".

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply