如何为 SQL CLR 存储过程提供 sql_variant 参数?

发布于 2024-10-07 00:27:20 字数 294 浏览 0 评论 0原文

如何向 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦上情书 2024-10-14 00:27:20

SQL 联机丛书中的映射 CLR 参数数据对象 被列为用于映射的正确类型< a href="http://msdn.microsoft.com/en-us/library/ms173829.aspx" rel="nofollow">sql_variant。

我创建了一个简单的 SQL Server 项目并向其中添加了以下类:

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void StoredProcedure1(object param1)
    {
        // Put your code here
        //Trace.Write(param1);
        SqlContext.Pipe.Send(param1.ToString());

    }
};

然后我修改了 test.sql 文件来执行此存储过程:

DECLARE @thing sql_variant = 'hahahahaha';

EXEC dbo.StoredProcedure1 @thing

这将按预期运行并产生以下输出:

哈哈哈哈哈哈

没有行受到影响。

(返回 0 行)

已完成运行 sp_executesql。

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:

public partial class StoredProcedures
{
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void StoredProcedure1(object param1)
    {
        // Put your code here
        //Trace.Write(param1);
        SqlContext.Pipe.Send(param1.ToString());

    }
};

I then modified the test.sql file to exercise this stored proc:

DECLARE @thing sql_variant = 'hahahahaha';

EXEC dbo.StoredProcedure1 @thing

This runs as expected and produces the following output:

hahahahaha

No rows affected.

(0 row(s) returned)

Finished running sp_executesql.

仙女山的月亮 2024-10-14 00:27:20

分区边界值,如 CREATE PARTITION FUNCTION 文档中所示,可以是许多不同类型之一:

所有数据类型都可用作分区列,但 text、ntext、image、xml、timestamp、varchar(max)、nvarchar(max)、varbinary(max)、别名数据类型或 CLR 用户定义数据除外类型。

它们的实际数据类型存储在 sys.partition_parameters 中。

但是,如果从 sys.partition_range_values 中选择它们,则 < code>value 字段的类型为 SQL_VARIANT(显然,这是有充分理由的)。

是的,SQL_VARIANT(如前所述)映射到 .NET 中的object

如果您需要检查 NULL,请将对象与 DBNull.Value 进行比较。

如果您想知道对象中值的基础类型(很可能不是 SQL_VARIANT / object ),请使用 GetType( ) 方法:

if (aVariant.GetType() == typeof(SqlInt16))

Partition Boundary values, as shown in the CREATE PARTITION FUNCTION documentation, can be one of many different types:

All data types are valid for use as partitioning columns, except text, ntext, image, xml, timestamp, varchar(max), nvarchar(max), varbinary(max), alias data types, or CLR user-defined data 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 type SQL_VARIANT (and for good reason, obviously).

Yes, SQL_VARIANT (as already stated) maps to object 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 the GetType() method:

if (aVariant.GetType() == typeof(SqlInt16))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文