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 閱讀(307) 評論(0)  編輯  收藏 所屬分類: DBMS

          主站蜘蛛池模板: 西安市| 益阳市| 呼伦贝尔市| 宁强县| 行唐县| 惠东县| 安多县| 茌平县| 嘉禾县| 中宁县| 同德县| 沁水县| 大同市| 长乐市| 绥芬河市| 阿拉善左旗| 邵武市| 长汀县| 司法| 印江| 赤壁市| 永丰县| 玛纳斯县| 韩城市| 长泰县| 稻城县| 金华市| 瓮安县| 天台县| 上思县| 繁峙县| 深圳市| 都兰县| 安国市| 淮南市| 奉化市| 卢湾区| 宝应县| 常州市| 安平县| 崇礼县|