|
|
|
Forum 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;
|
|
|
|
|
SSC-Enthusiastic
      
Group: General Forum Members
Last Login: Yesterday @ 3:58 AM
Points: 117,
Visits: 458
|
|
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
|
|
|
|
|
Forum 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
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 8,547,
Visits: 8,204
|
|
|
|
|
|
SSCrazy Eights
        
Group: General Forum Members
Last Login: Yesterday @ 8:46 AM
Points: 8,547,
Visits: 8,204
|
|
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
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 10:59 AM
Points: 2,525,
Visits: 4,324
|
|
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(?)
MackNext 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
|
|
|
|
|
SSC-Dedicated
           
Group: General Forum Members
Last Login: Yesterday @ 6:33 PM
Points: 32,889,
Visits: 26,757
|
|
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(?)
MackNext 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."
For better, quicker answers on T-SQL questions, click on the following... http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following... http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
|
|
|
|