如何拥有一个SQL CLR存储过程的参数SQL_VARIANT? [英] How to have a sql_variant parameter for a SQL CLR stored procedure?
问题描述
如何能一个sql_variant的参数添加到SQL CLR存储过程?使用System.Object的不工作了,我没有看到,我可以使用任何属性。
[Microsoft.SqlServer。 Server.SqlProcedure]
公共静态无效ClearOnePartition(
的SqlString ASTRING
,/ *我想这是一个SQL_VARIANT * /对象aVariant
)
{
//做的东西在这里
}
在从SQL联机丛书映射CLR参数数据 ,对象被列为正确的类型使用地图< A HREF =http://msdn.microsoft.com/en-us/library/ms173829.aspx相对=nofollow> SQL_VARIANT 。
我创建了一个简单的SQL Server项目,并添加以下类吧:
公共部分类StoredProcedures
{
〔Microsoft.SqlServer.Server.SqlProcedure]
公共静态无效StoredProcedure1(对象参数1)
{
//这里$把你的代码为b $ b //Trace.Write(param1 );
SqlContext.Pipe.Send(param1.ToString());
}
};
然后我修改了Test.sql文件的行使此存储过程:
定义@thing SQL_VARIANT ='hahahahaha';
EXEC dbo.StoredProcedure1 @thing
这运行正常,并产生下面的输出:
hahahahaha
没有行受到影响。
(0行(S)返回)
运行完毕sp_executesql的。
块引用>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 }
解决方案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.
这篇关于如何拥有一个SQL CLR存储过程的参数SQL_VARIANT?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!