oracle 基础和管理 如何判断某个表中是否存在某个字段?

来源:互联网 发布:ubuntu 卸载vmware 编辑:程序博客网 时间:2024/05/30 04:15

问题 如何判断某个表中是否存在某个字段?

注意:表名和字段名一定要大写,否则结果不正确
一:

检查表名:
select count(*) from user_TABLES where table_name = 表名(大写);
检查字段名:
select count(*) from User_Tab_Columns where table_name=表名(大写) and column_name=字段名(大写)

二:

需要用脚本来实现
我在脚本中判断如果不在某个字段的话,我就给他加上。 如何判断某个表中是否存在某个字段?

需要用脚本来实现
我在脚本中判断如果不在某个字段的话,我就给他加上。 响应者 1:select * from all_tab_columns

看一下select的结果,你就懂了.... 响应者 2:if exists(select 1 from dba_tab_columns where table_name='test' and column_name='abs')
alter table ........ 响应者 3:楼上的正确,如果该字段不存在,select 1 from dba_tab_columns where table_name='test' and column_name='abs' 结果是空 响应者 4:我借你的贵贴练习下procedure :)
create or replace procedure addcolumn(tabname in varchar2, colname in varchar2,coltype in varchar2) as
cursor c_cols is select column_name from user_tab_cols where table_name=tabname;
r_col c_cols%rowtype;
str_sql varchar2(200);
flag number(1);
begin
flag:=0;
open c_cols;
loop
fetch c_cols into r_col;
exit when c_cols%notfound;
--dbms_output.put_line(colname);
if r_col.column_name=colname then
flag:=1;
dbms_output.put_line('i found it!');
end if;
end loop;
if flag=0 then
str_sql:='alter table '||tabname||' add '||colname||' '||coltype;
dbms_output.put_line(str_sql);
execute immediate str_sql;
end if;

exception
when others then
null;

end addcolumn;

sql>execute addcolumn('t','id','number(2)');)
 

原创粉丝点击