Confused about how to use ORDER BY in the UNION

I want to dynamically generate the commands in the following order
1. delete the user
2 ALTER DATBASE... .resize command for that user tablespace
3 DROP TABLESPACE for this user

So, I wrote the following sql code

SQL > set lines 200
SQL > select 'drop user '||username|| ' cascade;' from dba_users 
  2  where username in ('S_DMJ','S_MBIN','S_URTL') 
  3  union
  4  select 'alter database datafile '||file_name ||' RESIZE 5M;' FROM DBA_DATA_FILES
  5  WHERE TABLESPACE_NAME IN ('S_DMJ','S_MBIN','S_URTL')
  6  UNION
  7  SELECT 'DROP TABLESPACE '||TABLESPACE_NAME|| ' INCLUDING CONTENTS AND DATAFILES;'
  8  FROM DBA_TABLESPACES
  9  WHERE TABLESPACE_NAME IN ('S_DMJ','S_MBIN','S_URTL');

'DROPUSER'||USERNAME||'CASC
----------------------------------------------------------------------------------------------------
DROP TABLESPACE S_URTL INCLUDING CONTENTS AND DATAFILES;
DROP TABLESPACE S_DMJ INCLUDING CONTENTS AND DATAFILES;
DROP TABLESPACE S_MBIN INCLUDING CONTENTS AND DATAFILES;
alter database datafile +DATA/LMPROD/datafile/S_URTL.dbf RESIZE 5M;
alter database datafile +DATA/LMPROD/datafile/S_DMJ.dbf RESIZE 5M;
alter database datafile +DATA/LMPROD/datafile/S_MBIN.dbf RESIZE 5M;
drop user S_URTL cascade;
drop user S_DMJ cascade;
drop user S_MBIN cascade;

9 rows selected.

SQL > 
I need output in this order for each diagram like this
drop user S_URTL cascade;
alter database datafile +DATA/LMPROD/datafile/S_URTL.dbf RESIZE 5M;
DROP TABLESPACE S_URTL INCLUDING CONTENTS AND DATAFILES;

Hello

Think very carefully about why you need to do this in SQL * more.
Because you need an automated process, PL/SQL would be better?
When you use UNION, you cannot ORDER BY something that is not in the SELECT clause. You can easily assign a sort key in every branch of the UNION and use SQL * more control "COLUMN sort_key NOPRINT" to prevent it to appear in the output, even if it has as a result set.

In pure SQL, you can make the UNION in a subquery, then the main query (with the ORDER BY clause) does not need to include the sort key in the output:

WITH     union_data     AS
(
     SELECT      'drop user '
          || username
          || ' cascade;'                    AS sql_txt
     ,          username || ' 1'               AS sort_key
     FROM     dba_users
     WHERE     username     IN ('S_DMJ','S_MBIN','S_URTL')
    UNION
        SELECT  'alter database datafile '
          || file_name
          ||' RESIZE 5M;'                    AS sql_txt
     ,          tablespace_name || '2'               AS sort_key
     FROM       dba_data_files
     WHERE      tablespace_name IN ('S_DMJ','S_MBIN','S_URTL')
    UNION
     SELECT  'DROP TABLESPACE '
          || tablespace_name
          || ' INCLUDING CONTENTS AND DATAFILES;'     AS sql_txt
     ,          tablespace_name || '2'               AS sort_key
        FROM      dba_tablespaces
     WHERE     tablespace_name     IN ('S_DMJ','S_MBIN','S_URTL');
)
SELECT       sql_txt
FROM       union_data
ORDER BY  sort_key
;

Published by: Frank Kulash, June 8, 2011 08:49

Tags: Database

Similar Questions

  • Using ORDER BY in a UNION of cursor clause

    Hello world
    I have a situation and I need your help guys. I have to use ORDER BY in the UNION of two queries and return value in the cursor.

    OPEN FOR cursor
    SELECT ID, DESC, SID, ITÍD, SID_DESC
    FROM (SELECT A.ID,
    A.DESC,
    SELECT,
    VALUE NULL AS ITÍD,
    B.SID_DESC
    FROM TABLEA, TABLEB B
    WHERE A.ID = B.ID
    order of A.SORTORDER CSA, B.SORTORDER)
    UNION
    SELECT ID, DESC, SID, ITÍD, ITID_DESCRIPTION,
    FROM (SELECT A.ID,
    A.DESC,
    NULL AS SID VALUE,
    C.ITID,
    C.ITID_DESC,
    FROM TABLEA,.
    TABLEC C
    WHERE A.ID = C.ID
    order of A.SORTORDER CSA, C.SORTORDER)


    I have SORT ORDER is the column in the three tables. TABLEA has a unique number for each record as sort order. 1 for each id from TABLEB to sortorder. If id is twice then its 1 and 2 for each ID TABLEC has sort order in TABLEB, 1 for each id, and that id is twice then it's 1 and 2 and id id is three times, it's 1, 2, 3.

    I do not receive correct sort that I can not use the command by like that in the UNION. Please let me know how I can handle this.

    Thanks in advance to everyone.
    I'd really appreciate your comments and your responses.

    You can try,

    SELECT ID,
           DESC,
           SID,
           ITID,
           SID_DESC
      FROM (SELECT A.ID,
                   A.DESC,
                   B.SID,
                   NULL AS ITID,
                   B.SID_DESC,
                   A.sortorder AS sortorder1,
                   b.sortorder AS sortorder2
              FROM TABLEA A, TABLEB B
             WHERE A.ID = B.ID
            UNION
            SELECT A.ID,
                   A.DESC,
                   NULL AS SID,
                   C.ITID,
                   C.ITID_DESC,
                   A.sortorder AS sortorder1,
                   c.sortorder AS sortorder2
              FROM TABLEA A, TABLEC C
             WHERE A.ID = C.ID)
    ORDER BY SORTORDER1 ASC, SORTORDER2
    

    G.

  • I got a license number for Adobe Master Collection from my school (Stevens Institute of Technology) and I can't understand how to use it to obtain the product. I created a user name and this is as far as I'm away.

    I got a license number for Adobe Master Collection from my school (Stevens Institute of Technology) and I can't understand how to use it to obtain the product. I created a user name and this is as far as I'm away. I need to download the product and I may not know where to start.

    Ask someone to your school... You have a Cloud redemption code, or a number of series CS6?

    If cloud, http://helpx.adobe.com/x-productkb/global/redemption-code-help.html Redemption Code and

    http://www.Adobe.com/products/creativecloud/FAQ.html

    http://helpx.adobe.com/creative-cloud/help/install-apps.html to install or uninstall

    http://forums.Adobe.com/community/download_install_setup/creative_cloud_faq

    What is http://helpx.adobe.com/creative-cloud/help/creative-cloud-desktop.html

    To start https://helpx.adobe.com/creative-cloud.html the Cloud

    If CS6, download to other downloads and enter your serial number when prompted

    Once again, your 1 resource is your school

  • How to use JavaScript to change the named view

    How to use JavaScript to change the named view

    Do you mean to jump to a specific Destination named within a PDF file? SE so there

    the method gotoNamedDest of the Document object.

  • How to use CSS to create the horizontal navigation bar in Dreamweaver CS6

    How to use CSS to create the horizontal navigation bar in Dreamweaver CS6

    One of the ways to do this is the following: Editor Tryit v1.9

    You can also use floats to get something in the same direction.

  • How to use JavaScript to limit the number of boxes to check the user can select a PDF form?

    How to use JavaScript to limit the number of boxes to check the user can select a PDF form?

    Here's the working solution

    Please rename all the boxes like chkBox0, chkBox1... .chkBox30 (must start with 0)

    Value of exports of each checkbox must be "Yes" (default value is Yes)

    Place this Code at the level of the Javascript Document

    -Code JavaScript document-

    var counter = 0;                Checked the meter

    -Count boxes checked again when the document is closed and reopened.

    for (var i = 0; i)<= 30;="">

    If (getField("chkBox"_+_i).value == 'Yes') {}

    Counter += 1;

    }

    }

    -The function of validation-

    function validateCheckBox(name,value) {}

    If (value == 'Yes' & meter)<>

    Counter += 1;

    } else if (value == 'Off') {}

    counter = 1;

    } else {}

    getField ("name") .value = "Off";

    App.Alert ("you can select only four checkboxes");

    }

    }

    -End of Document Javascript-

    Now put this line in the MouseUp event all checkboxes

    Box - Code MouseUp event.

    validateCheckBox (event.target.name, event.target.value);

    //----------------------------------------------------------------------------------

    Hope this will help

    Kind regards

    Post edited by: Mohammad Irfan

  • How to use a structure of the event in a Subvi

    I am using a structure of the event within a Subvi.  The Subvi has a table constructor that built tables and the structure of the event is used so that when the program runs, it allows you to change the elements of the array.  It works, but when I apply within a Subvi, it does not work properly.  This goes into a Subvi, because it will be part of a much larger progrram, so it must be modular.

    If you are using a structure of the event, it is not a Subvi.  If you worry about modularity, you should not use the structure of the event.

    You seem to have a fundamental misunderstanding of modularity.  An event structure must be placed in a while loop.  If you have a while loop in a Subvi, you have transferred control of your program to the Subvi.  You are not at this stage modular.  Your higher level GUI has no control.  You would have to create a stupid system of variables to pass data down to the loop to stop the loop and return to your main VI.  This is not a good design decision.

    Instead of searching for a way to make something broken, you should spend effort trying to understand what you really want to accomplish.  You should also spend some time to learn how to work the event structures.

    This application is small at the moment and it is already on the way to the unreadable.  It's a terrible thing to do for you.  Break these habits now.  variables are only when absolutely necessary.  Using subVIs to accomplish individual tasks, do not run the entire application.  With the structures of the event, use a value of stop button change event for will not be held hostage to the time-out.  If your variable is '2' in it, you're doing something wrong.  It has to be something different between set points, put it in the name.  If you want to make the subVIs, create an icon.

    But first and foremost, invest time in understanding dataflow

  • How to use Labview to control the motorized turntable?

    Hello

    I am new to Labview and do a project that requires the use of labview to control the motorized turntable (XYZ axes). I need to use the Labview programming and have no idea at all. Need help, thank you very much!

    Lei,

    Everything said by others is correct and to do because they describe, at a given time. But I always approached these projects of motion control first by looking at the 'motion controller' (I hope you have a) and 'drivers' (what you need) between the computer/LabVIEW and engines decide what communications will be required for these devices and get an idea of the syntax of command and the language to use with them.  In some cases, the controller and the motor driver are integrated in a single unit. Heck, in some cases, they are even integrated with each engine.

    The motors can be the stepper motors or perhaps servomotors.

    In the case of drivers stepper (devices that provide power electronics to run the engines), many of them are run using only two control signals for digital called 'step' (a pulse train) and 'direction' (high-low level) and if it is possible to generate directly with the PC, it's an order of magnitude easier (if you have not already a library of available motion control to manage the generation of these signals and) the execution of the stepper motors) of having a "Motion Controller" between the PC and the "motor".  Engine drivers step by step usually can be configured that current engine and the size of the stage and sometimes for the other plus-moteur/driver-parameter setting. The configuration of these can be made with jumpers, switches or, for some drivers, with orders ASCII on some sort of port of communication, but in general these are values you download the installer once at the beginning and then never touch again if you do not need generally to have a full time available on their change communication path.

    In the case of the actuator drivers (once again the devices that provide power electronics to run the engines), many use a +/-10V control input analog signal, and, again, while it is possible to generate the signal directly with the PC hardware, a separate axes controller is usually much simpler and more robust.  In the case of a servo system, there is also a signal back to an encoder or resolver attached to the engine to be used by any device will exercise the motion controller in the system.  There are once again associated with the driver configuration parameters (and controller in this case, since it is part of the servo feedback loop) which must be configured at the start but once set up properly, they rarely need to be changed again.

    A movement controller which is separated from the PC often manage several drivers, and can often coordinate a movement between them. It will load the rise up/down of the engine speed as that changes. It will be followed by position of each motor. The servo system, but it will take the feedback signal and use it to adjust the speed/position/torque of the engine. The motion controller also deal with race and home or index of signals is usually on most lines of movement and provide information of the State of the switch to the PC.   To run the motor controller is then just a matter of chaining a set commands of (usually ASCII) to indicate how fast to run and how far to go (and direction) and by sending a command "do it." Then, ask you to keep track of the status of the process and the States of the switches.

    In many cases the builders of controllers of movement are a primitive way to communicate with the controllers of a PC with no additional software other than what they offer. With this you can at least have an idea of how different the controller commands and responses they produce. That you can start to build sequences of commands to perform the actions you want.

    You can then add LabVIEW to the mix and to understand how you can get it to send sequences of similar orders on the paths of communication that are available and respond intelligently to the information it receives in return for motion control.

  • How to use resource element in the xml file in the workflow?

    Good afternoon

    In my workflow, I put a parameter of the attribute with the element of resource type and the value of the attribute is an xml file.

    And I had a problem with xml files in my workflow, I would like to use the information in the xml file to store in the variable...

    Example: in the xml file, I put some information about the number of VLAN ID, and in my script task, I would like to know how is used with myvariable.portgroup [0].spec.vlanId?

    Best regards.

    Like this:

    var myXml = new XML(resource.getContentAsMimeAttachment().content);
    

    You can then access your nodes like this:

    myXml.node.subnode

    Google for e4X to get the syntax complete.

  • How to use a function inside the package?

    Hello

    You will need to convert weight unit of kilogram kips when filling out the weight values into a temporary table. In our database, schema there is a function called convert_units is available.
    How could I use this feature in my package? My package is also resides in the same database schema.

    Here, I pasted the function.
         convert_units
       (
         in_base_unit_id       NUMBER,
         in_conversion_unit_id NUMBER,
         in_value              NUMBER
       ) RETURN NUMBER AS
    CURSOR unit_cur IS
      SELECT *
      FROM units_conversion
      WHERE base_unit_id = in_base_unit_id
        AND conversion_unit_id = in_conversion_unit_id;
    unit_rec unit_cur%ROWTYPE;
    BEGIN
      OPEN unit_cur;
      FETCH unit_cur INTO unit_rec;
      IF unit_cur%NOTFOUND THEN
        unit_rec := NULL;
      END IF;
      CLOSE unit_cur;
      RETURN (in_value+unit_rec.pre_adjust_add)*(unit_rec.rate);
    END convert_units;

    Hello

    It seems that the first two arguments of this function are ID in the units_conversion table. If you know this id = 1234 means kilograms, 9876 means kips, then you can do things like:

    wt_kips := convert_units (1234, 9876, wt_kilograms);
    

    or

    INSERT INTO tmp_table ( ..., weight_val,                               ...)
           VALUES           ( ..., convert_units (1234, 9876, wt_kilograms), ...);
    

    I can't just look at the code which could be any valid ID. I'm not even sure of the order of the arguments. I hope you have instructions on how to use the function.

    If the function is in a package named pk_xyz (a different package to the one in which you call it), add the name of the package, like this

    wt_kips := pk_xyz.convert_units (1234, 9876, wt_kilograms);
    
  • Satellite L550-117 - how to use VGA + HDMI at the same time?

    Hello
    I have a Satellite/Satellite L550 series Satellite L550 - 117.
    Can I use 2 monitors at the same time, using the function key (laptop screen + HDMI, VGA + HDMI, VGA, portable computer), but I would use "laptop screen", "VGA output" + "Output HDMI" at the same time (for example: to play a movie on an HD TV and work on the other two screens).
    I also want to play sounds on computer speakers 'and' an HDMI output, not the speakers for computer 'or' HDMI.

    Is it possible with this notebook and how?

    Thank you

    Hey Buddy,

    Good idea you have unfortunately it of not possible to use 3 monitors at the same time. You can use the internal monitor and an external monitor but not the two external monitors. This is not supported in the graphics card.

    Then, you use internal monitor, VGA connection or internal monitor + HDMI connection. That s what you can do.

  • How to use Labview for measuring the reflectance at different wavelengths automatically

    Hello!

    I'm using labview to communicate with MS257 (a monochromator). If I do not use Labview, I have to use the hand controller (set a wavelength from 300 to 1100 under a grating (a total of four grids).) It takes a lot of time. So I want to use labview to realize the automation. But I don't know how to do, I still can put only a wavelength as a command for MS257, not making any difference.

    Does anyone know how to deal with this?

    Thank you for your time.

    So where are you stuck? It is a fairly common task. Inside of a loop, you can increment the "xxx" and build a table of responses. Look at the Format function in the chain and a knot of shift register/feedback.

  • How to use NIDAQmx to determine the number of lines correlated DIO?

    How can I used NIDAQmx to determine the number of correlated DIO lines that supports a device?  For example, the NI6225 supports 8 correlated digital of/s lines while the NI6229 supports 16 correlated digital of/s lines.  How can I used NIDAQmx to determine the NI6225 supports only 8 lines while the NI6229 supports 16 lines?

    I moved this question from a previous thread.

    Thank you

    Ian

    Here is a code snippet that implements the regular expression detection:

  • How to use windows scan with the new canon printer is connected and the copy and printing, but can't seem to be able to use the technique of scan windows

    How to use windows scan with my new canon MX 922 printer.  It is conncected and copying and printing but can't seem to use the windows scan option, the cannon that which for me is slow thanks

    All in one, type printers (print, copy, scan) usually have a very specific set of instructions during the installation.  If not followed exactly the probability is that the scanner will not work.

    Canon: http://www.usa.canon.com/cusa/consumer

    Download and read the Setup instructions for your model, have you followed the instructions exactly, more precisely when to connect the printer / scanner during installation of the software process?

    J W Stuart: http://www.pagestart.com

  • How to use PS to judge the existence of a file in the directory specified and how to use PS to the directory specified for create to create the file? (javascript)

    1, how to use PS to judge of the existence of a file in the specified directory? (using javascript)

    2, how to use PS to the directory specified for create to create the folder? (using javascript)


    Thank you!

    If (Folder(/*_insert_the_path_here_*/).exists == false) {Folder(/*_insert_the_path_here_*/).create()}

Maybe you are looking for