如何从sys.sysconstraints计算状态?

时间:2017-09-16 09:47:43

标签: sql-server

select * from sys.sysconstraints

我的状态为

| id         |status|
|------------|------|
|<object_id> | 2593 |
|<object_id> | 3106 |

有人能说出在SQL Server中如何计算状态吗?

2 个答案:

答案 0 :(得分:3)

您可以使用exec sp_helptext 'sys.sysconstraints'查看其确切计算方式。

这将显示视图的定义。

CREATE VIEW sys.sysconstraints AS  
 SELECT constid = object_id,  
  id = parent_object_id,  
  colid = convert(smallint, property),  
  spare1 = convert(tinyint, 0),  
  status = convert(int,  
    CASE type  
     WHEN 'PK' THEN 1 WHEN 'UQ' THEN 2 WHEN 'F ' THEN 3  
     WHEN 'C ' THEN 4 WHEN 'D ' THEN 5 ELSE 0 END  
    + CASE WHEN property <> 0 THEN (16) ELSE (32) END  
    + CASE WHEN ObjectProperty(object_id, 'CnstIsClustKey') <> 0  
      THEN (512) ELSE 0 END  
    + CASE WHEN ObjectProperty(object_id, 'CnstIsNonclustKey') <> 0  
      THEN (1024) ELSE 0 END  
    + (is_system_named * 131072)  
    + (2048)      -- CNST_NOTDEFERRABLE  
    + (is_disabled * 16384)  
    + (is_not_for_replication * 2097152)),  
  actions = convert(int,  4096),  
  error = convert(int, 0)  
 FROM sys.objects$  
 WHERE (parent_object_id > 0 OR   
  (parent_object_id & 0xe0000000 = 0xa0000000)) -- IsLocalTempObjectId  
  AND type IN ('C ', 'F ', 'PK', 'UQ', 'D ') 

所以反转这个的代码是

 SELECT *
 FROM (VALUES(2593), 
             (3106)) V(Status)
 CROSS APPLY
 (
 SELECT CASE WHEN Status & 5 = 5 THEN 'D '
             WHEN Status & 4 = 4 THEN 'C '
             WHEN Status & 3 = 3 THEN 'F '
             WHEN Status & 2 = 2 THEN 'UQ'
             WHEN Status & 1 = 1 THEN 'PK '
         END AS type, 
         CASE WHEN Status & 16 = 16 THEN 'column level'
             WHEN Status & 32 = 32 THEN 'table level'
         END AS level, 
         CASE WHEN Status & 512 = 512 THEN 'Clustered key'
             WHEN Status & 1024 = 1024 THEN 'Non clustered key'
         END AS ClustNonClustKey, 
         CASE WHEN Status & 16384 = 16384 THEN 'true' ELSE 'false' END
         AS is_disabled, 
         CASE WHEN Status & 131072 = 131072 THEN 'true' ELSE 'false' END
         AS is_system_named, 
         CASE WHEN Status & 2097152 = 2097152 THEN 'true' ELSE 'false' END
         AS is_not_for_replication
)CA

返回

+--------+------+-------------+-------------------+-------------+-----------------+------------------------+
| Status | type |    level    | ClustNonClustKey  | is_disabled | is_system_named | is_not_for_replication |
+--------+------+-------------+-------------------+-------------+-----------------+------------------------+
|   2593 | PK   | table level | Clustered key     | false       | false           | false                  |
|   3106 | UQ   | table level | Non clustered key | false       | false           | false                  |
+--------+------+-------------+-------------------+-------------+-----------------+------------------------+

答案 1 :(得分:1)

基于sys.sysconstraints。伪比特掩码表示状态。

  

1 = PRIMARY KEY约束

     

2 = UNIQUE KEY约束

     

3 = FOREIGN KEY约束

     

4 =检查约束

     

5 =默认约束

     

16 =列级约束

     

32 =表级约束

无论如何,根据你想要实现的目标,我建议使用INFORMATION_SCHEMA或特定的约束视图,而不是试图解析这个已弃用的sys视图。