您能否创建 CLR UDT 以允许跨数据库共享表类型?
如果我有这样的 SQL 语句:
CREATE TYPE [dbo].[typeRateLimitVariables] AS TABLE(
[vchColumnName] [varchar](250) NULL,
[decColumnValue] [decimal](25, 10) NULL
)
并且我将其用作数据库中 UDF 的表变量,那么我就有足够的范围。但是假设我想从同一服务器上的另一个数据库调用标量 UDF,那么我最终会遇到未知类型错误。
我尝试在调用数据库上创建类型,但是很明显。然后我得到类型不匹配,因为尽管每个 UDT 具有相同的名称,但它们具有不同的范围,因此是不同的类型。
我知道您可以创建 CLR 类型,将程序集注册到 SQL Server,然后通用访问自定义类型。
我的想法是创建一个“TABLE”类型的 CLR UDT,但是我看不出这是如何实现的,因为我知道它必须是“SqlDbType.Structured”的 CLR 类型;
我的问题是:
- 有没有一种方法可以不使用 CLR 在 SQL 2008 R2 中为表变量创建全局作用域,如果没有...
- 我如何在 C# CLR 中定义 UDT,其中 UDT 本质上是一个 UDT ”如表”
If I had a SQL statement such as this:
CREATE TYPE [dbo].[typeRateLimitVariables] AS TABLE(
[vchColumnName] [varchar](250) NULL,
[decColumnValue] [decimal](25, 10) NULL
)
And I used it as a table variable to a UDF in a database, I'd have sufficient scope. BUt let's say I wanted to call the scalar UDF from another database on the same server, then I'd end up with an unknown type error.
I've tried creating the type on the calling DB, but obv. then I get a type mismatch because although each of the UDTs have the same name, they have different scopes and therefore are different types.
I know you can create CLR types, register the assembly to SQL Server, and then access the custom type universally.
My idea is to create a CLR UDT of type "TABLE", however I can't see how this can be implemented, as I know it must be of CLR type "SqlDbType.Structured";
My questions are:
- Is there a way without using the CLR to create global scope in SQL 2008 R2 for a table variable, and if not...
- How can I define a UDT in C# CLR, in which the UDT is essentially a UDT "AS TABLE"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你确定吗?用户定义类型是数据库级别的对象,而不是服务器级别的。 “通用”访问它们的唯一方法是将程序集加载到每个数据库中并在每个数据库中创建用户定义类型。 MSDN 文档在 SQL Server 中注册用户定义类型:
回答您的具体问题:
表类型和用户定义类型都不能跨数据库访问,在 CLR UDT 的一种情况下接受如 MSDN 文档中所述。
您不能,因为它们是两个独立的事物(即“类型”与“表类型”),而不是仅仅两种不同的实现方式(即 T-SQL UDF / Stored Proc 与 SQLCLR UDF / Stored Proc)。
编辑:
在纯粹的技术层面上,可以跨数据库使用类型(表类型和用户定义类型),但只能通过< code>USE 命令仅在即席/动态 SQL 中可用。因此,这种用法在实际层面上的适用性有限,但仍然是可能的,如下例所示:
Are you sure about this? User-Defined Types are database-level objects, not server-level. The only way to access them "universally" is by loading the Assembly into each of the databases and creating the User-Defined Type in each database. This much is stated in the MSDN documentation for Registering User-Defined Types in SQL Server:
To answer your specific questions:
Neither Table Types nor User-Defined Types are accessible across databases, accept in the one case for CLR UDTs as noted above in the MSDN documenation.
You cannot as those are two separate things (i.e. a "Type" vs a "Table Type") as opposed to being just two different means of implementation (i.e. T-SQL UDF / Stored Proc vs SQLCLR UDF / Stored Proc).
EDIT:
On a purely technical level, it is possible to use Types (Table Types and User-Defined Types) across databases, but only by switching the current context via the
USE
command which is only usable in ad hoc / dynamic SQL. Hence, this usage has limited applicability on a practical level, but nonetheless it is still possible as the following example shows: