是否可以向 Microsoft SQL 2000 中的表添加描述/注释?

发布于 2024-07-09 19:47:21 字数 164 浏览 7 评论 0原文

是否可以向 Microsoft SQL 2000 及更高版本中的表添加类似“元数据”的描述或注释?

您将如何通过 CREATE TABLE 语句来做到这一点?

是否可以向字段添加描述或注释?

如何在 MSSQL 2000 中查询此信息? 2005年?

Is it possible to add a "metadata"-like description or comments to a table in Microsoft SQL 2000 and above?

How would you do this through the CREATE TABLE statement?

Is it possible to add a description or comment to fields?

How do you query this info back in MSSQL 2000? 2005?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌梦深 2024-07-16 19:47:21

使用扩展属性。 例如,要将扩展属性添加到 dbo 架构中的表,您可以使用:

EXEC sys.sp_addextendedproperty @name=N'<NameOfProp>', 
@value=N'<Value>' , @level0type=N'SCHEMA',@level0name=N'dbo', 
@level1type=N'TABLE',@level1name=N'<Table>'

您可以更新它们:

EXEC sys.sp_updateextendedproperty 
   @name=N'MS_Description', @value=N'My Description' ,
     @level0type=N'SCHEMA',@level0name=N'dbo', 
      @level1type=N'TABLE'
     ,@level1name=N'<YOUR TABLE NAME>'

您可以像这样读取它们:

SELECT *
FROM fn_listextendedproperty (NULL, 'schema','dbo', 'table', '<yourtable>', default, default);

SELECT
p.name AS [Name],p.value
FROM
sys.tables AS tbl
INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id AND p.minor_id=0 AND p.class=1
WHERE
(tbl.name=N'<yourtablename>' and SCHEMA_NAME(tbl.schema_id)=N'dbo')
ORDER BY
[Name] ASC

Use extended properties. For example to add an extended property to a table in the dbo schema you can use:

EXEC sys.sp_addextendedproperty @name=N'<NameOfProp>', 
@value=N'<Value>' , @level0type=N'SCHEMA',@level0name=N'dbo', 
@level1type=N'TABLE',@level1name=N'<Table>'

You can update them:

EXEC sys.sp_updateextendedproperty 
   @name=N'MS_Description', @value=N'My Description' ,
     @level0type=N'SCHEMA',@level0name=N'dbo', 
      @level1type=N'TABLE'
     ,@level1name=N'<YOUR TABLE NAME>'

You can read them like:

SELECT *
FROM fn_listextendedproperty (NULL, 'schema','dbo', 'table', '<yourtable>', default, default);

or

SELECT
p.name AS [Name],p.value
FROM
sys.tables AS tbl
INNER JOIN sys.extended_properties AS p ON p.major_id=tbl.object_id AND p.minor_id=0 AND p.class=1
WHERE
(tbl.name=N'<yourtablename>' and SCHEMA_NAME(tbl.schema_id)=N'dbo')
ORDER BY
[Name] ASC
紫﹏色ふ单纯 2024-07-16 19:47:21

您当然可以使用上面提到的 SP 来执行此操作,但是,有一种更简单的方法可以执行此操作,并且我相信 MGT Studio 应该是您进行这些更改的首选,除非您尝试使用脚本自动化操作。 操作方法如下:
输入图像描述这里

1- 右键单击​​表格

2- 单击设计

3- 注释如上所示

为了让您获得完整的答案,这里有一个更简单的脚本来更改它(与上面的答案相比更简单):

DECLARE @v sql_variant 
SET @v = N'Comment here'
EXECUTE sp_addextendedproperty N'MS_Description', @v, N'SCHEMA', N'dbo', N'TABLE', N'Stack_testing', N'COLUMN', N'testinghere'

You could surely use the SP mentioned above to do it, however, there is an easier way to do it and I believe MGT Studio should be your first choice for making these changes unless you are trying to automate things using a script. Here is how to do it:
enter image description here

1- Right click on the table

2- Click on Design

3- Comment as shown above

Just so you have a complete answer, here is a simpler script to change it (simpler comparing to above answers):

DECLARE @v sql_variant 
SET @v = N'Comment here'
EXECUTE sp_addextendedproperty N'MS_Description', @v, N'SCHEMA', N'dbo', N'TABLE', N'Stack_testing', N'COLUMN', N'testinghere'
我们的影子 2024-07-16 19:47:21

大多数工具和人们使用扩展属性来支持这一点。 SSMS 使用的通用名称是 MS_Description

有几个内置存储过程用于创建这些属性以及读取它们,它们的过程会随着时间的推移而变化,因此 SQL 版本之间会存在兼容性差异。

Most tools and people use the Extended Properties for supporting this. The common name used by SSMS is MS_Description

There are several built in stored procedures for creating these properties as well as reading them, they procs changed over time so there will be compat differences between SQL versions.

佞臣 2024-07-16 19:47:21

不幸的是,这种方法不适用于高负载的生产环境。 操作扩展属性的存储过程在目标表上设置排他锁。 当它们被执行时,整个表是不可读的。

Unfortunately this approach is not applicable to highly loaded production environments. Stored procedures manipulating extended properties set an exclusive lock on the target table. While they are being executed entire table is unreadable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文