如何为 SQL CLR 存储过程提供 sql_variant 参数?
如何向 SQL CLR 存储过程添加 sql_variant 参数?使用 System.Object 不起作用,并且我没有看到任何可以使用的属性。
[Microsoft.SqlServer.Server.SqlProcedure]
public static void ClearOnePartition(
SqlString aString
, /* I want this to be a sql_variant */ object aVariant
)
{
//do stuff here
}
How can one add a sql_variant parameter to a SQL CLR stored procedure? Using System.Object does not work, and I don't see any attributes that I can use.
[Microsoft.SqlServer.Server.SqlProcedure]
public static void ClearOnePartition(
SqlString aString
, /* I want this to be a sql_variant */ object aVariant
)
{
//do stuff here
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SQL 联机丛书中的映射 CLR 参数数据 ,对象 被列为用于映射的正确类型< a href="http://msdn.microsoft.com/en-us/library/ms173829.aspx" rel="nofollow">sql_variant。
我创建了一个简单的 SQL Server 项目并向其中添加了以下类:
然后我修改了 test.sql 文件来执行此存储过程:
这将按预期运行并产生以下输出:
In Mapping CLR Parameter Data from SQL Books Online, Object is listed as the correct type to use to map sql_variant.
I created a simple SQL Server project and added the following class to it:
I then modified the test.sql file to exercise this stored proc:
This runs as expected and produces the following output:
分区边界值,如 CREATE PARTITION FUNCTION 文档中所示,可以是许多不同类型之一:
它们的实际数据类型存储在 sys.partition_parameters 中。
但是,如果从 sys.partition_range_values 中选择它们,则 < code>value 字段的类型为
SQL_VARIANT
(显然,这是有充分理由的)。是的,
SQL_VARIANT
(如前所述)映射到 .NET 中的object
。如果您需要检查 NULL,请将对象与
DBNull.Value
进行比较。如果您想知道对象中值的基础类型(很可能不是
SQL_VARIANT
/object
),请使用GetType( )
方法:Partition Boundary values, as shown in the CREATE PARTITION FUNCTION documentation, can be one of many different types:
And their actual datatype is stored in sys.partition_parameters.
But if one is selecting them out of sys.partition_range_values, then the
value
field is of typeSQL_VARIANT
(and for good reason, obviously).Yes,
SQL_VARIANT
(as already stated) maps toobject
in .NET.If you need to check for NULLs, compare the object to
DBNull.Value
.If you want to know the underlying type of the value in the object (it most likely isn't going to be
SQL_VARIANT
/object
), use theGetType()
method: