ORACEL中修改表字段名称

1将下面的程序贴入sqlplus
2 grant drop table to user_nmae
3 grant execute on yg_drop_column to user_name
4 执行 execute yg_drop_column('your_table','drop_column')即可
他保留了原有的表中数据



CREATE OR REPLACE PROCEDURE yg_drop_column(m_table_name varchar2, m_column_name varchar2)
as
v_column_name varchar2(30000);
i integer;
v_sqlfirst varchar2(30000);
v_sqlsecond varchar2(30000);
v_sqlthird varchar2(30000);
v_sqlforth varchar2(30000);

cursor c_alter_table is
select column_name from user_tab_columns where lower(table_name) = lower(m_table_name);


begin

-- 产生生成表的动态语句

i := 0;

v_sqlfirst := ' create table ' || m_table_name || '_dcx' || ' as (select ' ;

open c_alter_table;

loop
fetch c_alter_table into v_column_name;
exit when c_alter_table%notfound;
if(lower(trim(v_column_name)) != lower(trim(m_column_name))) then
if (i=0) then
v_sqlfirst := v_sqlfirst || ' ' || v_column_name || ' ' ;
else
v_sqlfirst := v_sqlfirst || ', ' || v_column_name || ' ' ;
end if;
i := i + 1;
end if;

end loop;

close c_alter_table;

v_sqlfirst := v_sqlfirst || ' from ' || m_table_name || ')' ;
v_sqlsecond := ' drop table ' || m_table_name ;
v_sqlthird := 'create table ' || m_table_name || ' as (select * from ' || m_table_name || '_dcx)' ;
v_sqlforth := 'drop table ' || m_table_name || '_dcx' ;



-- dbms_output.put_line(v_sqlfirst );
execute immediate v_sqlfirst ;
-- dbms_output.put_line(v_sqlsecond);
execute immediate v_sqlsecond ;
-- dbms_output.put_line(v_sqlthird);
execute immediate v_sqlthird;
-- dbms_output.put_line(v_sqlforth);
execute immediate v_sqlforth;

dbms_output.put_line(v_sqlfirst );
end;
/

你可能感兴趣的:(ora)