sqlserver數據庫數據字典生成器


知識點:

1獲取數據庫結構

2添加表說明

3添加字段說明

4導出excel

 

源碼:

https://files.cnblogs.com/files/a735882640/20180825DownTest.zip

說明文檔:

https://files.cnblogs.com/files/a735882640/%E6%95%B0%E6%8D%AE%E5%BA%93%E6%95%B0%E6%8D%AE%E5%AD%97%E5%85%B8%E7%94%9F%E6%88%90%E5%B7%A5%E5%85%B7%E8%AF%B4%E6%98%8E.zip

 

主要代碼:

--1)查詢所有表

 select * from (select top 100000000 ROW_NUMBER() OVER(ORDER BY m.name) as '序號',m.name as '表名',
(SELECT isnull(A.value,'')as value FROM sys.extended_properties A
LEFT JOIN sys.columns B ON A.major_id=B.object_id AND A.minor_id=B.column_id
LEFT JOIN sys.tables C ON A.major_id=C.object_id
WHERE A.class=1 AND C.name=m.name and isnull(B.name,'')='') as '表說明'
FROM sysobjects AS m
WHERE (m.type = 'u')
--and m.name='Fx_Taobao_Fxs'
--{2}
ORDER BY m.name)A
--where A.序號>0 and A.序號<=100


--2)    查詢數據庫結構:


----查詢結構
SELECT
    表名=case   when   a.colorder=1   then   d.name   else   ''   end,
    表說明=case   when   a.colorder=1   then   isnull(f.value,'')   else   ''   end,
    字段序號=a.colorder,
    字段名=a.name,
    標識=case   when   COLUMNPROPERTY(   a.id,a.name,'IsIdentity')=1   then   ''else   ''   end,
    主鍵=case   when   exists(SELECT   1   FROM   sysobjects   where   xtype='PK'   and   name   in   (
        SELECT   name   FROM   sysindexes   WHERE   indid   in(
            SELECT   indid   FROM   sysindexkeys   WHERE   id   =   a.id   AND   colid=a.colid
        )))   then   ''   else   ''   end,
    類型=b.name,
    占用字節數=a.length,
    長度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
    小數位數=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
    允許空=case   when   a.isnullable=1   then   ''else   ''   end,
    默認值=isnull(e.text,''),
    字段說明=isnull(g.[value],'')
FROM   syscolumns   a
    left join   systypes   b   on   a.xusertype=b.xusertype
    inner join   sysobjects   d   on   a.id=d.id     and   d.xtype='U'   and     d.name<>'dtproperties'
    left join   syscomments   e   on   a.cdefault=e.id
    left join   sys.extended_properties   g   on   a.id=g.major_id   and   a.colid=g.minor_id
    left join   sys.extended_properties   f   on   d.id=f.major_id   and   f.minor_id=0
where d.name='MERCHANDISECOMMENT'         --如果只查詢指定表,加上此條件
order by a.id,a.colorder




--3)    添加表說明sql語句:
--------------------------[Account_Type]  添加表說明:賬號類型表
--刪除舊說明
declare @name nvarchar(50)
select @name=a.name from   sys.extended_properties A
                            left join sys.columns B on A.major_id = B.object_id
                                                       and A.minor_id = B.column_id
                            left join sys.tables C on A.major_id = C.object_id
                     where  A.class = 1
                            and C.name = 'Account_Type'
                            and isnull(B.name, '') = '' 
print @name        
if(@name<>'')                    
begin
    execute sp_dropextendedproperty @name,'user','dbo','table','Account_Type',null,null;
end    

--添加新說明
if exists ( select  *
            from    sysobjects
            where   id = object_id(N'[Account_Type]')
                    and objectproperty(id, N'IsUserTable') = 1 )
    and not exists ( select isnull(B.name, '') as name ,
                            A.value
                     from   sys.extended_properties A
                            left join sys.columns B on A.major_id = B.object_id
                                                       and A.minor_id = B.column_id
                            left join sys.tables C on A.major_id = C.object_id
                     where  A.class = 1
                            and C.name = 'Account_Type'
                            and isnull(B.name, '') = '' )
    begin
        execute sp_addextendedproperty N'MS_Description', N'賬號類型表', N'user',
            N'dbo', N'table', N'Account_Type', null, null; 
    end;                         
go    




---4)    添加字段說明sql語句:

--------------------------[Account_Type].[Type_Id] 添加字段說明:類別Id
--刪除舊說明
declare @name nvarchar(50)
select @name=a.name from   sys.extended_properties A
                            left join sys.columns B on A.major_id = B.object_id
                                                       and A.minor_id = B.column_id
                            left join sys.tables C on A.major_id = C.object_id
                     where  A.class = 1
                            and C.name = 'Account_Type'
                            and isnull(B.name, '') = 'Type_Id' 
print @name        
if(@name<>'')                    
begin
    execute sp_dropextendedproperty @name,'user','dbo','table','Account_Type','column','Type_Id';  
end    
--添加新說明
if exists ( select top 1
                        1
                from    INFORMATION_SCHEMA.COLUMNS
                where   [TABLE_NAME] = 'Account_Type'
                        and [COLUMN_NAME] = 'Type_Id' )
    and not exists ( select isnull(B.name, '') as name ,
                            A.value
                     from   sys.extended_properties A
                            left join sys.columns B on A.major_id = B.object_id
                                                       and A.minor_id = B.column_id
                            left join sys.tables C on A.major_id = C.object_id
                     where  A.class = 1
                            and C.name = 'Account_Type'
                            and isnull(B.name, '') = 'Type_Id' )
    begin 
        exec sp_addextendedproperty N'MS_Description', N'類別Id', N'user',
            N'dbo', N'table', N'Account_Type', N'column', N'Type_Id';
    end; 
go

 


免責聲明!

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



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