Home » SQL & PL/SQL » SQL & PL/SQL » PL/SQL - Passing PARAMETER as a TABLE
PL/SQL - Passing PARAMETER as a TABLE [message #429901] |
Fri, 06 November 2009 03:44  |
aimy Messages: 126 Registered: June 2006 |
Senior Member |
|
|
Hi akl.
Here is my plan...
CREATE OR REPLACE PROCEDURE P_NIS_DEL_NONMIGRATED (table_name IN varchar2, column_name IN varchar2) as
cursor s1 is SELECT rowid, t.column_name
from table_name t
where not exists (select LOCN_TTNAME from LOCATIONS where LOCN_TTNAME = column_name);
-- cursor s1 is SELECT rowid, t.LOCN_TTNAME from LOCATIONS t;
i number := 0;
j number := 0;
start_time date := sysdate;
proc_name VARCHAR2(36) := 'P_NIS_DEL_NONMIGRATED_'||table_name;
begin
for c1 in s1 loop
delete from table_name where rowid = c1.rowid;
i := i + 1; -- Commit after every 1500 records
if i > 1500 then
commit;
i := 0;
end if;
j := j + 1;
end loop;
commit;
dbms_output.put_line(chr(0));
dbms_output.put_line('DELETION for '||table_name);
dbms_output.put_line('-------------------------------------------------');
dbms_output.put_line(chr(0));
DBMS_OUTPUT.PUT_LINE (j ||' rows have been deleted!');
dbms_output.put_line(chr(0));
insert into PROCEDURE_TRACKING_LOG
(procedure_name, table_name, rows_inserted, rows_updated, rows_deleted,
rows_inserted_of_deletion, rows_inserted_after_deletion,
rows_rejected, start_time, end_time,
elapse_time, remarks, subject_area)
values
(proc_name,table_name,NULL,NULL,j,NULL,NULL,NULL,start_time,sysdate,
(sysdate-start_time)* 86400,
NULL,'DELETION (NIS - NON MIGRATED)');
commit;
end;
/
That is I want to pass a table_name as well as its column_name as the parameter to the procedure.
And then the procedure will execute according to the given parameters.
Is it possible to do something like that?
Thank you very much..
[Updated on: Fri, 06 November 2009 04:54] by Moderator
|
|
| |
| Re: PL/SQL - Passing PARAMETER as a TABLE [message #429906 is a reply to message #429901] |
Fri, 06 November 2009 03:57   |
Frank Messages: 7317 Registered: March 2000 |
Senior Member |
|
|
This is about the worst way to achieve what you want.
First of all, you do not want to provide people with a procedure that can effectively remove all records from all tables.
I can not imagine that you don't foresee for which tables this procedure is going to be used. So, write the static sql-commands for each of these tables and use IF-THEN constructs to execute the desired one.
Then the deletion itself. You first fetch, one row at a time, each row you want to delete.
If you have sufficient rollback/undo space, just do the delete in one go, without intermediate commits.
If you cannot do that for whatever reason, don't fetch the rows you will delete, but use the cursor-query in your delete-statement and bind the number of rows hit by using WHERE ROWNUM <= 1500
|
|
| |
| Re: PL/SQL - Passing PARAMETER as a TABLE [message #430140 is a reply to message #429906] |
Mon, 09 November 2009 02:10   |
aimy Messages: 126 Registered: June 2006 |
Senior Member |
|
|
Frank wrote on Fri, 06 November 2009 17:57This is about the worst way to achieve what you want.
First of all, you do not want to provide people with a procedure that can effectively remove all records from all tables.
I can not imagine that you don't foresee for which tables this procedure is going to be used. So, write the static sql-commands for each of these tables and use IF-THEN constructs to execute the desired one.
Then the deletion itself. You first fetch, one row at a time, each row you want to delete.
If you have sufficient rollback/undo space, just do the delete in one go, without intermediate commits.
If you cannot do that for whatever reason, don't fetch the rows you will delete, but use the cursor-query in your delete-statement and bind the number of rows hit by using WHERE ROWNUM <= 1500
Thank you very much for your alert and also thank you for your advice.
It's just that, those tables that are going to be deleted are the huge one. So I guess by doing it within a PL/SQL with a subsequent commit, it would be save.
And since it's going to use the same concept of deletion, that's why I was thinking of creating some flexible PL/SQL rather than a single PL/SQL for each one.
That's all.
Besides, since the procedure requires parameters to execute, there will be less harm I guess?
Thank you.
|
|
| | | | | | |
| Re: PL/SQL - Passing PARAMETER as a TABLE [message #430331 is a reply to message #430304] |
Tue, 10 November 2009 02:11   |
 |
ayush_anand Messages: 270 Registered: November 2008 |
Senior Member |
|
|
CREATE OR REPLACE PROCEDURE ZZZ_P_DYNAMIC
(table_in IN VARCHAR2, column_in in VARCHAR2, string_in in VARCHAR2)
IS
cursor_handle INTEGER;
feedback INTEGER;
j number;
BEGIN
/* Create a cursor to use for the dynamic SQL */
cursor_handle := DBMS_SQL.OPEN_CURSOR;
/* Construct the SQL statement and parse it in native mode. */
DBMS_SQL.PARSE
(cursor_handle,'delete from '||table_in||' where '||column_in||' like ''%'||string_in||'%'||'''',
DBMS_SQL.NATIVE);
commit;
/* You should always execute your DDL! */
feedback := DBMS_SQL.EXECUTE (cursor_handle);
j :=DBMS_SQL.LAST_ROW_COUNT ;
DBMS_SQL.CLOSE_CURSOR (cursor_handle);
commit;
dbms_output.put_line(chr(0));
dbms_output.put_line('DELETION for ZZZ_DYNAMIC');
dbms_output.put_line('-------------------------------------------------');
dbms_output.put_line(chr(0));
DBMS_OUTPUT.PUT_LINE (j||' rows have been deleted!');
dbms_output.put_line(chr(0));
END ZZZ_P_DYNAMIC;
/CREATE OR REPLACE PROCEDURE zzz_p_dynamic (
table_in IN VARCHAR2,
column_in IN VARCHAR2,
string_in IN VARCHAR2
)
IS
sql_query VARCHAR2 (2000);
j NUMBER;
BEGIN
sql_query:='delete from '||table_in||' where '||column_in||' like ''%'||string_in||'%'||'''';
EXECUTE IMMEDIATE sql_query;
j := SQL%ROWCOUNT;
COMMIT;
DBMS_OUTPUT.put_line (CHR (0));
DBMS_OUTPUT.put_line ('DELETION for ZZZ_DYNAMIC');
DBMS_OUTPUT.put_line ('-------------------------------------------------');
DBMS_OUTPUT.put_line (CHR (0));
DBMS_OUTPUT.put_line (j || ' rows have been deleted!');
DBMS_OUTPUT.put_line (CHR (0));
END zzz_p_dynamic;
/
both work
Quote:BlackSwan wrote on Tue, 10 November 2009 10:07
aimy wrote on Mon, 09 November 2009 17:40
So anyone could help me for that row count?
I've been looking for a solution but it seems they are too complicated for just what I want to achieve.
TOO COMPLICATED?
feedback := DBMS_SQL.EXECUTE (cursor_handle);
j := sql%rowcount;
from
http://download-west.oracle.com/docs/cd/B28359_01/appdev.111/b28370/dynamic.htm
In the following situations, you must use native dynamic SQL instead of the DBMS_SQL package:
The dynamic SQL statement retrieves rows into records.
You want to use the SQL cursor attribute %FOUND, %ISOPEN, %NOTFOUND, or %ROWCOUNT after issuing a dynamic SQL statement that is an INSERT, UPDATE, DELETE, or single-row SELECT statement.
[Updated on: Tue, 10 November 2009 02:23]
|
|
| | | | |
Goto Forum:
Current Time: Tue Dec 1 10:21:12 CST 2009
Total time taken to generate the page: 0.04342 seconds
|