oracle判断表是否存在字段

大哥,打扰一下,oracle判断表是否存在字段
最新回答
緈諨の约定

2024-11-26 08:06:56

方法一:可以用user_tab_cols表进行查询,查询有结果表示字段存在:
sql:select * from user_tab_cols where table_name='T_AAA' and column_name='COL_BBB';
方法二:也可以用all_tab_columns表进行查询,查询有结果表示字段存在:
sql:select * from all_tab_columns where owner='SYS_CCC' and table_name='T_AAA' and column_name='COL_BBB';
备注:所有的查询字段必须是大写,否则查询会有误差。
风微甜

2024-11-26 15:39:08

oracle数据库中的sys scheme下有两张表user_tab_columns和user_tab_cols都保存了当前用户的表、视图和Clusters中的列信息,如果你需要查询当前用户下某张表的某个字段的话,可以写如下sql:select * from user_tab_cols where table_name='T_AAA' and column_name='COL_BBB';该sql查询T_AAA表的字段COL_BBB的结构信息,包括列名、表名、列字段类型、长度、精度、小数点后位数、是否允许为空、默认值等等。oracle数据库中的sys scheme下还有另外两张表all_tab_columns和all_tab_cols保存了数据库中所有表的列信息,如果你需要查询某张表的某个字段的话,也可以这样写sql:select * from all_tab_columns where owner='SYS_CCC' and table_name='T_AAA' and column_name='COL_BBB';该sql查询SYS_CCC用户下T_AAA表的字段COL_BBB的结构信息,结构信息和user_tab_columns的结构信息基本相同。
白云下的棉絮

2024-11-26 14:35:20

oracle在plsql里执行如下代码:DECLARE v_table tabs.table_name%TYPE; v_sql VARCHAR2(888); v_q NUMBER; CURSOR c1 IS SELECT table_name tn FROM tabs; TYPE c IS REF CURSOR; c2 c; BEGIN DBMS_OUTPUT.PUT_LINE('以下为空数据表的表名:'); FOR r1 IN c1 LOOP v_table :=r1.tn; v_sql :='SELECT COUNT(*) q FROM '||v_table; OPEN c2 FOR v_sql; LOOP FETCH c2 INTO v_q; EXIT WHEN c2%NOTFOUND; IF v_q=0 THEN DBMS_OUTPUT.PUT_LINE(v_table); END IF; END LOOP; CLOSE c2; END LOOP; EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Error occurred'); END; 然后点output,显示的就是空表
-对着教师唱情歌

2024-11-26 17:50:05

1、判断表是否在数据库中存在
'SELECT COUNT(*) FROM USER_OBJECTS WHERE OBJECT_NAME = UPPER (''' || V_TBL_NAME || ''')'
2、判断列是否在数据库中存在
‘SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = UPPER('''||V_TBL_NAME || ''') AND COLUMN_NAME = ''SURVEYRESULT'''

CREATE OR REPLACE PROCEDURE INSERTTABLESBYENTERPRISEID (

ENTERPRISEID IN STRING

)

IS

TYPE CURSOR_TEMP IS REF CURSOR;

V_CURSOR CURSOR_TEMP;

V_TBL_NAME VARCHAR2 (100) := '';

V_SQL VARCHAR2 (500) := '';

V_EXESQL VARCHAR2 (500) := '';

V_EXITOBJ INTEGER := 0;

BEGIN

V_SQL :=

'SELECT DISTINCT(RESULTTABLENAME) FROM SD_T_A01_'

|| ENTERPRISEID

|| ' WHERE ISDEL=0';

OPEN V_CURSOR FOR V_SQL;

LOOP

FETCH V_CURSOR INTO V_TBL_NAME;

EXIT WHEN V_CURSOR%NOTFOUND;

IF (V_TBL_NAME IS NULL)

THEN

BEGIN

V_TBL_NAME := 'SD_T_A07_' || ENTERPRISEID;

END;

END IF;

V_EXESQL := 'SELECT COUNT(*) FROM USER_OBJECTS WHERE OBJECT_NAME = UPPER (''' || V_TBL_NAME || ''')';

EXECUTE IMMEDIATE V_EXESQL INTO V_EXITOBJ;

IF V_EXITOBJ > 0

THEN

NULL;

V_EXITOBJ := 0;

V_EXESQL := 'SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = UPPER('''|| V_TBL_NAME || ''') AND COLUMN_NAME = ''SURVEYRESULT''';

EXECUTE IMMEDIATE V_EXESQL INTO V_EXITOBJ;

IF V_EXITOBJ > 0

THEN

V_SQL := 'ALTER TABLE '|| V_TBL_NAME|| ' ADD SurveyResult VARCHAR2(500)';

EXECUTE IMMEDIATE V_SQL;

END IF;

END IF;

END LOOP;

COMMIT;

END INSERTTABLESBYENTERPRISEID;
查理的天使

2024-11-26 16:18:02

你是说表里面有多少列么?

SQL> desc emp
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 EMPNO                                     NOT NULL NUMBER(4)
 ENAME                                              VARCHAR2(10)
 JOB                                                VARCHAR2(9)
 MGR                                                NUMBER(4)
 HIREDATE                                           DATE
 SAL                                                NUMBER(7,2)
 COMM                                               NUMBER(7,2)
 DEPTNO                                             NUMBER(2)

SQL> select count(COLUMN_NAME) from USER_TAB_COLUMNS where TABLE_NAME='EMP';

COUNT(COLUMN_NAME)
------------------
                 8


类似的视图还有DBA_TAB_COLUMNS 和 ALL_TAB_COLUMNS