How remove the line by line by comparing to the first column?

Hello!

I have a problem - I need to remove the line by line, but the problem is, I know that the first COLUMN of the table is a PK.
To retrieve the NAME of the COLUMN that I use:

SELECT column_name, table_name FROM USER_TAB_COLUMNS WHERE column_id = 1 and table_name = c1.tmp_table_name;
But it does not somehow.
Below you can see my script (which has not worked for now):

declare
XXX varchar2 (100);
Start
C1 in (select table_name, tmp_tables tmp_table_name) loop
IMMEDIATE EXECUTION
' SELECT column_name in ' | xxx | "USER_TAB_COLUMNS WHERE column_id = 1 AND table_name = ' |" ' || C1.tmp_table_name | " ' ;
immediate execution
"start."
for c2 in (select * from ' | c1.tmp_table_name | start loop ')
Insert into ' | C1.table_name | "values c2; delete from ' | C1.tmp_table_name | 'where ' | xxx |' = c2.' | xxx |'; exception: when other then null; end; end loop; end;';
end loop;
end;

P.S. The Inserts work perfect. I have a problem with deleting lines that are in c1.table_name, of c1.tmp_table_name (the two tables have the same structure, PK, always), because I have the names of columns different another tables which are PK. (for example: K, ID, NS and so on) Please help me to write the correct script.
For example this will be for the first line recovered as:
Start
C1 in (select table_name, tmp_tables tmp_table_name) loop
immediate execution
«Start for c2 in (select * from ' |)» C1.tmp_table_name | Start loop ')
Insert into ' | C1.table_name | "values c2; delete from ' | C1.tmp_table_name: ' where K = c2. K; exception: when other then null; end; end loop; end;';
end loop;
end;
This script works perfectly. But I have many other tables with different PK - K No.

Solution with the logging of errors table

-- create the error-logging table
CREATE TABLE tbl_MergeErrors (
    Stamp       TIMESTAMP(3),
    TableName   VARCHAR2(30),
    KeyColumn   VARCHAR2(30),
    KeyValue    VARCHAR2(4000),
    ErrorCode   NUMBER(5),
    ErrorMsg    VARCHAR2(4000),

  CONSTRAINT pk_MergeErrors
      PRIMARY KEY (TableName, Stamp)
      USING INDEX
)
/

-- procedure to insert errors
CREATE OR REPLACE
PROCEDURE LogMergeError (pTableName  IN VARCHAR2,
                         pKeyColumn  IN VARCHAR2,
                         pKeyValue   IN VARCHAR2)
IS PRAGMA AUTONOMOUS_TRANSACTION;

    -- you couldn't insert SQLCODE or SQLERRM directly into a table (ORA-00984)
    nSQLCODE   NUMBER(5)      := SQLCODE;
    vcSQLERRM  VARCHAR2(4000) := SQLERRM;

BEGIN
  INSERT INTO tbl_MergeErrors
         (Stamp, TableName, KeyColumn, KeyValue, ErrorCode, ErrorMsg)
      VALUES (SYSTIMESTAMP, RTrim( SubStr( pTableName, 1, 30)),
              RTrim( SubStr( pKeyColumn, 1, 30)), SubStr( pKeyValue, 1, 4000),
              nSQLCODE, vcSQLERRM);
  COMMIT WORK;

-- if an error occured here, then just roll back the autonomous transaction
EXCEPTION
  WHEN OTHERS THEN   ROLLBACK WORK;
END LogMergeError;
/

-- create the tables and insert test-data
CREATE TABLE TMP_TABLES (
    TABLE_NAME       VARCHAR2(200),
    TMP_TABLE_NAME   VARCHAR2(200),
  CONSTRAINT TMP_TABLES_X PRIMARY KEY (TABLE_NAME)
)
/
CREATE TABLE TMP_KL002 (
    K   VARCHAR2(40),
    N   VARCHAR2(200)
)
/
CREATE TABLE TMP_TABLE1 (
    NS   VARCHAR2(40),
    N    VARCHAR2(200)
)
/
CREATE TABLE KL002 (
    K VARCHAR2(40),
    N VARCHAR2(200),
  CONSTRAINT PK_KL002 PRIMARY KEY (K)
)
/
CREATE TABLE TABLE1 (
    NS   VARCHAR2(40),
    N    VARCHAR2(200),
  CONSTRAINT PK_TABLE1 PRIMARY KEY (NS)
)
/ 

