cuiyi's blog(崔毅 crazycy)

          記錄點滴 鑒往事之得失 以資于發展
          數據加載中……

          SQL Server: SYSOBJECTS

          Use OBJECTPROPERTY To Generate A List Of Object Types

          How do you query the sysobjects system table and get the object type back for every single object
          You can use the type and xtype columns, these contain the following data

          xtype
          Object type. Can be one of these object types:
          C = CHECK constraint
          D = Default or DEFAULT constraint
          F = FOREIGN KEY constraint
          L = Log
          FN = Scalar function
          IF = Inlined table-function
          P = Stored procedure
          PK = PRIMARY KEY constraint (type is K)
          RF = Replication filter stored procedure
          S = System table
          TF = Table function
          TR = Trigger
          U = User table
          UQ = UNIQUE constraint (type is K)
          V = View
          X = Extended stored procedure


          type
          Object type. Can be one of these values:
          C = CHECK constraint
          D = Default or DEFAULT constraint
          F = FOREIGN KEY constraint
          FN = Scalar function
          IF = Inlined table-function
          K = PRIMARY KEY or UNIQUE constraint
          L = Log
          P = Stored procedure
          R = Rule
          RF = Replication filter stored procedure
          S = System table
          TF = Table function
          TR = Trigger
          U = User table
          V = View
          X = Extended stored procedure

          Or you can use OBJECTPROPERTY. OBJECTPROPERTY is better in my opinion because you can see right away what you are looking for
          For example OBJECTPROPERTY ( id , 'IsUserTable' ) is much easier to understand than type = 'u'

          Bu using CASE with OBJECTPROPERTY we can generate a nice report

          SELECT name,CASE
          WHEN OBJECTPROPERTY ( id , 'IsSystemTable' ) =1 THEN 'System Table'
          WHEN OBJECTPROPERTY ( id , 'IsProcedure' ) =1 THEN 'Procedure' 
          WHEN OBJECTPROPERTY ( id , 'IsPrimaryKey' ) =1 THEN 'Primary Key' 
          WHEN OBJECTPROPERTY ( id , 'IsDefault' ) =1 THEN 'Default'
          WHEN OBJECTPROPERTY ( id , 'IsForeignKey' ) =1 THEN 'Foreign Key'
          WHEN OBJECTPROPERTY ( id , 'IsCheckCnst' ) =1 THEN 'Check Constraint'
          WHEN OBJECTPROPERTY ( id , 'IsView' ) =1 THEN 'View'
          WHEN OBJECTPROPERTY ( id , 'IsConstraint' ) =1 THEN 'Constraint'
          WHEN OBJECTPROPERTY ( id , 'IsTrigger' ) =1 THEN 'Trigger'
          WHEN OBJECTPROPERTY ( id , 'IsScalarFunction' ) =1 THEN 'Scalar Function'
          WHEN OBJECTPROPERTY ( id , 'IsTableFunction' ) =1 THEN 'Table Valued Function'
          WHEN OBJECTPROPERTY ( id , 'IsRule' ) =1 THEN 'Rule'
          WHEN OBJECTPROPERTY ( id , 'IsExtendedProc' ) =1 THEN 'Extended Stored Procedure'
          WHEN OBJECTPROPERTY ( id , 'IsUserTable' ) =1 THEN 'User Table'
          END ObjectType, *
          FROM sysobjects

          And of course there are a bunch of INFORMATION_SCHEMA views that you can use to get some of the same information back

          SELECT * FROM INFORMATION_SCHEMA.CHECK_CONSTRAINTS
          SELECT * FROM INFORMATION_SCHEMA.CONSTRAINT_TABLE_USAGE
          SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS
          SELECT * FROM INFORMATION_SCHEMA.TABLES
          SELECT * FROM INFORMATION_SCHEMA.VIEWS
          SELECT * FROM INFORMATION_SCHEMA.COLUMNS


          for example, want to find a procedure and drop it.
          if exists (select * from dbo.sysobjects 
                       where id = object_id('dbo.PROC_HELLOWORLD') 
                                 and OBJECTPROPERTY(id, 'IsProcedure') = 1)
          drop procedure dbo.PROC_HELLOWORLD;

          another a bit difficult example, want to find a UDF and drop it.
          if exists (select * from dbo.sysobjects
                       where id = object_id('dbo.FUN_HELLOWORLD)
                                and type in ('FN', 'IF', 'TF', 'FS', 'FT'))
          DROP FUNCTION dbo.FUN_HELLOWORLD

          posted on 2012-12-31 13:04 crazycy 閱讀(303) 評論(0)  編輯  收藏 所屬分類: DBMS

          主站蜘蛛池模板: 盐亭县| 渑池县| 綦江县| 寿光市| 张家口市| 马边| 永安市| 仁化县| 左云县| 周宁县| 宁乡县| 永胜县| 金昌市| 皋兰县| 马鞍山市| 德安县| 林西县| 鄂托克旗| 沈阳市| 任丘市| 蒲城县| 莱西市| 兴隆县| 华宁县| 盱眙县| 轮台县| 蒲江县| 通辽市| 开平市| 湘西| 邵东县| 广宗县| 鹿邑县| 申扎县| 河曲县| 孝感市| 萨迦县| 海盐县| 祁东县| 富源县| 新竹县|