Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

RE:- HELP! Cursor results displayed in table Expand / Collapse
Author
Message
Posted Wednesday, October 31, 2012 7:53 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 31, 2012 8:18 AM
Points: 2, Visits: 1

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;
Post #1379317
Posted Wednesday, October 31, 2012 8:16 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Monday, January 13, 2014 7:34 AM
Points: 117, Visits: 499
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
Post #1379335
Posted Wednesday, October 31, 2012 8:22 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 31, 2012 8:18 AM
Points: 2, Visits: 1
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
Post #1379341
Posted Wednesday, October 31, 2012 8:22 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1379344
Posted Wednesday, October 31, 2012 8:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: 2 days ago @ 2:24 PM
Points: 11,990, Visits: 11,007
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 Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1379347
Posted Friday, November 02, 2012 5:31 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, April 03, 2014 10:10 AM
Points: 2,792, Visits: 4,874
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1380288
Posted Friday, November 02, 2012 6:42 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 4:06 PM
Points: 35,969, Visits: 30,262
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." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1380653
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse