How to generate a sql script that is based on the structure of the table

I want to generate a sql script that is based on a table structure.

For example:

If the table is:

CID id c_value
-----------------------------
1 1 zz
2 1 yy
3 2 zz
4 2 xx
5 3 ss
6 tt 3


The expected output is:

WITH
CHILD_tab like)
SELECT 1 cid, 1 id, 'zz' c_value of all the double union
SELECT 2 cid, 1 id, 'yy' c_value of all the double union
SELECT 3 cid, 2 id, 'zz' c_value of all the double union
SELECT 4 cid, 2 id, 'xx' c_value of all the double union
SELECT 5 cid, 3 id, 'ss' c_value of all the double union
SELECT 6 cid, 3 id, 'tt' double c_value)


11.1.0.7.0 release

I do a lot of XML these days (maybe too well) so here is a solution involving XQuery.
We pass a query string and it generates a CLOB containing the WITH clause:

SELECT DBMS_XMLGEN.Convert(
XMLQuery(
q'[concat(
"WITH t AS (
",
string-join(
 for $i in /ROWSET/ROW
 return concat( " SELECT ",
                string-join($i/*/concat("'",ora:replace(text(),"'","''"),"' ",local-name()),", "),
                " FROM dual" ),
" UNION ALL
"
),
"
)")]'
passing dbms_xmlgen.getXMLType('SELECT * FROM scott.emp')
returning content
).getClobVal(), 1) AS WITH_CLAUSE
FROM dual;
WITH_CLAUSE
--------------------------------------------------------------------------------
WITH t AS (
 SELECT '7369' EMPNO, 'SMITH' ENAME, 'CLERK' JOB, '7902' MGR, '17/12/80' HIREDATE, '800' SAL, '20' DEPTNO FROM dual UNION ALL
 SELECT '7499' EMPNO, 'ALLEN' ENAME, 'SALESMAN' JOB, '7698' MGR, '20/02/81' HIREDATE, '1600' SAL, '300' COMM, '30' DEPTNO FROM dual UNION ALL
 SELECT '7521' EMPNO, 'WARD' ENAME, 'SALESMAN' JOB, '7698' MGR, '22/02/81' HIREDATE, '1250' SAL, '500' COMM, '30' DEPTNO FROM dual UNION ALL
 SELECT '7566' EMPNO, 'JONES' ENAME, 'MANAGER' JOB, '7839' MGR, '02/04/81' HIREDATE, '2975' SAL, '20' DEPTNO FROM dual UNION ALL
 SELECT '7654' EMPNO, 'MARTIN' ENAME, 'SALESMAN' JOB, '7698' MGR, '28/09/81' HIREDATE, '1250' SAL, '1400' COMM, '30' DEPTNO FROM dual UNION ALL
 SELECT '7698' EMPNO, 'BLAKE' ENAME, 'MANAGER' JOB, '7839' MGR, '01/05/81' HIREDATE, '2850' SAL, '30' DEPTNO FROM dual UNION ALL
 SELECT '7782' EMPNO, 'CLARK' ENAME, 'MANAGER' JOB, '7839' MGR, '09/06/81' HIREDATE, '2450' SAL, '10' DEPTNO FROM dual UNION ALL
 SELECT '7788' EMPNO, 'SCOTT' ENAME, 'ANALYST' JOB, '7566' MGR, '19/04/87' HIREDATE, '3000' SAL, '20' DEPTNO FROM dual UNION ALL
 SELECT '7839' EMPNO, 'KING' ENAME, 'PRESIDENT' JOB, '17/11/81' HIREDATE, '5000' SAL, '10' DEPTNO FROM dual UNION ALL
 SELECT '7844' EMPNO, 'TURNER' ENAME, 'SALESMAN' JOB, '7698' MGR, '08/09/81' HIREDATE, '1500' SAL, '0' COMM, '30' DEPTNO FROM dual UNION ALL
 SELECT '7876' EMPNO, 'ADAMS' ENAME, 'CLERK' JOB, '7788' MGR, '23/05/87' HIREDATE, '1100' SAL, '20' DEPTNO FROM dual UNION ALL
 SELECT '7900' EMPNO, 'JAMES' ENAME, 'CLERK' JOB, '7698' MGR, '03/12/81' HIREDATE, '950' SAL, '30' DEPTNO FROM dual UNION ALL
 SELECT '7902' EMPNO, 'FORD' ENAME, 'ANALYST' JOB, '7566' MGR, '03/12/81' HIREDATE, '3000' SAL, '20' DEPTNO FROM dual UNION ALL
 SELECT '7934' EMPNO, 'MILLER' ENAME, 'CLERK' JOB, '7782' MGR, '23/01/82' HIREDATE, '1300' SAL, '10' DEPTNO FROM dual
)

It may be useful for small datasets only because we reached quickly ORA-01706.

Tags: Database

Similar Questions

  • How do I create a form that redirects based on the entry of the form?

    What I'm trying to do, is to have a form with a single entry. Lets call it "ID".

    There are several pages that form can lead to the ID according to user input.

    All extensions page ends with the code. For example, www.page.com/0001.html.

    So I want the user to "0001" then submit. The user will be directed to the page www.page.com/0001 if they put one is not valid user ID is directed to the "Not found" page

    Any help would be much apreciated! Thank you.

    on form.php put a form like this:


    on redirect.php put this:

    <>

    If (isset($_POST['id'])) {}

    Header ('Location: http://yoursite.com/'. $_POST['id']. 'html');

    "exit";

    } else {}

    Header ('Location: http://yoursite.com/form.php');

    "exit";

    }

    ?>

    best,

    Shocker

  • When I click on a file, it brings up a search screen. How can I change back so that it opens just the file?

    When I click on a file, it brings up a search screen.  How can I change back so that it opens just the file?

    Open the box and run by pressing the Windows logo and the R keys together then type;

    regsvr32 /i shell32.dll click OK

  • How to display a hidden shortcut that is pinned to the Start Menu?

    HIDDEN PIN TO THE MENU SHORTCUT START

    How to display a hidden shortcut that is pinned to the Start Menu? This problem occurs very simply but does not quite simply. I've pinned a shortcut to the Start Menu before you uncheck the hidden attribute (knew I had) causing the shortcut to be recorded without being visible. When I tried to pin it again I was alerted to his presence by the fact that the second pinning made an extension of title (2). When I tried to change the title, I received the message that a link of this title was already present. I don't want to stay as a situation lasting this is why I need instructions on how to change it. I could just change the title of the second pinning something close but different but then I would have two shortcuts to the same program pinned on my Start Menu, without being able to be removed. I just * because if I can tell. I could uninstall the program and then run another program looking for broken shortcuts to 'fix' and then (I guess that means deletion when they can't be 'fixed') but I would like to avoid the extra steps for pedestrians to the advantage of learning a new technique of Windows if such jurisdiction exists. Could use this help as soon as POSSIBLE. Thank you.

    C:\Users\\AppData\Roaming\Microsoft\Internet Explorer\Quick Launch\User Pinned\StartMenu

    Just tested myself and my shortcut is there.

  • How to create a dependent list of values based on the relationship master detail

    Hello

    I'm on jdev 11.1.2.4.

    1. I have a table main 'Reception' with the columns receipt_id and student_id (ReceiptVO)

    2. as well as an array of details 'Receipt_Detail' with the columns receipt_id, course_id (ReceiptDetailVO)

    (There are some othere columns in the two tables, but I've only mentioned those which is relevant to this example.)

    3. the two tables are related with receipt_id.

    I want to create a dependent class LOV that filter courses related to the student selected in the main table. (There is a student_course table that holds student_id, relationship of course_id, StudentCourseVO)

    to do this, I have

    1 created VO based on the student_course table

    2 Add a view that filters based on the id of the student

    3 assigned this VO to receipt_detail tables course_id

    It comes

    in the accessor receipt_details VO view, I do not show the ability to select the student_id of the main table (IE ReceiptVO). It only shows the columns in the ReceiptDetailVO.

    Please can you help me?

    Thank you

    What you need in your ReceiptDetailVO is the StudentId of master ReceiptVO attribute.

    To do this, you can:

    1. for your ViewLink (between ReceiptVO and ReceiptDetailVO), you must generate the Source accessor:

    In this way, in the VoRowImpl Java to ReceiptDetailVO class, you will get a way to access master ReceiptVO line (you can find a new method with the name that you gave the name of the accessor in the top of the dialog box)

    2. then, in the ReceiptDetailVO, create Transient attribute, say, MasterStudentId. Access Java's VORowImpl class, within the get for this new attribute accessor method, use the previous step name accessor method to get the line main and finally master StudentId.

    3. the rest is easy - use this new attribute transitional to filter LOV about the student teacher ID...

  • The columns appear in the data store that are not in the table

    Hi guru,.

    I have a table that I built in Oracle and added to ODI by refactoring it, but when I add it as a source to my interface, it contains columns that are not physically in the Oracle table that I can look into a TOAD. I dropped the prior of the table deleted-> interface-> reverse engineering table but I get an additional column. Y at - it an explanation for that? I have the table listed in a stored procedure - should I delete this step as well before reverse engineering, or is something else.

    Thanks in advance.

    Mike

    Yes. There is a possible explanation. How do you are engineering it? If you use Oracle RKM, you may be using a customized version of this KM which adds a column of your table name...

  • How to format the output of a .sql script that has select statements.

    Hello

    I have a .sql script which is having several select statements. Each Select statement is to have hundreds of Table columns.

    When we run the .sql script, we are unable to read the output.

    Please let know us the commands that we include for better readable output format.

    You work with the lin standard set 80.
    Increase this setting to set lin 3000 or more.

  • How to create an effective script that mass deletes snapshots

    our company is currently using a product that drives the operating system and software updates for our virtual machines and takes a snapshot before pushing updates in case something breaks. the problem is that the same software does not remove the pictures until next update button and you're just too long we wait with snapshots lying around. what I did to remedy this situation is I built the below script and configured it as a task.

    # Add the snap-snap powercli in
    Add-PSSnapin VMware.VimAutomation.Core

    # Connect to an instance of vCenter
    $strviserver = "vCenterServer".
    SE connect-VIServer $strviserver

    # Get all of the snapshots for each virtual machine in the vcenter, then delete them
    Get - VM | Get-Snapshot - name "updates" | Remove-Snapshot - confirm: $false

    # Remove the snap-snap powercli in
    Remove-PSSnapin VMware.VimAutomation.Core

    It works, but it is not nearly as effective as I wish it were. Essentially, the script currently removes a snapshot at a time right now. I want so he can find any snapshot on this server vcenter by the same specified name and submit the task deletion-Snapshot for each snapshot at the same time. This should cause a lot of tasks to try and tail. The effective result would be that vCenter will treat all instant destruction because it has resources to do in a given time and will address the queue there resources.  If someone can tell me how to do this or something better, I would really appreciate it! Thanks in advance!

    You can consider using the RunAsync switch on the cmdlet Remove-Snapshot .

    In this way, that the script will continue while delete the snapshot is still running.

    Note that there is obviously that a limit on how many parallel snapshot removes that may occur due to the resources available.

  • How to execute a SQL statement that is stored within a SQL Table

    Hello

    If someone please help me with the following problem I would be eternally grateful

    I have a SQL statement that is stored inside a certain SQL table, I want to use this SQL statement inside my PL/SQL procedure.

    Thought a simple solution was to get the SQL statement in a table, and then run it, but how could I do so exactly with PL/SQL? I only started to play with PL/SQL in the last days.

    Thanks in advance!

    This is what it looks like more or less:

    Display result for:

    ---------------------

    SELECT TRIM (OBJ_VALU_TXT)

    OF OBJ_VALU_DOC

    WHERE TYPE_OBJET = 'FLD '.

    AND OBJECT_CODE = 15443

    AND OBJ_VALU_CD = 'CAB '.

    ORDER BYDOC_SEQ_NO

    00001

    ------------------------------------------------------------------

    SELECT

    VALUE (MAX (RECEIPT_NO) + 1,: OUT-COMP-FACTOR)

    THE FRONT DESK

    WHERE (RECEIPT_NO BETWEEN

    (: OUT-COMP-FACTOR AND: OUT TO A NUMBER) OR

    (RECEIPT_NO >: OUT-COMP-FACTOR AND)

    (: OUT-TO-NUMBER = 0)

    Here's a demo of your condition.

    create table t (col1 varchar2 (200));

    table created

    insert into values t ('select * from double ');

    1 row inserted

    declare
    v_col varchar2 (200);
    v_val varchar2 (200);
    Start

    Select col1 in t v_col;

    run immediately v_col in v_val;
    dbms_output.put_line (v_val);
    end;

    -----
    X

    Use in the clause, you can use as many variables as needed. But the basic approach remains the same.

    But storage in DB SQL is not an efficient design.

    Ishan

  • Transformation of Scripts that run only on the selected Tables

    I'm relatively new to Data Modeler, but found almost everything to be very intuitive. I'm now doing use custom transformation scripts to dynamically add custom DDL elements. I wrote a transformation script that adds a sequence at the end of my DDL trigger successfully, and I'm happy with the results. The problem I run into is that we have two different categories of tables that require slightly different sequence triggers, but as my model is set to the level of database, my transformation scripts are applied to all arrays regardless they come what schema. Do you have any suggestions on how to limit the tables which proceeds by a transformation script? I thought to add if statements to check what schema tables are coming from, but if I have a situation where I have two different tables that require two different processing scripts, but they are in the same pattern, how can that I manage that? Ideally, I'd like to just pick a table and apply a transformation script for this table individually. Is this possible?

    I work in a relational model with Data Modeler 4.0.

    to dynamically add items customized to DDL

    you need to check the custom of DDL in DM 4.1 features - "tools > rules and design transformations > Table DDL transformations"-DM 4.1 comes with example to generate tables of the newspaper and associated trigger.

    . I wrote successfully in a transformation script that adds a sequence at the end of my DDL trigger

    Data Modeler can generate the sequence and relaxation for you - you need to define the column as 'auto increment' / identity and set in preferences, you want "trigger" put in place. 4.1 DM added support for z/OS, DB2 LUW DB2 and MS SQL Server 2012.

    Do you have any suggestions on how to limit the tables which proceeds by a transformation script? I thought to add if the instructions to check the schema, the tables are coming, but if I have a situation where I have two different tables that require two different processing scripts, but they are in the same schema

    Well you need to sort it out on yourself - you can use classification types, dynamic properties user defined properties (in DM 4.1) or put paintings in various subviews or use the search feature (4.0.3 DM / DM 4.1 comes for example how to use the search results to create subview).

    Ideally, I'd like to just pick a table and apply a transformation script for this table individually. Is this possible?

    Here is an example of how get selected tables:

    appv = model.getAppView();
    dpv = appv.getCurrentDPV();
    //check there is a diagram selected and it belongs to the same model
    if(dpv!=null && dpv.getDesignPart() == model){
     tvs = dpv.getSelectedTopViews();
     for(var i=0;i
    

    Philippe

  • What SQL script to use to configure the default TestStand tables?

    Hello

    I'm setting up a small test application and want to record the results of the test to a SQL Server database. Page in preparation for the implementation of logging database mentions the scripts I use to set my database. In my case, I found 3 table creation scripts:

    • SQL Server create insert generic result Tables.sql
    • SQL Server create Recordset result generic Tables.sql
    • SQL Server Create Tables.sql stored Proc result

    What are the differences between them, and which one should I use?

    Thank you!

    In the options dialog of database--> go to the patterns tab and click on each of the options (generic recordset, insert generics, etc.). In the comments section, you will find the difference.

    You can use one of them but to ensure that any script that you ran the DB tables same option is selected in the DB options.

    I hope this helps.

  • SQL that are based on the values of the corresponding fields.

    Hi my name is Hawk.

    I'm new to the Forum, and I wanted to just introduce myself and say hi.

    I have a daunting task to summarize values and filling in a spreadsheet with columns. I have 600 lines to go, and I was wondering if

    someone knows a way in sql to sum (a body of numbers) based on the existence of a value being the same on another field of varchar2.

    For example, here is an example of the data table that I have.

    CREATE TABLE EXAMPLE

    ("FIRSTNAME", VARCHAR2 (20 BYTE),

    VARCHAR2 (20 BYTE) "LASTNAME."

    NUMBER OF "SUMVALUE."

    NUMBER OF "ROW_ID".

    );

    Insert into SAMPLE (FIRSTNAME, LASTNAME, SUMVALUE, ROW_ID) values ('Charlie', 'chocolate factory', 100, 1);

    Insert into SAMPLE values (FIRSTNAME, LASTNAME, SUMVALUE, ROW_ID) ('Johnny', 'Desire', 200, 2);

    Insert into SAMPLE (FIRSTNAME, LASTNAME, SUMVALUE, ROW_ID) values ('P', 'Frankenstein', 300, 3);

    Insert into SAMPLE (FIRSTNAME, LASTNAME, SUMVALUE, ROW_ID) values ("Charlie", "chocolate factory", 400, 4);

    Insert into SAMPLE (FIRSTNAME, LASTNAME, SUMVALUE, ROW_ID) values ("Charlie", "chocolate factory", 500, 5);

    and Yes, I have 600 lines having to ask and things to the top score on manually, it's just bs data...

    So what I do is the following.

    Write some sql that will total the number field (sumvalue) based on data with firstname = lastname = chocolate factory and Charlie.

    It seems so simple, but still I can not understand how do.

    Is that what I'm trying to make sense?  Also, I hope this is the right place to post this. I looked on this before I posted to make sure.

    Hawk.

    Hi, Hawk,

    Here's one way:

    SELECT SUM (sumvalue) AS total_sumvalue

    SAMPLE

    WHERE firstname = 'Charlie '.

    AND lastname = 'chocolate factory '.

    ;

    Welcome to the forum!

    There are those who use the forum for years and have not yet learned to post CREATES instructions TABLE and INSERT for their sample data, as you did.  Keep up the good work!

    You should also post the exact results you want from the data provided.  For example

    TOTAL_SUMVALUE

    --------------

    1000

    (this is what produces the query I posted).  As you can see, without that David and I came with 2 different interpretations of what you want and maybe aren't exactly what you need.

  • When I click on a link that I searched in a search engine a new faucet is opened. How can I change it so that it remains in the same tab, in that I tried

    When I click on the link in a search engine. allows you to use Google for instance and say that I find the camden wyoming fire company. When I click on the link to the website a new tab opens with the web site. . How can I change it so that it uses the same tap in that I tried

    Hello to bratman0, by default google results should already open in the same tab, if they don't please ensure that you have not set to open a new window browser for them within the parameters of google: https://support.google.com/websearch/answer/425

  • How can I downloaded an app that is not in the App Store

    Different is an application that I found on the internet that is not in the App Store. But whenever I try to download... .it does not work... How can I download this app

    You do not have.  Cannot install iOS Apps devices from anywhere, but the App store.   If it is not in the App Store, the app is very probably not designed for iOS and would not be able to be run in any case.

  • How to remove a domain controller that was removed from the Internet works but not demoted

    How to remove a domain controller that has been remove from the Internet works but not demoted

    got two domain controllers and been deleted but not retrograde

    can help pls

    Hello

    Your Windows is better suited for the directory on TechNet Services. You can follow the link to ask your question: http://social.technet.microsoft.com/Forums/en-us/winserverDS/threads

    I hope this helps.

Maybe you are looking for

  • Where can I find tips on how to put a lot of my documents on my desk in shirts?

    Where can I find tips on how to place the documents saved on my desktop in folders of files?

  • Does not save on Mybook after waking up

    My Macbook Pro recognizes my WD my book for Mac external hard drive when booting, and I can make manual backups.  However, after the computer wakes from sleep mode the backup fails and I should use WD Unlocker in the Finder menu to unlock the drive a

  • Is Nsurlsessiond activity safe?

    Hello, I have a Macbook Pro. I have trouble with the nsurlsessiond activity that makes my connection delay. nsurlsessiond is to continue, you are trying to download data to: Host names S3-AP-Southeast-1-w-a.amazonaws.com AP - sin - 00001.s3.amazonaws

  • Cannot repair after Win32/Olmaric.AYA

    Have been infected with the Olmaric virus and that you want to repair the boot sector, but although I can boot windows, when I boot from a Windows CD, the operating system is not found. It is a Win7 64 bit operating system and I have the same "load d

  • Network with Airport Extreme problems connected to the Linksys switch

    I have a wired network in my house.  It has the following configuration: Modem base station Airport Extreme for Linksys switch and then all wired connections.  It is in a wiring closet in a room upstairs.  I have 2 other extremes from the airport, to