Deleting from multiple tables where few tables have same column name
Hi,
I am new to PL/SQL and need some help. I need to delete data older then X years from some 35 odd tables in my schema and out of those tables 25 tables have same column name on which i can have my "where" clause and rest 10 table have different table names. I am doing something like this :
declare
table_list UTL_FILE.FILE_TYPE;
string_line VARCHAR2(1000);
tables_count number:=0;
table_name VARCHAR2(400);
column_name VARCHAR2(400);
BEGIN
table_list := UTL_FILE.FOPEN('ORALOAD','test7.txt','R');
DBMS_OUTPUT.PUT_LINE(table_list);
LOOP
UTL_FILE.GET_LINE(table_list,string_line);
table_name := substr(string_line,1, instr(string_line,'|')-1);
column_name := substr(string_line, instr(string_line,'|')+1);
DBMS_OUTPUT.PUT_LINE(table_name);
DBMS_OUTPUT.PUT_LINE(column_name);
IF column_name = 'SUBMISSION_TIME' THEN
delete from :table_name where to_date(:column_name)<(sysdate-(365*7));
ELSE
delete from || table_name || where ( || to_date(column_name) || ) <(sysdate-(365*7));
END IF;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(table_list);
DBMS_OUTPUT.PUT_LINE('Number of Tables processed is : ' || tables_count);
UTL_FILE.FCLOSE(table_list);
END;
WHERE the text file "text7.txt" contains list of table name and column names separated by a pipe line. But when I execute the above proc it gives error "invalid table name".
Can something like this be done or is there any other way to execute this task of deletion from 35 tables.
Thanks.
Thanks for replies. I don't know what I am doing wrong but still not getting this damn thing work...This is the proc i am running now :
declare
table_list UTL_FILE.FILE_TYPE;
string_line VARCHAR2(1000);
tables_count number:=0;
table_name VARCHAR2(4000);
column_name VARCHAR2(4000);
code_text VARCHAR2(2000);
BEGIN
table_list := UTL_FILE.FOPEN('ORALOAD','test7.txt','R');
LOOP
UTL_FILE.GET_LINE(table_list,string_line);
table_name := substr(string_line,1, instr(string_line,'|')-1);
column_name := substr(string_line, instr(string_line,'|')+1);
IF column_name = 'SUBMISSION_TIME' THEN
DBMS_OUTPUT.PUT_LINE('do nothing');
ELSE
code_text:= 'begin delete from'|| (table_name) ||'where to_date' || (column_name) || '<(sysdate-(365*7))';
Execute Immediate code_text;
END IF;
END LOOP;
EXCEPTION
WHEN NO_DATA_FOUND THEN
UTL_FILE.FCLOSE(table_list);
DBMS_OUTPUT.PUT_LINE('Number of Tables processed is : ' || tables_count);
UTL_FILE.FCLOSE(table_list);
END;
But it gives following error :
" ORA-06550: line 1, column 51:
PL/SQL: ORA-00933: SQL command not properly ended
ORA-06550: line 1, column 7:
PL/SQL: SQL Statement ignored
ORA-06550: line 1, column 68:
PLS-00103: Encountered the symbol "end-of-file" when expecting one of the following:
ORA-06512: at line 22 "
Similar Messages
-
Create Trigger to Delete from multiple tables
Hello:
I'm trying to write a trigger which will allow me to delete from multiple records. I have two tables where the record for the same client_id needs to be deleted.
Is it possible to do this? I started writing some code and this is what I have so far:
create or replace trigger app_t1
before delete on <table1> ?? -
for each row
begin
delete from client where clientid = :new.clientid;
delete from key where pk = :newclientid;
end;I'm stuck on the line where I have "before delete on" where I'm supposed to provide a table name. I can only use one table and I need to delete from two.
This trigger is supposed to be used within APEX. In APEX, fields are designated as :P1_clientid where P1 references page 1 of the application. Yet, :P1_clientid is set to the field clientid in the table.
So when I write my trigger, I'm not sure how I'm supposed to set my variables.
Can someone help?
I'm also going to post this into the APEX forum.
Thanks.It's not clear to me if you are just trying to keep two tables in syn or whether you are trying to achieve something else.
A couple of points though:
- In delete database triggers the :new attributes are NULL. You probably mean to use the :old attributes.
- Is there some relationship between the two tables? If so, setting the foreign key to CASCADE DELETE might do the trick for you.
- Another option - better than a database trigger in my opinion - is to just code a procedure that deletes from both tables and call that one from APEX.
Regards,
Rob. -
Display Data from multiple models in one table
Hi Experts,
Is it possible to display data from multiple models in one table smltnsly.
I have created a table dynamically.Now I would like to display data from multiple models... If this possible,can anyone give me a lead as to how to do it..
Regards
SUHi
Your Model Nodes be
Model1
---Output_Model1
Attrib1
Attrib2
Model2
---Output_model2
Attrib1
Attrib2
and the value node is
ValueNode
---Attrib1
---Attrib2
Now the coding.
int size;
IPrivate<ViewName>.IOutput_mode1Node node1 = wdContext.nodeOuptut_Model1();
IPrivate<ViewName>.IValueNodeElement elem;
size = node1.size();
for(int i=0; i<size; i++)
elem = wdContext.createValueNodeElement();
elem.setAttrib1( node1.getOutput_Model1ElementAt(i).getAttrib1() );
elem.setAttrib2( node1.getOutput_Model1ElementAt(i).getAttrib2();
wdContext.nodeValueNode().addElement( elem );
similar code for Model Node 2
Regards
Abhimanyu L -
How to get the data from multiple nodes to one table
Hi All,
How to get the data from multiple nodes to one table.examples nodes are like A B C D E relation also maintained
Regards,
IndraHI Indra,
From Node A, get the values of the attributes as
lo_NodeA->GET_STATIC_ATTRIBUTES( IMPORTING STATIC_ATTRIBUTES = ls_attributesA ).
Similarily get all the node values from B, C, D and E.
Finally append all your ls records to the table.
Hope you are clear.
BR,
RAM. -
JOIN 2 tables that have same column ?
I need to learn how to join two tables that both have the same column name:
tbl1 - idskey
tbl2 - idskey
the idskey column holds a id_number
When I do the JOIN I would like to make sure that only Distinct records are joined from both tables and that any duplicates are removed in the final join. So if:
Tbl1 has a idskey of: 12345
and
Tbl2 has a idskey of: 12345
In the final JOIN I want to remove one of those duplicates.
I actually need to join 3 tables that have the same linking column names for the join, but if I learn how to do this correctly on 2, that will be a start.
10g for db, thanks!Hi,
SELECT DISTINCT and GROUP BY are the most common ways to get unique results from non-unique keys. Exactly how you use them depends on exactly what you want to do.
SELECT DISTINCT guarantees that no two rows in the result set, conisdering all columns, will be identical.
GROUP BY produces one row from a set of rows that have a common feature. The values on that row may be a composite of values from various rows in that set (e.g., an average).
Please post a small, specific example. For instance:
"I have two rows in tbl1 ...
and these fhtee rows in tbl2 ...
Notice how there is one row with idskey=12345 in tbl1 but two such rows in tbl2.
How can I get theses results ...
where only one row has idskey=12345?" -
Update SAME column name in two tables from ONE query
Dear All Seniors
Please tell me is it possible to update a same column name in two tables.
I have two tables in same schema
(1)table name
pem.igp_parent
column name
igp_no.
igp_type
(2)table name
pem.igp_child
column name
igp_no.
igp_type
i want to update igp_no column in one query please tell me how it would be possible.
thanks
yassenYou want to update the data from what to what? Where is the new data coming from?
If you are trying to put the same data in two different tables, that strongly implies that you have a normalization problem that needs to be addressed.
Why do you want a single query rather than updating each table in turn? Can you join the two target tables to produce a key-preserved view?
Justin -
Same column name from different table
i have a sql query as like this : "SELECT * FROM TABLE1,TABLE2". i use oracle. both TABLE1 and TABLE2 have the same column named 'COLUMN1'. while i get rows how i know the value of COLUMN1 from which table (TABLE1 or TABLE2).
sample code snippet is above. do u help me!
while (rs.next())
value1 = rs.getString("COLUMN1");
// is value1's value from table1 or table2. how do i know this?
// i try value1 = rs.getString("TABLE1.COLUMN1"); but it doesn't work :(
....I case you don't know what an alias is, it would look something like this:
SELECT a.COLUMN1 as FirstColumn1, b.COLUMN1 as SecondColumn1 FROM FirstTable a, SecondTable b
Notice that in the FROM clause we've appended a short name for each table. You're not limited to one character, but I try to keep it simple. Now we can refer to the tables as a and b.
Because I did that I have to refer to any ambiguous columns (although it's good practice to refer to ALL columns) using the table name prefix and a period. This tells the driver which "COLUMN1" I want. Then we include as AS clause which allows us to tell the driver what we want that column name to be when it's returned to us. This is specially usefule when I have two columns in two separate tables with the same name (as you have here) or if I'm calculating data (i.e. (a.QTY * b.PRICE) as UnitPrice) that doesn't have a column name, so here I can give it one.
It's a little weird at first since you use the alias names in the select before you actually define them in the FROM clause, but you'll get use to it.
Now you retrieve FirstColumn1 and SecondColumn1 from your ResultSet, not Column1.
HTH. -
I add all photos taken on camera roll to individual albums. However, once a photo is deleted from camera roll, it's also deleted from the albums where it also resides. Is there a way to delete or clear out the camera roll but keep them in albums?
See this discussion...
https://discussions.apple.com/message/20971818#20971818
From the More Like This section on the right -
Can emails be bulk deleted from Iphone 4s or does each have to be marked individually then deleted. Trash can be bulk deleted.
Each has to be deleted.
But it can go faster if the iPhone is held in the horizontal position, then on the left side listing tap Edit in the upper raight bar of that pane, and just tap each email to be deleted, then when you have a batch to delete, tap delete at the bottom left. That is the fastest way I have found. -
How can i compare 2 internal table's data which have same structure ?
hi friends,
i want to know how to compare 2 internal table's data which have same structureDATA: BEGIN OF LINE,
COL1 TYPE I,
COL2 TYPE I,
END OF LINE.
DATA: ITAB LIKE TABLE OF LINE,
JTAB LIKE TABLE OF LINE.
DO 3 TIMES.
LINE-COL1 = SY-INDEX.
LINE-COL2 = SY-INDEX ** 2.
APPEND LINE TO ITAB.
ENDDO.
MOVE ITAB TO JTAB.
LINE-COL1 = 10. LINE-COL2 = 20.
APPEND LINE TO ITAB.
IF ITAB GT JTAB.
WRITE / 'ITAB GT JTAB'.
ENDIF.
APPEND LINE TO JTAB.
IF ITAB EQ JTAB.
WRITE / 'ITAB EQ JTAB'.
ENDIF.
LINE-COL1 = 30. LINE-COL2 = 80.
APPEND LINE TO ITAB.
IF JTAB LE ITAB.
WRITE / 'JTAB LE ITAB'.
ENDIF.
LINE-COL1 = 50. LINE-COL2 = 60.
APPEND LINE TO JTAB.
IF ITAB NE JTAB.
WRITE / 'ITAB NE JTAB'.
ENDIF.
IF ITAB LT JTAB.
WRITE / 'ITAB LT JTAB'.
ENDIF.
The output is:
ITAB GT JTAB
ITAB EQ JTAB
JTAB LE ITAB
ITAB NE JTAB
ITAB LT JTAB
This example creates two standard tables, ITAB and JTAB. ITAB is filled with 3 lines and copied to JTAB. Then, another line is appended to ITAB and the first logical expression tests whether ITAB is greater than JTAB. After appending the same line to JTAB, the second logical expression tests whether both tables are equal. Then, another line is appended to ITAB and the third logical expressions tests whether JTAB is less than or equal to ITAB. Next, another line is appended to JTAB. Its contents are unequal to the contents of the last line of ITAB. The next logical expressions test whether ITAB is not equal to JTAB. The first table field whose contents are different in ITAB and JTAB is COL1 in the last line of the table: 30 in ITAB and 50 in JTAB. Therefore, in the last logical expression, ITAB is less than JTAB.
regards,
srinivas
<b>*reward for useful answers*</b> -
how do i retrieve photos that have been deleted from my ipod touch if i have windows operating system?
Yest it will restore photos deleted from yur iOd probvide that the photos were taken before the the last synce/backyp was made. Make syre yu have automatic syncing turned off (Itunes>Preference>Devices>Check the box that says prevent auto syncing when connected). Otherwise when connected, a backup that reflects the deleted photos will be made and will replace the previous backup.
-
how to transfer all my music, movies, and application from my new iPhone5 to Computer. All my music, movies, and apps have been deleted from my computer, thankfully I still have them all in my iPhone. I need to transfer everything back into my PC.. Help!
You need a third-party program lke one of those discussed here:
newer copy -
How To Create Table View With Same Column name But Different Table?
Hi All,
I have the problem to create a tableview with same column name but in different table.
The Table that i have:-
Table - PAC051MPROFORMA
Column - mrn,visitid
Table - PAC051TPROFORMA
Column - mrn,visitid
Table - PAC052MTRANSBILL
Column - mrn,visitid
Then i want to create a table view to view that table. This is my SQL
CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
As Select PAC051MPROFORMA.mrn,PAC051MPROFORMA.visitid,PAC051TPROFORMA.mrn,PAC051TPROFORMA.visitid,PAC052MTRANSBILL.mrn,PAC052MTRANSBILL.visitid
where
*(a.PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)*
and
*(a.PAC051TPROFORMA.mrn=PAC052TRANSBILL.mrn)*
That SQL Return this error = ORA-00957: duplicate column name
Then I modify that SQL to
CREATE VIEW pacviewproforma (mrn,visitid)
As Select PAC051MPROFORMA.mrn,PAC051MPROFORMA.visitid,PAC051TPROFORMA.mrn,PAC051TPROFORMA.visitid,PAC052MTRANSBILL.mrn,PAC052MTRANSBILL.visitid
where
*(a.PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)*
and
*(a.PAC051TPROFORMA.mrn=PAC052TRANSBILL.mrn)*
This time this error return = ORA-01730: invalid number of column names specified
What should i do?
Thanks...Hi,
SQL> CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
2 As Select
3 PAC051MPROFORMA.mrn,
4 PAC051MPROFORMA.visitid,
5 PAC051TPROFORMA.mrn,
6 PAC051TPROFORMA.visitid,
7 PAC052MTRANSBILL.mrn,
8 PAC052MTRANSBILL.visitid
9 from PAC051MPROFORMA,PAC051TPROFORMA,PAC052MTRANSBILL
10 where
11 (PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)
12 and
13 (PAC051TPROFORMA.mrn=PAC052MTRANSBILL.mrn);
CREATE VIEW pacviewproforma (mrn,visitid,mrn,visitid,mrn,visitid)
ERROR at line 1:
ORA-00957: duplicate column namePlease give different names to each column.
Something like this..
SQL> CREATE OR REPLACE VIEW pacviewproforma (MPROFORMA_mrn,MPROFORMA_visitid,TPROFORMA_mrn,TPROFORMA
_visitid,MTRANSBILL_mrn,MTRANSBILL_visitid)
2 As Select
3 PAC051MPROFORMA.mrn,
4 PAC051MPROFORMA.visitid,
5 PAC051TPROFORMA.mrn,
6 PAC051TPROFORMA.visitid,
7 PAC052MTRANSBILL.mrn,
8 PAC052MTRANSBILL.visitid
9 from PAC051MPROFORMA,PAC051TPROFORMA,PAC052MTRANSBILL
10 where
11 (PAC051MPROFORMA.mrn=PAC051TPROFORMA.mrn)
12 and
13 (PAC051TPROFORMA.mrn=PAC052MTRANSBILL.mrn);
View created.
SQL> DESC pacviewproforma;
Name Null? Type
MPROFORMA_MRN NUMBER
MPROFORMA_VISITID NUMBER
TPROFORMA_MRN NUMBER
TPROFORMA_VISITID NUMBER
MTRANSBILL_MRN NUMBER
MTRANSBILL_VISITID NUMBER
ORA-01730: invalid number of column names specifiedThe list of column nmae you specified during the CREATE VIEW should match with the SELECT list of the view.
Twinkle -
View the tables with same column name
Hi all
I have 10-20 tables and they have some same column names
How can I know it?
I mean, I want to know the tables which have the common column names
Thanks...SQL> break on column_name
SQL> set linesize 1000
SQL> set pagesize 10000
SQL> select column_name, owner, table_name
2 from (select owner, table_name, column_name, count(*) over (partition by column_name) ct
3 from dba_tab_columns)
4 where ct>1
SQL> /
COLUMN_NAME OWNER TABLE_NAME
AADSAS_ID_NBR H89UCBAC PS_SAD_ADA_CD
H89UCBAC PS_SAD_ADA_SUS
H89UCBAC PS_SAD_ADA_TMP
AA_CODE H89UCBAC PS_RQ_CONDIN_SLVW
H89UCBAC PS_RQ_CONDIN_SRCH
H89UCBAC PS_DUMMYDESCR_LANG
H89UCBAC PS_DUMMY_DESCR
H89UCBAC PS_RQ_NOTINCOND_LV
H89UCBAC PS_RQ_NOTINCOND_SR
[...]Nicolas. -
List of dept where all employees have same age
Hello Guys ,
I have a emp table and a city table , wherein I have details related to employees and cities .
I want those dept where in all employees belongs to a particular city .
Thanks in advanceHi,
96097f0e-f165-463a-a0a2-3d15214c8a3d wrote:
Hello Guys ,
I have a emp table and a city table , wherein I have details related to employees and cities .
I want those dept where in all employees belongs to a particular city .
Thanks in advance
The title you chose for this thread is
List of dept where all employees have same age
but I don't see anything about age in the description of the problem. This is one example why you need to post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data, as detailed in the forum FAQ: https://forums.oracle.com/message/9362002
As mentioned before, post your best attempt at a solution.
Did you mix up 2 separate problems from the same homework assignment? If so, solve the problem about the particular city first; it's a little simpler.
Maybe you are looking for
-
On my IPad, the books I have downloaded are there; however, the text has disappeared. It only happened with the books that were older. Any idea how I can get the text back on these books?
-
Anyone going from the iPad VGA connector to DVI-A?
Anyone going from the iPad VGA connector to DVI-A? I'd love to hear about your setup. I'm having an issue. Please respond at this thread: http://discussions.apple.com/thread.jspa?threadID=2566089&tstart=0
-
Hello !! Can anybody please let me know as to how should i make my java program compliant with proxy sever (in other words ...what changes are required in a java program so that it becomes compatible with a proxy server)....what i want to do is....i
-
Hi gurus: How to export the the object what I did like Infoobject & InfoCube &InfoSource & so on from one server, then import to another server. Thanks. chan
-
Having problems with multisim 12 wattmeter.
When I add the wattmeter to calculate the PF in multisim 12 ,it can not run the circuit anymore and Convergence Assistant also fails to resolve this problem automatically. However, when I remove it, everything would be fine. I would appreciate any id