INSERT INTO TMP_TABLES (TABLE_NAME, TMP_TABLE_NAME) VALUES ('kl002','tmp_kl002');
INSERT INTO TMP_TABLES (TABLE_NAME, TMP_TABLE_NAME) VALUES ('table1','tmp_table1');
INSERT INTO tmp_KL002 (K, N) VALUES ('00', 'none');
INSERT INTO tmp_KL002 (K, N) VALUES ('07', 'exists');
INSERT INTO tmp_KL002 (K, N) VALUES ('08', 'not assigned');
INSERT INTO tmp_table1 (NS, N) VALUES ('2000', 'basic');
INSERT INTO tmp_table1 (NS, N) VALUES ('3000', 'advanced');
INSERT INTO tmp_table1 (NS, N) VALUES ('4000', 'custom');
COMMIT WORK;

-- to test, if it works correct when primary key values exists before
INSERT INTO KL002 VALUES ('07', 'exists before');
COMMIT WORK;

-- check the data before execution
SELECT * FROM TMP_KL002 ORDER BY K;
SELECT * FROM KL002 ORDER BY K;
SELECT * FROM TMP_TABLE1 ORDER BY NS;
SELECT * FROM TABLE1 ORDER BY NS;

-- empty the error-logging table
TRUNCATE TABLE tbl_MergeErrors DROP STORAGE; 

-- a solution
DECLARE

    PLSQL_BLOCK  CONSTANT VARCHAR2(256) := '
BEGIN
  FOR rec IN (SELECT * FROM <0>) LOOP
    BEGIN
      INSERT INTO <1> VALUES rec;
      DELETE FROM <0> t WHERE (t.<2> = rec.<2>);
    EXCEPTION
      WHEN OTHERS THEN
          LogMergeError( ''<1>'', ''<2>'', rec.<2>);
    END;
  END LOOP;
END;';

BEGIN
  FOR tabcol IN (SELECT t.Tmp_Table_Name, t.Table_Name, c.Column_Name
                 FROM Tmp_Tables t,
                      User_Tab_Columns c
                 WHERE     (c.Table_Name = Upper( t.Tmp_Table_Name))
                       AND (c.Column_ID = 1)
            ) LOOP
    EXECUTE IMMEDIATE Replace( Replace( Replace( PLSQL_BLOCK,
                               '<0>', tabcol.Tmp_Table_Name),
                               '<1>', tabcol.Table_Name),
                               '<2>', tabcol.Column_Name);
  END LOOP;
END;
/ 

-- check the data after execution ...
SELECT * FROM TMP_KL002 ORDER BY K;
SELECT * FROM KL002 ORDER BY K;
SELECT * FROM TMP_TABLE1 ORDER BY NS;
SELECT * FROM TABLE1 ORDER BY NS;

-- ... and also the error-logging table
SELECT * FROM tbl_MergeErrors ORDER BY Stamp, TableName;

