简体   繁体   English

从Sql Server查询所有用户定义的类型?

[英]Query all user defined types from Sql Server?

我如何从sql server中查询所有用户定义的数据类型,就像我对存储在sysobjects中的数据库对象执行此操作的方式一样。

In SQL Server 2005 (and above) you can do this 在SQL Server 2005(及更高版本)中,您可以执行此操作

SELECT * FROM sys.Types WHERE is_user_defined = 1

If you are on SQL Server 2000, you can use SysTypes. 如果您使用的是SQL Server 2000,则可以使用SysTypes。 I think the query is as follows: 我认为查询如下:

SELECT * FROM SysTypes WHERE xusertype > 256

There is no standard query to get all the User defined databases alone. 没有标准查询可以单独获取所有用户定义的数据库。 We can use 我们可以用

select * from sys.databases

But it shows all the Databases, I'm using Server management studio. 但是它显示了所有数据库,我正在使用Server Management Studio。 So by default i have the following databases named : 'Master','tempdb','msdb','model' 因此,默认情况下,我有以下名为: 'Master','tempdb','msdb','model'

So i always use this following query to get the user defined databases. 因此,我始终使用以下查询来获取用户定义的数据库。

select * from sys.databases where name not in('master','model','msdb','tempdb')

It works for fine. 它工作正常。 If you have extra databases by default, it will be the Following (resource,distribution,reportservice, reportservicetemp). 如果默认情况下有其他数据库,则将为“后续”(资源,分发,reportservice,reportservicetemp)。 Just use the names in that query. 只需在该查询中使用名称即可。

You can use the sys.types view for this. 您可以为此使用sys.types视图。 When user_type_id and system_type_id are different, you have a user type. 如果user_type_idsystem_type_id不同,则您具有用户类型。

As AakashM says, the sys.types view is only available from SQL Server 2005 and higher. 正如AakashM所说, sys.types视图仅在SQL Server 2005及更高版本中可用。

EDIT: A better way to determine whether a type is a user-defined type is to check the is_user_defined flag (see Tim C's answer). 编辑:确定类型是否为用户定义类型的更好方法是检查is_user_defined标志(请参见Tim C的答案)。

If you are still on SQL Server 2000 (as your use of ' sysobjects ' suggests), look in systypes . 如果您仍在使用SQL Server 2000(如对sysobjects的使用所建议的那样),请查看systypes If you are on SQL Server 2005 or later, as rwwilden says you should use sys.types (and move to using sys.objects rather than sysobjects !) 如果您使用的是SQL Server 2005或更高版本,则如rwwilden所言,应使用sys.types (并改为使用sys.objects而不是sysobjects !)

You can use the INFORMATION_SCHEMA views. 您可以使用INFORMATION_SCHEMA视图。 INFORMATION_SCHEMA.ROUTINES has UDFs and Stored Procs. INFORMATION_SCHEMA.ROUTINES具有UDF和存储的Procs。 INFORMATION_SCHEMA.TABLES has tables and views. INFORMATION_SCHEMA.TABLES具有表和视图。

Actually system_type_id being different from user_type_id is NOT a good indication. 实际上,system_type_id与user_type_id不同并不是很好的指示。 Note that this: 注意:

SELECT * FROM sys.Types 
WHERE (system_type_id <> user_type_id)
AND is_user_defined = 0.

Returns these: 返回这些:

  • hierarchyid 等级标识
  • geometry 几何
  • geography 地理
  • sysname 系统名称

So use SELECT * FROM sys.types WHERE is_user_defined = 1 as was pointed out by Tim C and Ronald Wildenberg 因此,请使用Tim C和Ronald Wildenberg指出的SELECT * FROM sys.types WHERE is_user_defined = 1

You can also find these in INFORMATION_SCHEMA.DOMAINS. 您也可以在INFORMATION_SCHEMA.DOMAINS中找到它们。 personally I prefer to use that over the sys schema, as it's an ANSI standard that works over multiple types of databases. 我个人更喜欢在sys模式上使用它,因为它是ANSI标准,适用于多种类型的数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM