惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - ji yang

页面点击分析工具设计与实现 使用FreeMind代替PowerPoint做演示 Velocity 2011大会归来 memcached对中文key的支持 更安全的服务器:Windows账号权限修改监控 网站活跃用户统计的思路与设计 以服务的方式提供站点基础功能支持 safari的url地址转义问题 fastdfs dotnet 客户端发布 go lang web项目的部署与发布 从设计图到HTML页面时的注意点 关于加班 关于Google App Engine 一个简单的Spring.net绑定例子 SOA中国路线图活动感受 文章被转载了 Asp.net中基类页的设计和使用 产品营销上的求同策略
通过扩展属性为SqlServer的索引添加注释信息
ji yang · 2011-08-01 · via 博客园 - ji yang

在Sqlserver数据库中,我们可以为字段添加注释信息,这样的好处是方便查看各个字段的含义。
而索引没有默认的地方让你加注释信息,其实通过给索引添加扩展属性,也可以达到这个目的。

选中一个索引:
属性,扩展属性,在表格的名称,值里分别加上:"说明", "这个索引的实际用处" 即可。

如果一个索引同时做了几件事,可以这样添加注释:
(1)插入数据时,根据date+code判断指定的数据是否存在 (2)后台根据date统计一段时间的汇总数据

这样当别人不清楚一个索引的作用时,只要看一下该索引的扩展属性即可。

如果要查看一个数据库里的索引注释列表可以这样查询:

select
      
object_name(major_id) as table_name,
      (select name from sys.indexes where object_id = major_id and index_id = minor_id) as index_name,
      name,
      value
from sys.extended_properties
where class_desc = 'INDEX'

在开发过程中,我们一般会在开发数据库上,指定各个索引的注释,如果要把这儿的注释同步到生产服务器,可以通过脚本来做:

EXEC sp_addextendedproperty @level0type=N'SCHEMA'@level0name=N'dbo',  @level1type=N'TABLE'@level2type=N'INDEX'@level1name=N'表名'@level2name=N'索引名'@name=N'说明'@value=N'索引的实际用处'

更新:sp_addextendedproperty
删除:sp_dropextendedproperty

调用这儿的 sp_ 存储过程可以完成任务,但稍微有点繁琐,这时可以自己创建一个存储过程,方便给索引添加注释信息:

-----------------------------------------------------
-- 给指定表的指定索引设置扩展属性(默认的扩展属性名为:'说明'),方便为索引添加注释
----------------------------------------------------
ALTER PROCEDURE [dbo].[SetIndexDesc]
    
@tablename    nvarchar(200),
    
@indexname    nvarchar(200),
    
@descvalue    nvarchar(500)
AS
BEGIN
      
---先检查索引上是否存在注释信息,如果存在,先删除
      if exists (
            
select * from sys.extended_properties
            
where class_desc = 'INDEX' and object_name(major_id) = @tablename
                 
and (select name from sys.indexes where object_id = major_id and index_id = minor_id) = @indexname
      ) begin
            
EXEC sp_dropextendedproperty N'说明', N'SCHEMA', N'dbo', N'TABLE'@tablename , N'INDEX'@indexname;
      
end---在索引上创建注释信息
      EXEC sp_addextendedproperty
                  
@level0type=N'SCHEMA',
                  
@level0name=N'dbo',
                  
@level1type=N'TABLE',
                  
@level2type=N'INDEX',
                  
@level1name=@tablename,
                  
@level2name=@indexname,
                  
@name=N'说明',
                  
@value=@descvalue
END

调用时:

exec SetIndexDesc '表名''索引名', N'注释信息'

如果需要把开发数据库的索引注释列表同步到生产服务器,可以这样做:

select 'exec SetIndexDesc ''' + object_name(major_id) + '''''' (select name from sys.indexes where object_id = major_id and index_id = minor_id) + ''', N''' + convert(nvarchar(500), value) + ''''
from sys.extended_properties
where class_desc = 'INDEX' and name = N'说明'

把输出的脚本列表,在生产服务器上执行一下即可。

说明:这儿的脚本在SqlServer2005上测试通过。