-->

Select Extended Property from SQL Server tables

2019-02-25 01:15发布

问题:

I am writing a simple CMS for a client's database. There are 12 tables and they need to manage all of the data in 4 of them.

I set up a dynamic data project (Linq-to-SQL as that's what I'm most familiar with) and the first page makes all 12 tables available for edit as it should. These are in an enumerable list called visibleTables.

Next I went into SSMS and created an extended property called UserEdit and set it to 0 or 1 depending on whether the user should see it on that screen.

What I would love to do is filter the visibleTables list by that extended property but don't know how to.

I am able to see the list of tables and the property with this query:

select major_id, name, value
from sys.extended_properties
where name = 'UserEdit'

and I was going to loop through the visibleTables and remove the ones with 0s but I haven't figured that out either yet as the major_id doesn't seem to be a property I can find.

回答1:

Well, according to http://msdn.microsoft.com/en-us/library/ms177541.aspx, "major_id" for your class of extended properties (OBJECT_OR_COLUMN) really indicates the "object_id". So the following query would get you all the extended properties along with the tables they belong to:

select p.*, t.*
from sys.extended_properties p
inner join sys.tables t on p.major_id = t.object_id
where class = 1

You can filter it as you like, but let me know if you need help.



回答2:

  SELECT major_id, minor_id, t.name AS [Table], c.name AS [Column], value AS [Extended Property]
  FROM sys.extended_properties AS ep
  INNER JOIN sys.tables AS t ON ep.major_id = t.object_id 
  INNER JOIN sys.columns AS c ON ep.major_id = c.object_id AND ep.minor_id = c.column_id
  WHERE class = 1 order by t.name; 

This work for me...