Wednesday, October 14, 2009

Bulding a Create table script for all tables in a schema

Hi,Here is a small procedure I put together to extract some tables out of a BD, I didn't have OS access to run export and import utility, it might be helpful for some of you...



procedure create_t_script (p_owner in varchar2)
cursor c_tables is select table_name, owner from all_tables where owner=p_owner;
cursor c_cols (l_table in VARCHAR2, l_owner in VARCHAR2) is
select * from all_tab_columns where owner=l_owner and table_name=l_table;
v_temp number;
begin

for lv_table in c_tables loop
dbms_output.put_line('CREATE TABLE '||lv_table.table_name||' ');
v_temp :=0;
for lv_cols in c_cols (lv_table.table_name, lv_table.owner) loop
if v_temp = 0 then
dbms_output.put_line('(');
else
dbms_output.put_line(',');
end if;
v_temp:=1;

dbms_output.put(lv_cols.column_name||' '||lv_cols.data_type);
if lv_cols.data_type like '%CHAR%' then
dbms_output.put(' ('||lv_cols.DATA_LENGTH||')');

end if;

if lv_cols.data_default is not null then
dbms_output.put(' DEFAULT '||lv_cols.data_default);
end if;


if lv_cols.nullable ='N' then
dbms_output.put(' NOT NULL');
end if;

end loop;
dbms_output.put_line(');');
dbms_output.put_line('');
end loop;

exception when others then dbms_output.put(sqlerrm);
end;

1 comment:

udialamorte said...

ford transit connect titanium and metal parts - Titanium
Ford construction work, titanium exhaust tips The Atrium and The Atrium are titanium or ceramic flat iron ready to titanium color transport all components of the tower, including titanium eyeglasses steel and titanium sheet metal brass ore.