sql server 語句獲取表的描述,主鍵等等


sql語句添加表,字段的描述

描述類型全部設置為:MS_Description

--添加表的描述
--格式如右:execute sp_addextendedproperty 'MS_Description','字段備注信息','user','dbo','table','字段所屬的表名','column','添加注釋的字段名';

--添加表描述
EXECUTE sp_addextendedproperty 'C', '課程表', 'user', 'dbo', 'table', 'Course',NULL, NULL;
    
--添加字段描述
EXECUTE sp_addextendedproperty 'MS_Description', '課程ID', 'user', 'dbo','table', 'Course', 'column', 'CourseId';

 

或者:右鍵表(列)的屬性,找到擴展屬性

 

①獲取表名

 

--查詢所有的表名稱
SELECT name FROM  sys.objects obj WHERE obj.type='U'; -- xtype:類型可以區分表,視圖,存儲過程,函數等等

 

 

②獲取表的描述  sys.extended_properties  這張表存的擴展描述

 

SELECT  obj.name 表名 ,
        ds.value 描述
FROM    sys.extended_properties ds
        LEFT JOIN sys.objects obj ON ds.major_id = obj.object_id
WHERE   ds.minor_id = 0
        AND obj.name = 'Course';

 

 

 

最后整理:

--最后整理
SELECT obj.name,
       se.value
FROM sys.objects obj
    LEFT JOIN [sys].[extended_properties] se
        ON obj.object_id = se.major_id
           AND se.minor_id = 0
WHERE obj.type = 'U';

 

③獲取表的主鍵信息

--表 sys.indexes
SELECT * FROM sys.indexes  WHERE is_primary_key = 1   AND object_id =Object_Id('Course')

 

--最后整理
SELECT obj.name,
       CAST(CASE
                WHEN
                (
                   SELECT COUNT(1) FROM sys.indexes  WHERE is_primary_key = 1   AND object_id =obj.object_id
                ) >= 1 THEN
                    1
                ELSE
                    0
            END AS BIT) AS HasPrimaryKey
FROM sys.objects obj
WHERE obj.type = 'U';

 

 ④獲取主鍵的類型和名稱

--得到表的主鍵列id
SELECT sc.column_id
FROM sys.indexes si
    INNER JOIN sys.index_columns sc
        ON si.index_id = sc.index_id
           AND si.object_id = sc.object_id
WHERE si.is_primary_key = 1
      AND si.object_id = OBJECT_ID('Course');

 

 --獲取主鍵類型和名稱
SELECT TOP 1
       colm.name AS TablePrimarkeyType,
       systype.name AS TablePrimarkeyName
FROM sys.columns colm
    INNER JOIN sys.types systype
        ON colm.system_type_id = systype.system_type_id
WHERE colm.object_id = OBJECT_ID('Course')
      AND colm.column_id IN (
                                SELECT ic.column_id
                                FROM sys.indexes idx
                                    INNER JOIN sys.index_columns ic
                                        ON idx.index_id = ic.index_id
                                           AND idx.object_id = ic.object_id
                                WHERE idx.object_id = OBJECT_ID('Course')
                                      AND idx.is_primary_key = 1
                            );

 

 --整理
SELECT obj.name,
       t.TablePrimarkeyType,
       t.TablePrimarkeyName
FROM sys.objects obj
    OUTER APPLY
(
    SELECT TOP 1
           colm.name AS TablePrimarkeyType,
           systype.name AS TablePrimarkeyName
    FROM sys.columns colm
        INNER JOIN sys.types systype
            ON colm.system_type_id = systype.system_type_id
    WHERE colm.object_id = obj.object_id
          AND colm.column_id IN (
                                    SELECT ic.column_id
                                    FROM sys.indexes idx
                                        INNER JOIN sys.index_columns ic
                                            ON idx.index_id = ic.index_id
                                               AND idx.object_id = ic.object_id
                                    WHERE idx.object_id = obj.object_id
                                          AND idx.is_primary_key = 1
                                )
) t
WHERE obj.type = 'U';

 

 

 ⑤最終結果:

 

--獲取表的結構  表名,擁有着,行數,是否擁有主鍵,表描述,表的主鍵,表的主鍵名稱
SELECT obj.name Tablename,
       schem.name Schemname,
       idx.rows,
       CAST(CASE
                WHEN
                (
                    SELECT COUNT(1)
                    FROM sys.indexes
                    WHERE object_id = obj.object_id
                          AND is_primary_key = 1
                ) >= 1 THEN
                    1
                ELSE
                    0
            END AS BIT) HasPrimaryKey,
        b.value  TableDesc,
        t.*
FROM sys.objects obj
    INNER JOIN sysindexes idx   --行數
        ON obj.object_id = idx.id
           AND idx.indid <= 1
    INNER JOIN sys.schemas schem   --架構
        ON obj.schema_id = schem.schema_id
    LEFT JOIN sys.extended_properties b  --描述
        ON obj.object_id = b.major_id 
           AND b.minor_id = 0  AND b.name='MS_Description'
    OUTER APPLY  --主鍵名稱和類型
(
    SELECT TOP 1
           colm.name AS TablePrimarkeyType,
           systype.name AS TablePrimarkeyName
    FROM sys.columns colm
        INNER JOIN sys.types systype
            ON colm.system_type_id = systype.system_type_id
    WHERE colm.object_id = obj.object_id
          AND colm.column_id IN (
                                    SELECT ic.column_id
                                    FROM sys.indexes idx
                                        INNER JOIN sys.index_columns ic
                                            ON idx.index_id = ic.index_id
                                               AND idx.object_id = ic.object_id
                                    WHERE idx.object_id = obj.object_id
                                          AND idx.is_primary_key = 1
                                )
) t
WHERE obj.type = 'U'
-- AND obj.name LIKE 'My_%'
ORDER BY obj.name;

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM