Advertisement
Guest User

du

a guest
Aug 24th, 2017
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.73 KB | None | 0 0
  1. SET serveroutput ON SIZE 100000
  2. SET feedback off
  3.  
  4. DECLARE
  5. vtablename varchar2(30) := 'EMP'; -- Your Tablename
  6. vcolumnlist varchar2(2000);
  7. vinsertlist varchar2(2000);
  8. vrefcurcolumns varchar2(4000);
  9. vrefcurquery varchar2(2000);
  10. vrefcuroutput varchar2(2000);
  11. vcolumnname varchar2(2000);
  12. cursor c1 IS SELECT columnname, datatype FROM usertabcolumns WHERE tablename = vtablename ORDER BY columnid;
  13. refcur sysrefcursor;
  14. BEGIN
  15. FOR i IN c1 loop
  16. vcolumnlist := vcolumnlist||','||i.columnname;
  17. IF i.datatype = 'NUMBER' THEN
  18. vcolumnname := i.columnname;
  19. elsif i.datatype = 'DATE' THEN
  20. vcolumnname := chr(39)||'todate('||chr(39)||'||chr(39)'||'||tochar('||i.columnname||','||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);
  21. elsif i.datatype = 'VARCHAR2' THEN
  22. vcolumnname := 'chr(39)||'||i.columnname||'||chr(39)';
  23. END IF;
  24. vrefcurcolumns := vrefcurcolumns||'||'||chr(39)||','||chr(39)||'||'||vcolumnname;
  25. END loop;
  26. vcolumnlist := ltrim(vcolumnlist,',');
  27. vrefcurcolumns := substr(vrefcur_columns,8);
  28.  
  29. vinsertlist := 'INSERT INTO '||vtablename||' ('||vcolumnlist||') VALUES ';
  30. vrefcurquery := 'SELECT '||vrefcurcolumns||' FROM '||vtablename;
  31.  
  32. OPEN refcur FOR vrefcurquery;
  33. loop
  34. fetch refcur INTO vrefcuroutput;
  35. exit WHEN refcur%notfound;
  36. vrefcuroutput := '('||vrefcuroutput||');';
  37. vrefcuroutput := REPLACE(vrefcuroutput,',,',',null,');
  38. vrefcuroutput := REPLACE(vrefcuroutput,'(,','(null,');
  39. vrefcuroutput := REPLACE(vrefcuroutput,',,)',',null)');
  40. vrefcuroutput := REPLACE(vrefcuroutput,'null,)','null,null)');
  41. vrefcuroutput := vinsertlist||vrefcuroutput;
  42. dbmsoutput.putline (vrefcur_output);
  43. END loop;
  44. END;
  45. /`
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement