Procedure to insert in an unknown table - oracle

Question about Oracle - pl/sql.
is it possible to create a procedure that takes the name of the table as parameter, it retrieves the columns of the table and asks for the values of each column and then it insert the data.
if it is possible how can i do it?

Related

Generate insert statement using a stored procedure for all tables in a database

Using a a stored procedure I want to generate insert scripts for all the tables available in database and the table name and the columns must be passed as a parameter, how to do this?

Trigger to update after insert on the same row

I have a function my_func, the input of this function is one of the column in the same row after insertion, how to achieve this.
I have created the table using,
create table sample_trigger_hash ( INDEXID number(19,0) ,
XMLCOLUMN XMLTYPE ,
CHECKFIELD RAW(30)
) XMLTYPE COLUMN XMLCOLUMN STORE AS BINARY XML;
I am thinking of an insertion statement from DBD::Oracle in perl , with just the first two fields.
The following is my trigger,
CREATE OR REPLACE
TRIGGER hash_trigger
AFTER INSERT ON sample_trigger_hash
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
BEGIN
SELECT my_hash(EXTRACT(:OLD.XMLCOLUMN ,''))
INTO :NEW.CHECKFIELD
FROM dual;
END hash_trigger;
Am getting the error,
ORA-04084: cannot change NEW values for this trigger type
04084. 00000 - "cannot change NEW values for this trigger type"
*Cause: New trigger variables can only be changed in before row
insert or update triggers.
*Action: Change the trigger type or remove the variable reference.
But my requirement is to get the XMLType value after insertion, how to achieve this. If I use CLOB storage option for the XMLType, I am getting the same hash value before storing and after storing. But this is not the case when I store it as binary XML. (an XSI section is added to the binary version) More details in this other question.
Your requirement doesn't make sense. You want to include the hashed value in the newly inserted row, so put the function in a before insert trigger.

Convert ntext to clob

I have to copy data from one table to another which one table is in Oracle and one is in MSSQL Server. I want to copy the data from MSSQL Server table to Oracle table. The problem is that the MSSQL Server table has one column which is of data type ntext and the destination column in Oracle table is clob.
When I use the query
insert into oracle.table select * from sqlserver.table#mssql; I get the following error:
SQL Error: ORA-00997: illegal use of LONG datatype
Can anyone advice on this please?
I tried it through a PL/SQL Procedure and it worked. I created a cursor, passed in the values to my variables declared in VARCHAR2 and then run an EXECUTE IMMEDIATE for the INSERT INTO....SELECT * FROM <TABLE_NAME>#MSSQL.

Oracle: Changing VARCHAR2 column to CLOB

I have an encountered an issue where the data I was trying to store in my varchar2(4000) column was too big, so I wish to change the column to one more suitable for storing large amounts of textual data. Specifically, a serialized array.
Firstly, is CLOB the best data type for me to use for this purpose? Is there a more appropriate data type?
Secondly, when I try to alter the column using the usual snyntax:
ALTER TABLE table MODIFY column CLOB
I get the following error: ORA-22858: invalid alteration of datatype
What's the most straightforward way to alter this table without losing any data?
The most straightforward way, given that the operation of moving from a varchar column to a CLOB is disallowed, would be to create a new column and move the data from the old column to the new column:
ALTER TABLE some_table ADD (foo CLOB);
UPDATE some_table SET foo = old_column;
ALTER TABLE some_table DROP COLUMN old_column;
ALTER TABLE some_table RENAME COLUMN foo TO old_column;
The VARCHAR2 column cannot be directly converted to CLOB but it can be done in 2 steps:
Convert column datatype from VARCHAR2 to LONG.
Convert column datatype from LONG to CLOB.
ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;
For Oracle 11g:
ALTER TABLE table MODIFY column long;
ALTER TABLE table MODIFY column clob;

Stored Procedure for copying data from one table to another

I have pairs of tables in the format TABLE and TABLE_TWIN now
TABLE is the main table with lots of data
TABLE_TWIN is a table with the exact same fields with a little data (different data)
Now I would like to copy all rows from TABLE_TWIN to TABLE using a stored procedure. I have many such tables and could like the stored procedure to take the table name(s) as parameter(s) so that I can use the same procedure for each table pair. I do not want to write long INSERT statements because these tables have around 50 attributes each.
I am not good with PL/SQL so I need some help here.
Thanks!
SQL is not so long... But if you prefer a procedure, here it is:
create or replace procedure table_copy(
p_tab_from varchar2,
p_tab_to varchar2)
is
begin
execute immediate 'insert into '||p_tab_to||' (select * from '||p_tab_from||')';
end;
insert into table_twin (select * from table)
should do it

Resources