日期:2014-05-16  浏览次数:20332 次

常用数据库查询判断表和字段是否存在

1、MSSQL Server

        表:select  COUNT(*)  from  dbo.sysobjects WHERE name=  'table_name';
        字段:select  COUNT(*)  from syscolumns WHERE id=object_id(‘table_name’)  and name=  'column_name';

2、My SQL

        表:select COUNT(*) from information_schema.tables WHERE table_name = 'table_name';
    字段:select COUNT(*) from information_schema.columns WHERE table_name = 'table_name' and column_name = 'column_name';

3、Oracle

        表:SELECT count(*) FROM USER_OBJECTS WHERE  OBJECT_NAME =  'table_name';
        字段:SELECT COUNT(*) FROM USER_TAB_COLUMNS WHERE TABLE_NAME = 'table_name' and column_name = 'column_name';

4、PostgreSql

        表:select count(*) from information_schema.tables WHERE table_schema='table_schema' and  table_name ='table_name';

        字段:select count(*) from information_schema.columns WHERE table_schema='table_schema' and table_name ='table_name' and  column_name='column_name';


    总结:以上结果为1表示存在,为0表示不存在。