Skip to Main Content
  • Questions
  • DYNAMIC INSERT SCRIPT WITH TIMESTAMP ISSUE

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, vinodha.

Asked: January 12, 2017 - 4:58 am UTC

Last updated: January 12, 2017 - 6:21 am UTC

Version: 3.2.20.09

Viewed 1000+ times

You Asked

Hi,

I am new to oracle, i have used your create dynamic insert script for generating the insert script. But that query is taking care of only three datatypes like NUMBER, DATE and VARCHAR2(). It is not taking care about the TIMESTAMP data type since i need to check the TIMESTAMP(6) dayta type as i am using that in my query. (I have tried the same DATE datatype script but i have changed only the date to timestamp and tested since it is not giving any error it is not giving output in timestamp). Please help me to solve the issue.

your query: Please help us to add the TIMESTAMP(6) DATATYPE to create insert script

set serveroutput on size 100000
set feedback off

declare
v_table_name varchar2(30) := 'EMP'; -- Your Tablename
v_column_list varchar2(2000);
v_insert_list varchar2(2000);
v_ref_cur_columns varchar2(4000);
v_ref_cur_query varchar2(2000);
v_ref_cur_output varchar2(2000);
v_column_name varchar2(2000);
cursor c1 is select column_name, data_type from user_tab_columns where table_name = v_table_name order by column_id;
refcur sys_refcursor;
begin
for i in c1 loop
v_column_list := v_column_list||','||i.column_name;
if i.data_type = 'NUMBER' then
v_column_name := i.column_name;
elsif i.data_type = 'DATE' then
v_column_name := chr(39)||'to_date('||chr(39)||'||chr(39)'||'||to_char('||i.column_name||','||chr(39)||'dd/mm/yyyy hh:mi:ss'||chr(39)||')||chr(39)||'||chr(39)||', '||chr(39)||'||chr(39)||'||chr(39)||'dd/mm/rrrr hh:mi:ss'||chr(39)||'||chr(39)||'||chr(39)||')'||chr(39);
elsif i.data_type = 'VARCHAR2' then
v_column_name := 'chr(39)||'||i.column_name||'||chr(39)';
end if;
v_ref_cur_columns := v_ref_cur_columns||'||'||chr(39)||','||chr(39)||'||'||v_column_name;
end loop;
v_column_list := ltrim(v_column_list,',');
v_ref_cur_columns := substr(v_ref_cur_columns,8);

v_insert_list := 'INSERT INTO '||v_table_name||' ('||v_column_list||') VALUES ';
v_ref_cur_query := 'SELECT '||v_ref_cur_columns||' FROM '||v_table_name;

open refcur for v_ref_cur_query;
loop
fetch refcur into v_ref_cur_output;
exit when refcur%notfound;
v_ref_cur_output := '('||v_ref_cur_output||');';
v_ref_cur_output := replace(v_ref_cur_output,',,',',null,');
v_ref_cur_output := replace(v_ref_cur_output,'(,','(null,');
v_ref_cur_output := replace(v_ref_cur_output,',,)',',null)');
v_ref_cur_output := replace(v_ref_cur_output,'null,)','null,null)');
v_ref_cur_output := v_insert_list||v_ref_cur_output;
dbms_output.put_line (v_ref_cur_output);
end loop;
end;
/

please provide me a solution that should taking care of TIMESTAMP(6) datatype.

and Connor said...


Is this answer out of date? If it is, please let us know via a Comment

More to Explore

DBMS_OUTPUT

More on PL/SQL routine DBMS_OUTPUT here