简体   繁体   English

如何从 CLR 存储过程中获取结果

[英]How to get a result from CLR stored procedure

I have a CLR stored procedure in c# that goes something like我在 c# 中有一个 CLR 存储过程,类似于

public partial class StoredProcedures
{  
    [Microsoft.SqlServer.Server.SqlProcedure]
    public static void DecimalToWords (decimal sum)
    {
        SqlPipe pipe = SqlContext.Pipe;
        SqlMetaData[] cols = new SqlMetaData[1];
        cols[0] = new SqlMetaData("Sum", SqlDbType.NVarChar, 1024);
        SqlDataRecord rec = new SqlDataRecord(cols);

        string result = sum.ToString();

        pipe.SendResultsStart(rec);

        rec.SetSqlString(0, new SqlString(result));
        rec.SetSqlString(0, new SqlString("End of result"));
        pipe.SendResultsEnd();
    }
}

I tried calling it by我试着叫它

GO

DECLARE @return_value int

EXEC    @return_value = [dbo].[DecimalToWords]
        @sum = 10002

SELECT  'Return Value' = @return_value

GO

This return @return_value as 0 and the result of the actual procedure as an empty table with a single column name 'sum' which I assume means that the actual procedure works.这将 @return_value 返回为 0,并将实际过程的结果作为一个空表返回,其中包含单个列名“sum”,我认为这意味着实际过程有效。

I also tried calling it by我也尝试通过调用它

DECLARE @return_value int
Declare @tep_table table
(
  Sum varchar(1024)
)

Insert into @tep_table
EXEC    @return_value = [dbo].[DecimalToWords]
        @sum = 123

Select * From @tep_table

This also returns an empty table.这也返回一个空表。

How would I get the value of 'result' string from my procedure?如何从我的程序中获取“结果”字符串的值? Is there a problem with my procedure code?我的程序代码有问题吗? Am I calling the procedure wrong?我是否调用了错误的程序?

You are missing SendResultsRow :您缺少SendResultsRow

        pipe.SendResultsStart(rec);

        rec.SetSqlString(0, new SqlString(result));
        rec.SetSqlString(0, new SqlString("End of result"));
        pipe.SendResultsRow(rec);
        pipe.SendResultsEnd();

You don't really need the Start/Row/End in your example, but I assume is just an example.在您的示例中,您实际上并不需要 Start/Row/End,但我认为这只是一个示例。 When consuming the result, just execute the procedure and the result set will be sent to the client.消费结果时,只需执行过程,结果集就会发送给客户端。

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

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