Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


RE:- HELP! Cursor results displayed in table


RE:- HELP! Cursor results displayed in table

Author
Message
christopher.grant
christopher.grant
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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;
Mackers
Mackers
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
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
christopher.grant
christopher.grant
Forum Newbie
Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)Forum Newbie (2 reputation)

Group: General Forum Members
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 :-)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
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)
Sean Lange
Sean Lange
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16539 Visits: 16992
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. :-P

_______________________________________________________________

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)
Eugene Elutin
Eugene Elutin
Hall of Fame
Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)Hall of Fame (3K reputation)

Group: General Forum Members
Points: 3040 Visits: 5478
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. :-P


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
Jeff Moden
Jeff Moden
SSC-Forever
SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)SSC-Forever (44K reputation)

Group: General Forum Members
Points: 44968 Visits: 39862
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. :-P


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.
Although they tell us that they want it real bad, our primary goal is to ensure that we dont actually give it to them that way.
Although change is inevitable, change for the better is usually not.
Just because you can do something in PowerShell, doesnt mean you should. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search