我如何获得所有架构的SQL Server数据库的列表如何获得、架构、数据库、列表

2023-09-03 02:19:00 作者:爱柠檬的女孩

我想找回在给定的SQL Server数据库的所有模式的列表。使用 ADO.NET 模式检索API,我得到的所有集合的列表,但没有收集架构。 我可以遍历'表'程序集合(如果需要的人),并获得独特的名单架构名称,但不存在获得相同的结果的更简单/更短的方法是什么?

I want to retrieve a list of all schemas in a given Sql Server database. Using the ADO.NET schema retrieval API I get a list of all collections but there is no collection for 'Schemas'. I could traverse the 'Tables', 'Procedures' collections (and others if required) and obtain a list of unique schema names but isn't there a easier/shorter way of achieving the same result?

例:对于标准'AdventureWorks的数据库我想也得到了下面的列表 - DBO,HumanResources,人员,生产,采购,销售(我省略了像 db_accessadmin 的db_datareader 等其它标准舍姆的名字)

Example: For the standard 'AdventureWorks' database I would like too obtain the following list - dbo,HumanResources,Person,Production,Purchasing,Sales (I've omitted the other standard schem names like db_accessadmin,db_datareader etc)

编辑:我可以通过查询系统视图获取架构的名单 - INFORMATION_SCHEMA.SCHEMATA ,但使用的架构API作为第一选择将preFER

I can get the list of schemas by querying the system view - INFORMATION_SCHEMA.SCHEMATA but would prefer using the schema API as first choice.

推荐答案

有关2005年以后,这些都将让你在寻找什么。

For 2005 and later, these will both give what you're looking for.

SELECT name FROM sys.schemas
SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA

2000年,这会给数据库的在实例的。

SELECT * FROM INFORMATION_SCHEMA.SCHEMATA

这就是落后不相容指出,在@漂泊的答案。

That's the "backward incompatability" noted in @Adrift's answer.

在SQL Server 2000中(或更低),不会有真正的模式正因为如此,虽然可以以类似的方式使用角色名称空间。在这种情况下,这可能是最接近的等价物

In SQL Server 2000 (and lower), there aren't really "schemas" as such, although you can use roles as namespaces in a similar way. In that case, this may be the closest equivalent.

SELECT * FROM sysusers WHERE gid <> 0