-- of couse you must issue an COMMIT (the ROLLBACK is only for testing
ROLLBACK WORK;

-- drop the test-tables
DROP TABLE TABLE1 PURGE;
DROP TABLE KL002 PURGE;
DROP TABLE TMP_TABLE1 PURGE;
DROP TABLE TMP_KL002 PURGE;
DROP TABLE TMP_TABLES PURGE;

-- you shouldn't drop the error-logging table, but I use it to free up my db
DROP TABLE tbl_MergeErrors PURGE;

Greetings, Niels

Tags: Database

Similar Questions

  • Delete in af:table always remove the first line when using ExecuteWithParams

    Hello world

    I got a page with master form and af:table two details related to the master. When I'm trying to remove a line in an af:table of detail, it always removes the first line, any line, I selected before. The table has a single = rowSelection and the line is properly visually selected. I remove the line using a "delete hyperlink" on each line, but I first have to select the line. Delete called a bond (or a method at the bean by removing the current line of the iterator, I tried both, none of them work)

    Links page has an ExecuteWithParams defining the correct ID to edit on the master of iterator and an InvokeExecuteWithParams the renderModel value. If I put the refresh condition zero and I hard-code an ID in the binding variable in the model, I am able to delete the selected line.

    In addition, I don't know if this information is useful, but if I set the iterator to PPR ChangeEventPolicy, when I select a line, there always select the first line.

    Any ideas what I could do wrong? Is this a bug?

    I use JDev 11.1.1.7

    Thank you

    Guillaume

    You can try creating a method in ApplicationModule for filter master records instead of executeWithParams?

    Ashish

  • How better to insert a subarray (line) in a 2D array to the correct index which is based on the value of the first column

    Hello

    I would like to insert a subarray (line) in a 2D to the correct index table. The position is to say the index value depends on the value of the first column of the table 2d.

    As an examlple my 2d array would look like this

    230 50 215 255

    300 60 270 330

    360 20 350 370

    And I would like to insert another line (subarray) with the following values

    320 40 300 340

    This new line should be placed between the second and third rows (this is based on the first column only).

    I tried the threshold 1 d function table by taking an 1Dsub array of my 2d array (first column), then using the first of the new line (320) as the threshold. It sort of work, but it does not work when I start the table (IE there is only 1 row) and it seems to not work properly on other occasions (as explained in the help of Labview).

    Hopefully the explanation is clear enough for any suggestion. Thanks in advance for the help!

    JTRI wrote:

    The idea is I have start with a new table and add these lines in the right order every time that the user sets the values Jack

    Ahh, so try this.

    This will also work with an empty array.

    You want to do with this function it is a Subvi.

    Make the entries 'table' and 'subarray"on the connector, then 'new array' output.

    You can then put this Subvi in a loop with a registry to shift and it will help to add new lines in a sorted order, when they are added.

    That is what you were aiming for?

  • How can I remove the first 3 characters of the files?

    I'm using the bridge for a while and rename the lot

    How can I rename to remove the first 3 characters of the files?

    (these are numbers)

  • How to count the first column automatically.

    Dear all.

    I would like to calculate the first column from 1 to N, when the user clicks on the add a line button.

    Please refer to the following

    -------------------------------------- Code --------------------------------------

    var nNodes is Test_Result1.sub_Product_accessories. Config_Acc.resolveNodes ("Config_Acc [*]"). Length;


    If (nNodes < = 6) {}
    Test_Result1.sub_Product_accessories. Config_Acc.instanceManager.addInstance ();
    }

    AS-IS

    count.JPG

    AS IT WILL BE

    count2.JPG

    Javascript code the Initialize event of the AccNum field:

    this.rawValue = this.parent.index + 1

  • Remove the first 5 blocks in a data stream

    Hi all

    I have a problem to remove the first 5 blocks in the data stream. My sampling rate is 1 s, block size is 1 and the entrance is the module «the ddf file read»

    I use the following modules for an average analysis 30 years running.

    [read the folder]---> [Formule1] -> [set variable] -> [formula2]

    |                 ^

    --> [time]-|

    module parameter

    ======                =========

    delay of 30

    Formula1 ${var_1} + in (0) - in (1)

    the value of variable ${var_1}

    Formula2 in (0) / 30

    This configuration is used for channels 13 and one of these channels is used for purposes of triggering. Due to the nature of the variable defined and read in the underlinedmodules, the trigger sequence is delayed for 2 sec. Since I used the trigger to collect the last returns average of each channel, it is now mixed with 2 sec for the next round.

    My question is: is there a way to reduce say 5 blocks of data from the stream? Please help and have a nice day

    Look at the SEPARATE module in the Group of data reduction.

    It allows you to set up an initial leap, then a current break.

    To do this, you want to jump 5 blocks once, does through go zero blocks... who spends the first five and then release all the data blocks of subsequence.

  • Columns of folder: by default, how can return the first column 'Name' without having to move it manually every time?

    Something's happened awhile and when I create a folder which appears the first column is the column 'Date modified '. By default, how can return the first column 'Name' without having to move it manually every time?

    Hello

    I suggest you to visit these links and check if it helps:

    http://Windows.Microsoft.com/en-us/Windows-Vista/working-with-files-and-folders#section_4

    http://Windows.Microsoft.com/en-us/Windows-Vista/folders-frequently-asked-questions

    It will be useful.

  • I have a text field. I need to remove the first 8 characters of the field, leaving the remaining characters. Help?

    I have a text field. I need to remove the first 8 characters of the field, leaving the remaining characters. Help?

    Example:

    Text Example.JPG

    I need to remove these numbers PXXXXXX. Leaving the other characters.

    Thank you

    Bob

    A normal 'substring' is what you're looking for.

    For a formula of the column in the response criteria tab:

    SUBSTRING ("YourPresTable". "YourPresColumn" OF 9)

    To make the RPD and the workload of shipping off the coast to the database rather than forcing the work on the server of the OBI or presentation:

    Substring ("01 - Sample App Data (ORCL)" ".." ") "" "BISAMPLE '." " D10 product (dynamic table)". ("' Prod_Dsc ' 9)

  • How does the first (v1.0) account in the light of the cost of the effects?

    I am doing a report on the history of after effects and I need to know how only the first account in the light of the cost of the effects.

    Any info will be helpful. must not be v1.0, all old version will do!

    Also after I know how much it costs, I need a way to calculate how much it is in today's silver.

    After effects 2.0 sold for $1 995 in 1993 dollars.

    I see if I can find the 1.0 price information.

    Here is some information of history, including an explanation for our first few code names:

    http://blogs.Adobe.com/toddkopriva/2010/05/birthplace-of-after-effects.html

  • How the first column heading must be repeated if the table breaks across pages

    Hi Experts,

    RTF model, header row in the table automatically repeat if record go to the next page. in similar fasion, I want to repeat the first column heading, table breaks on any page.

    Please help reslove this issue...

    Hello

    You can add the tag to the first column and the first row of the table. It will repeat 1st column when table to break across pages.

    Here 1 means, repeat first column when table to break across pages.

    I hope this will solve your problem.

    P.S. If you provide 2, this means, repeat for column 2 of the table through the pages

    Please check the issue as resolved, if that answers your question. Thank you

  • Read the data in the first column selected in a multicolumn listbox

    When a line is selected in a ListBox multicolumn (1 point), how can I go on reading the data in the first column?

    The listbox multicolumn itself is the digital picture data type. If you have allowed only 1 point selection and selection mode select any row, it returns the line number. The property node 'Element names' to return a table 2d-chains of the elements in your Inbox. The index of the row to the value of the listbox and column 0. See code attached.

  • adding multiple files *.csv while retaining the original file name in the first column

    Hi guys Cela made some time.

    I am trying to add several *.csv files while retaining the original file name in the first column, the real data set is about 40 cases.

    file a.csv contains:
    1, line an a.csv
    2, line 2 in a.csv

    file b.csv contains:
    1, line a b.csv
    2, line 2 in b.csv

    result output.csv is:

    I would like this:
    a.csv, 1, the line in a.csv
    a.csv, 2, line 2 in a.csv
    b.csv, 1, the line in b.csv
    b.csv, 2, line 2 in b.csv

    Any suggestions to speed up my hobbling attempts would be aprieciated

    Thank you

    -SS

    What you could do is given in attachment.

    Started with 2 files:

    a.csv

    copy of a.csv

    Both with data:

  • Photosmart Premium C309a print only the first column of the Excel worksheet

    Photosmart Premium C309a print only the first column of the Excel worksheet

    Hello

    I see you are having a problem printing to an Excel document.

    I suggest you install a default substitution such as the jet pilot desktop 990 c driver.

    You can follow this thread of Shane, who will show you how.

    After that, you can try printing again from Excel.

  • Change the first column seen in the datagrid by code?

    Hello

    Is anyway to change the first column in a DataGrid using action script? I mean, for example, think I have 10 columns, only 3 are seen (0,1,2). I just want to have a button to change the first column view. Thus, for example, if I press it, 1.2, and third column will be those that is displayed in the datagrid control. I need to emulate the same thing that the scroll bar when you drag in a datagrid, but instead of letting the scroll bar does that automatically, I want to "scroll" datagrid manually.

    Moreover, is anyway through datagrid children and hide the scrollbar manually?

    Thanks in advance,

    Aron.

    horizontalScrollPolicy

  • checkbox in the first column of the reports

    Hi all
    I have a classic report where the first column is checkbox display.
    Instead of the box, can I have the radio button.
    If it is not possible,
    When a check box is selected, the others should not get selected.


    Please guide me to get it used.

    Thanks in advance
    Good bye
    Sonny_starck

    Hello Sonny_starck,

    Yes you can. Try this:

    SELECT APEX_ITEM.RADIOGROUP (1,deptno,'20',dname) dt, deptno, dname
    FROM   dept
    ORDER  BY 1
    

    Kind regards
    Roel

    http://roelhartman.blogspot.com/
    http://www.bloggingaboutoracle.org/
    http://www.Logica.com/

Maybe you are looking for

  • Satellite C870 - 1 H 2 and support for virtualization

    Hello I am (was to) buy a Toshiba Satellite C870 - 1 H 2 (from amazon.co.uk).The specifications look ideal for me, but at the last moment, I thought to check if VT - x extensions are supported. I know that the processor supports extensions (as detail

  • HP Pavilion dv6 QG478PA #ACJ: noisy fan &amp; heating

    my laptop fan clocked to break speed with noise and also heating. I updated the BIOS of the processor etc. and also HP feel cool, adjusting the start-up etc but not effective so far. Especially when I Browse (Chrome, Firefox, Opera) internet also upd

  • Windows updates errors

    HelloI contacted dell on the no funtion of the updates of windows and a guy had a problem because when it restored her computer, she restored to the original version of the defender and updated and so did not work. I did the same and Dell sent me to

  • GaugeField Query

    How can I change the default color of gaugeField. By default, gaugeField color is blue. How can I make this yellow color. ? If I replace the paint gaugeField like this method: protected void paint (Graphics graphics) { } The gaugeField cannot be seen

  • From photography panoramic photo fusion Lightroom CC

    Hi allWe hope to get some more tips here experianced users. I'm just poking a 360 panoramic photograph in lightroom. Project consists of 36 raw images, 3 x 12 rows each. Each gross overlap and no mistake here. Pictures taken with a drone, with lychee